Simple analysis of Spring core IOC container principle

Source: Internet
Author: User

The general process of parsing one side, the specific can see the source code a method of a method of the following

XmlBeanFactoryfunction is DefaultListableBeanFactory based on this basic container and implements additional functions, such as XML reads, on the basis of this basic container.

DefaultListableBeanFactoryis BeanFactory a default implementation class.

public class XmlBeanFactory extends DefaultListableBeanFactory {    private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);        /**     * Resource是Spring中对与外部资源的抽象,最常见的是对文件的抽象,特别是XML文件。     * 而且Resource里面通常保存了Spring使用者的Bean定义     * 比如applicationContext.xml在被加载时,就会被抽象为Resource来处理。     */    public XmlBeanFactory(Resource resource) throws BeansException {        this(resource, null);    }        public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {        super(parentBeanFactory);        this.reader.loadBeanDefinitions(resource);    }}

Simple example:

public class MySimpleBeanFactory {    public static void main(String[] args) {        ClassPathResource resource = new ClassPathResource("META-INF/beans.xml");        BeanFactory beanFactory = new XmlBeanFactory(resource);        User user = beanFactory.getBean("user1", User.class);        System.out.println(user.getName()+" : "+user.getAge());    }}

Steps to create a container:

    1. To create an abstraction of a bean configuration fileResource
    2. Created BeanFactory , for example:XmlBeanFactory
    3. Parse the bean and create a BeanDefinition reader, for example XmlBeanDefinitionReader : DefaultBeanDefinitionDocumentReaderBeanDefinitionParserDelegate
    4. Get BeanDefinition post-registration into DefaultListableBeanFactory :
// 最后BeanDefinition就是放到这个map里的Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(256);

Will gradually improve in the future.

Simple analysis of Spring core IOC container principle

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.