Spring is very large, there are a lot of design patterns, and programming several wind and rain Spring is like an ageless urf, now open Spring source analysis tour, this article mainly write the composition of Spring and its structure and each class responsibility introduction

Spring has two core classes

A DefaultListableBeanFactory

  • DefaultListableBeanFactory

DefaultListableBeanFactory is the core part of the bean loading, contains all the bean information, XmlBeanFactory inherited his, The difference is that XmlBeanFactory uses a personalized XmlBeanDefinitionReader to read configuration files

   public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
        super(parentBeanFactory);
        this.reader = new XmlBeanDefinitionReader(this);
        this.reader.loadBeanDefinitions(resource);
    }
    
Copy the code
  • AliasRegistry

    Define a simple add, delete, change or search operation on Alias. Alias is an Alias, for example, map.put(“Yanglele”, yanglele.class); Yanglele is the alias of Yanglele. Class in the container

    public interface AliasRegistry {
         void registerAlias(String var1, String var2);
    
         void removeAlias(String var1);
    
         boolean isAlias(String var1);
    
         String[] getAliases(String var1);
     }
    Copy the code
  • SimpleAliasRegistry

      public class SimpleAliasRegistry implements AliasRegistry {
         protected final Log logger = LogFactory.getLog(this.getClass());
         private final Map<String, String> aliasMap = new ConcurrentHashMap(16);
         
         -----
    Copy the code

    Map is primarily used as a cache for aliases and AliasRegistry is implemented

  • BeanDefinitionRegistry

     public interface BeanDefinitionRegistry extends AliasRegistry {
         void registerBeanDefinition(String var1, BeanDefinition var2) throws BeanDefinitionStoreException;
    
         void removeBeanDefinition(String var1) throws NoSuchBeanDefinitionException;
    
         BeanDefinition getBeanDefinition(String var1) throws NoSuchBeanDefinitionException;
    Copy the code

    Define various operations for adding, deleting, changing, and checking beanDefinitions

  • SingletonBeanRegistry

    Defines the registration and retrieval of singletons

     public interface SingletonBeanRegistry {
          void registerSingleton(String var1, Object var2);
    
          @Nullable
          Object getSingleton(String var1);
    
          boolean containsSingleton(String var1);
    
          String[] getSingletonNames();
    
          int getSingletonCount();
    
          Object getSingletonMutex();
      }
    Copy the code
  • DefaultSingletonBeanRegistry

    Implementation of functions in interface SingletonBeanRegistry

    public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry { private static final int SUPPRESSED_EXCEPTIONS_LIMIT = 100; private final Map<String, Object> singletonObjects = new ConcurrentHashMap(256); private final Map<String, ObjectFactory<? >> singletonFactories = new HashMap(16); private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap(16);Copy the code
  • FactoryBeanRegistrySupport

On the basis of DefaultSingletonBeanRegistry added to FactoryBean special function

  • BeanFactory

      public interface BeanFactory {
          String FACTORY_BEAN_PREFIX = "&";
    
          Object getBean(String var1) throws BeansException;
    
          <T> T getBean(String var1, Class<T> var2) throws BeansException;
          --------
    Copy the code

    Defines the various properties that get beans and beans

  • HierarchicalBeanFactory

     public interface HierarchicalBeanFactory extends BeanFactory {
          @Nullable
          BeanFactory getParentBeanFactory();
    
          boolean containsLocalBean(String var1);
     }
    Copy the code

    Inherit BeanFactory and add support for parentFactory on the basis of BeanFactory

    Two XmlBeanDefinitionReader

    XmlBeanFactory inherits from this, except that XmlBeanFactory uses the personalized XmlBeanDefinitionReader to read configuration files

  • EnvironmentCapable

    Define the method to get the Environment

  • BeanDefinitionReader

Define the resource file to read and convert to BeanDefinition

       public interface BeanDefinitionReader {
        BeanDefinitionRegistry getRegistry();

        @Nullable
        ResourceLoader getResourceLoader();

        @Nullable
        ClassLoader getBeanClassLoader();
        ------
Copy the code
  • AbstractBeanDefinitionReader

    Implement methods in BeanDefinitionReader and EnvironmentCapable

      public abstract class AbstractBeanDefinitionReader 
      implements BeanDefinitionReader, EnvironmentCapable {
         protected final Log logger = LogFactory.getLog(this.getClass());
    Copy the code