Spring Bean的生命周期

1. 实例化Bean对象,这个时候Bean的对象是非常低级的,基本不能够被我们使用,因为连最基本的属性都没有设置,可以理解为连Autowired注解都是没有解析的;
2. 填充属性,当做完这一步,Bean对象基本是完整的了,可以理解为Autowired注解已经解析完毕,依赖注入完成了;
3. 如果Bean实现了BeanNameAware接口,则调用setBeanName方法;
4. 如果Bean实现了BeanClassLoaderAware接口,则调用setBeanClassLoader方法;
5. 如果Bean实现了BeanFactoryAware接口,则调用setBeanFactory方法;
6. 调用BeanPostProcessor的postProcessBeforeInitialization方法;
7. 如果Bean实现了InitializingBean接口,调用afterPropertiesSet方法;
8. 如果Bean定义了init-method方法,则调用Bean的init-method方法;
9. 调用BeanPostProcessor的postProcessAfterInitialization方法;当进行到这一步,Bean已经被准备就绪了,一直停留在应用的上下文中,直到被销毁
10. 如果应用的上下文被销毁了,如果Bean实现了DisposableBean接口,则调用destroy方法,如果Bean定义了destory-method声明了销毁方法也会被调用。
为了验证上面的逻辑,可以做个试验:
首先定义了一个Bean,里面有各种回调和钩子,其中需要注意下,我在SpringBean的构造方法中打印了studentService,看SpringBean被new的出来的时候,studentService是否被注入了;又在
setBeanName中打印了studentService,看此时studentService是否被注入了,以此来验证,Bean是何时完成的自动注入的:
1 public class SpringBean implements InitializingBean , DisposableBean, BeanNameAware, BeanFactoryAware,
BeanClassLoaderAware {
2
3 public SpringBean () {
4 System.out. println ( "SpringBean 构造方法 :" + studentService);
5 System.out. println ( "SpringBean 构造方法 " );
6 }
7 8 @Autowired
9 StudentServiceImpl studentService;
10
11 @Override
12 public void afterPropertiesSet () throws Exception {
13 System.out. println ( "afterPropertiesSet" );
14 }
15
16 @Override
17 public void destroy () throws Exception {
18 System.out. println ( "destroy" );
19 }
20
21 @Override
22 public void setBeanClassLoader (ClassLoader classLoader) {
23 System.out. println ( "setBeanClassLoader" );
24 }
25
26 @Override
27 public void setBeanFactory (BeanFactory beanFactory) throws BeansException {
28 System.out. println ( "setBeanFactory" );
29 }
30
31 @Override
32 public void setBeanName (String name) {
33 System.out. println ( "setBeanName:" + studentService);
34 System.out. println ( "setBeanName" );
35 }
36
37 public void initMethod () {
38 System.out. println ( "initMethod" );
39 }
40
41 public void destroyMethod () {
42 System.out. println ( "destroyMethod" );
43 }
44 }
再定义一个BeanPostProcessor,在重写的两个方法中进行了判断,如果传进来的beanName是
springBean才进行打印:
1 @Component
2 public class MyBeanPostProcessor implements BeanPostProcessor {
3 @Override
4 public Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException {
5 if (beanName. equals ( "springBean" )) {
6 System.out. println ( "postProcessBeforeInitialization" );
7 }
8 return bean;
9 }
10
11 @Override
12 public Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException { 13 if (beanName. equals ( "springBean" )) {
14 System.out. println ( "postProcessAfterInitialization" );
15 }
16 return bean;
17 }
18 }

 

定义一个配置类,完成自动扫描,但是SpringBean是手动注册的,并且声明了initMethod和
destroyMethod:
@Configuration
2 @ComponentScan
3 public class AppConfig {
4 @Bean(initMethod = "initMethod" ,destroyMethod = "destroyMethod" )
5 public SpringBean springBean () {
6 return new SpringBean ();
7 }
8 }

最后就是启动类了:

1 public static void main (String[] args) {
2 AnnotationConfigApplicationContext annotationConfigApplicationContext =
3 new AnnotationConfigApplicationContext (AppConfig. class );
4 annotationConfigApplicationContext. destroy ();
5 }

 

运行结果:

1 SpringBean 构造方法 : null
2 SpringBean 构造方法
3 setBeanName : com.codebear.StudentServiceImpl@31190526
4 setBeanName
5 setBeanClassLoader
6 setBeanFactory
7 postProcessBeforeInitialization
8 afterPropertiesSet
9 initMethod
10 postProcessAfterInitialization
11 destroy
12 destroyMethod

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值