读Spring源码一

 

Beans   Core :这两个模块是整个框架的基础模块,提供控制翻转和依赖注入 。(Ioc/DI) 主要的类是BeanFactory(延时加载)

Context :构建与Beans和Core模块之上,对它们进行了扩展,主要类是ApplicationContext(非延时加载)

 

先看下前提基础知识:

Bean的初始化方法和销毁方法.  4类
①:什么是bean的生命周期?
  bean的创建----->初始化----->销毁方法
  由容器管理Bean的生命周期,我们可以通过自己指定bean的初始化方法和bean的销毁方法
 

@Configuration
public class MainConfig {
   
    //指定了bean的生命周期的初始化方法和销毁方法.
    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Car car() {
        return new Car();
    }
}


  针对单实例bean的话,容器启动的时候,bean的对象就创建了,而且容器销毁的时候,也会调用Bean的销毁方法
  针对多实例bean的话,容器启动的时候,bean是不会被创建的而是在获取bean的时候被创建,而且bean的销毁不受
IOC容器的管理.
②:通过  InitializingBean和DisposableBean 的二个接口实现bean的初始化以及销毁方法

@Component
public class Person implements InitializingBean,DisposableBean {
    public Person() {
        System.out.println("Person的构造方法");
    }
    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean的destroy()方法 ");
    }

   /**
     * InitializingBean 接口方法定义
     * 该接口仅有一个afterPropertiesSet方法,该方法会在bean的所有属性被设置完成
     * (包括各种Aware的设置,如BeanFactoryAware, ApplicationContextAware等)后由容器 
        (BeanFactory)调用
     *
     * 此方法允许bean实例在设置了所有bean属性后执行总体配置的验证和最终初始化

     * 如果一个类在实现InitializingBean的同时又在配置文件中指定了 
       init-method,系统则是先调用afterPropertieSet()方法,然后再调用init-method中指定的方法。

     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean的 afterPropertiesSet方法");
    }
}


③:通过JSR250规范 提供的注解@PostConstruct 和@ProDestory标注的方法

@Component
public class Book {
    public Book() {
        System.out.println("book 的构造方法");
    }

/**
@PostConstruct是Java自己的注解,而非Spring的注解。

Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法。被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。

通常我们会是在Spring框架中使用到@PostConstruct注解 该注解的方法在整个Bean初始化中的执行顺序:

Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)
*/

    @PostConstruct
    public void init() {
        System.out.println("book 的PostConstruct标志的方法");
    }
    @PreDestroy
    public void destory() {
        System.out.println("book 的PreDestory标注的方法");
    }
}


④:通过Spring的BeanPostProcessor的  bean的后置处理器会拦截所有bean创建过程
     postProcessBeforeInitialization 在init方法之前调用
     postProcessAfterInitialization 在init方法之后调用

@Component
public class TulingBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("TulingBeanPostProcessor...postProcessBeforeInitialization:"+beanName);
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("TulingBeanPostProcessor...postProcessAfterInitialization:"+beanName);
        return bean;
    }
}


    BeanPostProcessor的执行时机      
populateBean(beanName, mbd, instanceWrapper)
initializeBean{
    applyBeanPostProcessorsBeforeInitialization()
    invokeInitMethods{
        isInitializingBean.afterPropertiesSet
        自定义的init方法
    }
    applyBeanPostProcessorsAfterInitialization()方法
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值