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
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < context:annotation-config  />
  
     < bean  id="customerService" class="com.mkyong.customer.services.CustomerService">
         < property  name="message" value="i'm property message" />
     </ bean >
  
</ beans >

  

目录
相关文章
|
18天前
|
缓存 Java Spring
Spring 框架中 Bean 的生命周期
Spring 框架中 Bean 的生命周期
30 1
|
5月前
|
Java Spring 容器
Spring源码:Bean的生命周期(二)
FactoryBean 和 BeanFactory 是两个不同的概念。前者是一个接口,我们可以在实现该接口时通过调用 getObject 方法来返回实例,同时 FactoryBean 本身也是一个实例。后者是 Spring 容器的工厂,通过其中的 bean 定义 Map 一个一个地实例化我们通过注解等方式注入进去的 bean 工厂。在判断 FactoryBean 时,如果当前 BeanFactory 中没有对应的 bean 定义,那么就会去父容器中寻找相应的 bean 定义并进行判断。如果我们的类实现了 SmartFactoryBean 接口,那么它将会在 Spring 容器启动时就会调用 g
|
2天前
|
Java 数据库连接 开发者
浅谈Spring的Bean生命周期
浅谈Spring的Bean生命周期
10 1
|
5月前
|
XML Java 数据格式
Spring之Bean的生命周期
Spring之Bean的生命周期
55 0
|
1月前
|
Java Spring
Spring5深入浅出篇:bean的生命周期
Spring5深入浅出篇:bean的生命周期
|
5月前
|
存储 缓存 Java
Spring源码:Bean生命周期(三)
在之前的文章中,我们已经对 `bean` 的准备工作进行了讲解,包括 `bean` 定义和 `FactoryBean` 判断等。在这个基础上,我们可以更加深入地理解 `getBean` 方法的实现逻辑,并在后续的学习中更好地掌握`createBean` 方法的实现细节。
Spring源码:Bean生命周期(三)
|
3月前
|
XML Java 数据格式
Spring Bean的生命周期解读
Spring IOC 容器的设计主要是基于BeanFactory和ApplicationContext两个接口,其中ApplicationContext是BeanFactory的子接口之一,换句话说BeanFactory是Spring IOC容器所定义的最顶层接口,而ApplicationContext是其高级接口之一,并且对于BeanFactory功能做了许多有用的扩展,所以在绝大部分的工作场景中,都会使用ApplicationContext作为Spring IOC 容器。
|
3月前
|
缓存 Java Spring
Spring5源码(25)-Spring填充bean属性及应用生命周期接口
Spring5源码(25)-Spring填充bean属性及应用生命周期接口
38 0
|
3月前
|
XML 缓存 Java
Spring5源码(9)-Bean的作用域和生命周期
Spring5源码(9)-Bean的作用域和生命周期
19 0
|
3月前
|
Java Spring 容器
深入Spring原理-2.Bean的生命周期
深入Spring原理-2.Bean的生命周期
45 0