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,如需转载请自行联系原作者
相关文章
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
1197 26
|
Java Spring
在使用Spring的`@Value`注解注入属性值时,有一些特殊字符需要注意
【10月更文挑战第9天】在使用Spring的`@Value`注解注入属性值时,需注意一些特殊字符的正确处理方法,包括空格、引号、反斜杠、新行、制表符、逗号、大括号、$、百分号及其他特殊字符。通过适当包裹或转义,确保这些字符能被正确解析和注入。
1224 3
|
Java Spring
一键注入 Spring 成员变量,顺序编程
介绍了一款针对Spring框架开发的插件,旨在解决开发中频繁滚动查找成员变量注入位置的问题。通过一键操作(如Ctrl+1),该插件可自动在类顶部添加`@Autowired`注解及其成员变量声明,同时保持光标位置不变,有效提升开发效率和代码编写流畅度。适用于IntelliJ IDEA 2023及以上版本。
418 2
一键注入 Spring 成员变量,顺序编程
|
缓存 Java Spring
实战指南:四种调整 Spring Bean 初始化顺序的方案
本文探讨了如何调整 Spring Boot 中 Bean 的初始化顺序,以满足业务需求。文章通过四种方案进行了详细分析: 1. **方案一 (@Order)**:通过 `@Order` 注解设置 Bean 的初始化顺序,但发现 `@PostConstruct` 会影响顺序。 2. **方案二 (SmartInitializingSingleton)**:在所有单例 Bean 初始化后执行额外的初始化工作,但无法精确控制特定 Bean 的顺序。 3. **方案三 (@DependsOn)**:通过 `@DependsOn` 注解指定 Bean 之间的依赖关系,成功实现顺序控制,但耦合性较高。
1537 4
实战指南:四种调整 Spring Bean 初始化顺序的方案
|
存储 Java Spring
【Spring】获取Bean对象需要哪些注解
@Conntroller,@Service,@Repository,@Component,@Configuration,关于Bean对象的五个常用注解
653 12
|
存储 Java 应用服务中间件
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
792 12
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
715 6
|
XML Java 数据格式
🌱 深入Spring的心脏:Bean配置的艺术与实践 🌟
本文深入探讨了Spring框架中Bean配置的奥秘,从基本概念到XML配置文件的使用,再到静态工厂方式实例化Bean的详细步骤,通过实际代码示例帮助读者更好地理解和应用Spring的Bean配置。希望对你的Spring开发之旅有所助益。
|
安全 Java 开发者
Spring容器中的bean是线程安全的吗?
Spring容器中的bean默认为单例模式,多线程环境下若操作共享成员变量,易引发线程安全问题。Spring未对单例bean做线程安全处理,需开发者自行解决。通常,Spring bean(如Controller、Service、Dao)无状态变化,故多为线程安全。若涉及线程安全问题,可通过编码或设置bean作用域为prototype解决。
556 1