Deep understanding of Spring Series 3: BeanFactory parsing, springbeanfactory

Source: Internet
Author: User

Deep understanding of Spring Series 3: BeanFactory parsing, springbeanfactory

As mentioned in the introduction to one of the Spring Series: the Spring container will parse the class into the BeanDefinition structure in Spring and store BeanDefinition to a DefaultListableBeanFactory, this article will analyze BeanFactory in depth.

First, let's take a look at the ultlistablebeanfactory class diagram,

You can see that DefaultListableBeanFactory indirectly implements the BeanFactory interface. First, analyze this BeanFactory and directly view the BeanFactory source code,

public interface BeanFactory {    String FACTORY_BEAN_PREFIX = "&";    Object getBean(String name) throws BeansException;    <T> T getBean(String name, Class<T> requiredType) throws BeansException;    <T> T getBean(Class<T> requiredType) throws BeansException;    Object getBean(String name, Object... args) throws BeansException;    <T> T getBean(Class<T> requiredType, Object... args) throws BeansException;    boolean containsBean(String name);    boolean isSingleton(String name) throws NoSuchBeanDefinitionException;    boolean isPrototype(String name) throws NoSuchBeanDefinitionException;    boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;    boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;    Class<?> getType(String name) throws NoSuchBeanDefinitionException;    String[] getAliases(String name);}

The method name in the interface can easily understand the intent of each method. The most common or common method is the getBean method, which is used to obtain the Bean instance. BeanFactory is the root interface used to access Spring Bean containers. It is a simple Bean factory, that is, the top-layer definition of IOC containers, various IOC containers are extended to meet different needs, including the frequently used ApplicationContext.

DefaultListableBeanFactory is the core part of Bean loading. It is the default Implementation of Spring registration and Bean loading. The following lists two important attributes in the DefaultListableBeanFactory source code,

/** Map of bean definition objects, keyed by bean name */private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(256); /** List of bean definition names, in registration order */private volatile List<String> beanDefinitionNames = new ArrayList<String>(256);

The bean definition is parsed into BeanDefinition, and beanName is obtained. beanName and BeanDefinition are stored in beanDefinitionMap, and beanName is stored in beanDefinitionNames.

DefaultListableBeanFactory indirectly inherits DefaultSingletonBeanRegistry, which has the following attributes,

/** Cache of singleton objects: bean name --> bean instance */private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(256);

SingletonObjects is used to store instances of a single-instance bean. The getBean method is to retrieve instance objects from this Map.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.