Spring中bean注入前后的一些操作:

InitializingBean 和 DisposableBean

init-method 和 destroy-method

@PostConstruct 和 @PreDestroy

In Spring, InitializingBean and DisposableBean are two marker interfaces, a useful way for Spring to perform certain actions upon bean initialization and destruction.

  1. For bean implemented InitializingBean, it will run afterPropertiesSet() after all bean properties have been set.
  2. For bean implemented DisposableBean, it will run destroy() after Spring container is released the bean.

In Spring, you can use init-method and destroy-method as attribute in bean configuration file for bean to perform certain actions upon initialization and destruction.

Note
The @PostConstruct and @PreDestroy annotation are not belong to Spring, it’s located in the J2ee library – common-annotations.jar.

具体的使用

对于第一个:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import  org.springframework.beans.factory.DisposableBean;
import  org.springframework.beans.factory.InitializingBean;
  
public  class  CustomerService  implements  InitializingBean, DisposableBean
{
     String message;
  
     public  String getMessage() {
       return  message;
     }
  
     public  void  setMessage(String message) {
       this .message = message;
     }
  
     public  void  afterPropertiesSet()  throws  Exception {
       System.out.println( "Init method after properties are set : "  + message);
     }
  
     public  void  destroy()  throws  Exception {
       System.out.println( "Spring Container is destroy! Customer clean up" );
     }
  
}

  下面的例子展示了 init-method and destroy-method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public  class  CustomerService
{
     String message;
  
     public  String getMessage() {
       return  message;
     }
  
     public  void  setMessage(String message) {
       this .message = message;
     }
  
     public  void  initIt()  throws  Exception {
       System.out.println( "Init method after properties are set : "  + message);
     }
  
     public  void  cleanUp()  throws  Exception {
       System.out.println( "Spring Container is destroy! Customer clean up" );
     }
  
}

  

1
2
3
4
5
6
7
8
9
10
11
12
< beans  xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  
     < bean  id="customerService" class="com.mkyong.customer.services.CustomerService"
         init-method="initIt" destroy-method="cleanUp">
  
         < property  name="message" value="i'm property message" />
     </ bean >
  
</ beans >

  第三种的使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import  javax.annotation.PostConstruct;
import  javax.annotation.PreDestroy;
  
public  class  CustomerService
{
     String message;
  
     public  String getMessage() {
       return  message;
     }
  
     public  void  setMessage(String message) {
       this .message = message;
     }
  
     @PostConstruct
     public  void  initIt()  throws  Exception {
       System.out.println( "Init method after properties are set : "  + message);
     }
  
     @PreDestroy
     public  void  cleanUp()  throws  Exception {
       System.out.println( "Spring Container is destroy! Customer clean up" );
     }
  
}

  By default, Spring will not aware of the @PostConstruct and @PreDestroy annotation. To enable it, you have to either register ‘CommonAnnotationBeanPostProcessor‘ or specify the ‘<context:annotation-config />‘ in bean configuration file,

1. CommonAnnotationBeanPostProcessor
1
2
3
4
5
6
7
8
9
10
11
12
< beans  xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  
     < bean  class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
  
     < bean  id="customerService" class="com.mkyong.customer.services.CustomerService">
         < property  name="message" value="i'm property message" />
     </ bean >
  
</ beans >

  

2. <context:annotation-config />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< beans  xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  
     < context:annotation-config  />
  
     < bean  id="customerService" class="com.mkyong.customer.services.CustomerService">
         < property  name="message" value="i'm property message" />
     </ bean >
  
</ beans >

  


==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/12/27/2835289.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBean对象的生命周期可以分为以下几个步骤: 1. 加载Bean定义:在Spring容器启动时,会读取配置文件或注解,将Bean定义加载到内存。这些定义描述了Bean的各种属性,如的路径、初始化方法、销毁方法等。 2. 创建Bean实例:根据加载的Bean定义,Spring容器会使用合适的实例化策略创建Bean的实例。这可能涉及到使用构造函数、工厂方法或反射来实例化Bean。 3. 设置Bean属性:在创建Bean实例后,Spring容器会使用依赖注入的方式设置Bean的属性。这包括通过构造函数、setter方法或字段注入来为Bean的属性赋值。 4. 调用Bean的初始化方法:一旦Bean的所有属性都被设置好,Spring容器会调用Bean的初始化方法。这个初始化方法可以通过在Bean定义配置指定的方法名来调用,也可以实现InitializingBean接口来定义初始化逻辑。 5. 注册Bean后置处理器:在Bean的初始化方法调用之前,Spring容器会注册Bean后置处理器,这些处理器可以在Bean初始化前后对Bean进行自定义处理。这个阶段的主要方法是registerBeanPostProcessors。 6. Bean的使用:一旦Bean的初始化方法调用完成,Bean就可以被应用程序使用了。 7. 销毁Bean:当Spring容器关闭时,会调用Bean的销毁方法进行资源的释放。这个销毁方法可以通过在Bean定义配置指定的方法名来调用,也可以实现DisposableBean接口来定义销毁逻辑。 总结来说,SpringBean对象的生命周期包括加载Bean定义、创建Bean实例、设置Bean属性、调用初始化方法、注册Bean后置处理器、Bean的使用和销毁Bean的过程。这些步骤确保了Bean在Spring容器的正确创建、初始化和销毁,从而实现了灵活的Bean管理和生命周期控制。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值