Three common usage and source Spring source code analysis of the IOC to achieve (a)

A, SpringIOC source code analysis

1. Core functions bean configuration and access api

There are four

 

 

 

 

(P175 from the proficient spring4.x)

The first three are common

The first way

<?xml version="1.0" encoding="GBK"?> 

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

    xmlns="http://www.springframework.org/schema/beans" 

    xsi:schemaLocation="http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> 
<!-- 指定class属性,通过构造方法创建Bean实例 --> <bean id="person" class="com.mao.gouzao.Person"> </bean> </beans> public static void main( String[] args ) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); System.out.println(ctx.getBean("person")); }

 

 

The second way

definition

@Service

public class UserServiceImpl {
    public void test(){
        System.out.println(666);
    }
}

 

Obtain

@Autowired

private static UserServiceImpl userService;

 

The third way

 definition

@Configuration

public class MainConfig {
    @Bean
    public Person person(){
        return new Person();
    }
}

Note : By @Bean form is the use of the words, bean default name is the method name, if @Bean (value = "bean name ") then the bean name is specified

To read the container Bean information (incoming class configuration) 

 Obtain

public static void main( String[] args )

{
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class);
    System.out.println(ctx.getBean("person"));
}

 

2. How to achieve

1. Based on the configuration of the implementation of Java class source code analysis

1. The implementation of these two codes

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class);
System.out.println(ctx.getBean("person"));

 

Take a look at open-source constructor:

public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
   this();
   register(annotatedClasses);
   refresh();
}

 

2. First, do not worry, the protagonist child AnnotationConfigApplicationContext it has a parent, because the relationship between class initialization order, will first initialize the parent class, so you have to look at the parent, the parent class has finally seen as DefaultResourceLoader

 

 

 

 

DefaultResourceLoader, it is clear that resources are used to load, no-argument constructor in the penultimate AbstractApplicationContext also initialize a load resources Related:

 

Ok, a look at the last GenericApplicationContext the parent, the parent class using the following default initialization no arguments

public GenericApplicationContext() {
   this.beanFactory = new DefaultListableBeanFactory();
}

 

Here is the creation of an important class DefaultListableBeanFactory springioc System, DefaultListableBeanFactory which is critical to achieve the ioc associated with many features to offer to our AnnotationConfigApplicationContext use (see later)

 

3.ok parent class are read, and then back to our protagonist a child AnnotationConfigApplicationContext

We look at its constructor is just what code (return itself):

public AnnotationConfigApplicationContext() {
   this.reader = new AnnotatedBeanDefinitionReader(this);
   this.scanner = new ClassPathBeanDefinitionScanner(this);
}

On two lines.

 

4. The first look at a this.reader = new AnnotatedBeanDefinitionReader (this);

Go

public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry) {
   this(registry, getOrCreateEnvironment(registry));
}

 

It calls its constructor again

There is also directed to a method for getOrCreateEnvironment (registry)

Guess you like

Origin www.cnblogs.com/chz-blogs/p/11694405.html