spring的IOC容器与DI依赖注入

spring-context 4.3.9release版本
注解类型注入讲解

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
/**
 * @author laowang
 * @date 2018/12/21 3:25 PM
 * @Description:
 */
@Configuration  // 告诉spring 这是一个配置类
@ComponentScan(value = "com.myself.spring.annotion.service")
public class MainConfig2 {


    @Autowired
    BookService bookService;
    /**
     * @return
     */
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    @Bean(value = "person")   // 给容器注册一个bean,类型为返回值类型,id默认是用方法名
    public Person person() {
        System.out.println("person 注册到容器中");
        return new Person("少林", 28);
    }

    /**
     * 给容器注册一个bean,类型为返回值类型,id默认是用方法名
     *
     * @return
     */
    @Bean
    public Color getColor() {
        System.out.println("Color 注册到容器中");
        return new Color();
    }
}
/**
 * @author laowang
 * @date 2018/12/21 3:27 PM
 * @Description:dfaq
 */
public class Person implements BeanNameAware , BeanFactoryAware , ApplicationContextAware, BeanPostProcessor , InitializingBean {

    private String name;
    private int age;

    public Person( ) {
        System.out.println("无参构造器。。。");
    }

    public Person(String name, int age) {
        System.out.println("有参构造器。。。");

        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("BeanNameAware setBeanName : " + name);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("BeanFactoryAware setBeanFactory : " + beanFactory.toString());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("ApplicationContextAware setApplicationContext");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor postProcessBeforeInitialization");

        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor postProcessAfterInitialization");

        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean afterPropertiesSet");

    }
}
/**
 * @author laowang
 * @date 2018/12/25 9:26 AM
 * @Description:
 */
public class Color {

    public Color() {
        System.out.println("构造器。。。");
    }

    public void init(){
        System.out.println("init...");
    }

    public void destroy(){
        System.out.println("destroy...");
    }
}

debug 入口

/**
	 * Create a new AnnotationConfigApplicationContext, deriving bean definitions
	 * from the given annotated classes and automatically refreshing the context.
	 * @param annotatedClasses one or more annotated classes,
	 * e.g. {@link Configuration @Configuration} classes
	 */
	public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
		this();
		register(annotatedClasses);
		refresh();
	}

思路大概: register(annotatedClasses);方法完成将类层次的bean注入到beanDefinition
Map里,然后是执行刷新工厂方法refresh();

@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				initMessageSource();

				// Initialize event multicaster for this context.
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				onRefresh();

				// Check for listener beans and register them.
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				finishRefresh();
			}

思路:invokeBeanFactoryPostProcessors(beanFactory);方法,是完成每个已注入类中的子类注入,比如@Bean,@import等,@Autowire,DI依赖注入,防止循环依赖注入,spring是提供了一个提前暴露类-----,注意spring的防止依赖注入是只针对单例的
而registerBeanPostProcessors(beanFactory);方法中有一个 BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);方法,通过getBean方法来实例化注入的bean,涉及到了bean的声明周期知识点

以上是我读代码的薄见,不对的地方请指教
下面是我debug、的导图,请指教
spring的IOC容器与DI依赖注入_第1张图片

spring的IOC容器与DI依赖注入_第2张图片

spring的IOC容器与DI依赖注入_第3张图片

spring的IOC容器与DI依赖注入_第4张图片
这个是防止单例的循环依赖问题—spring提供了当前bean的暴露类,提供依赖 – 个人理解
spring的IOC容器与DI依赖注入_第5张图片

你可能感兴趣的:(spring框架)