博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring中bean注入前后的一些操作:
阅读量:6296 次
发布时间:2019-06-22

本文共 4637 字,大约阅读时间需要 15 分钟。

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,如需转载请自行联系原作者
你可能感兴趣的文章
Android WebView 学习
查看>>
(转)从给定的文本中,查找其中最长的重复子字符串的问题
查看>>
HDU 2159
查看>>
spring batch中用到的表
查看>>
资源文件夹res/raw和assets的使用
查看>>
UINode扩展
查看>>
LINUX常用命令
查看>>
百度云盘demo
查看>>
概率论与数理统计习题
查看>>
初学structs2,简单配置
查看>>
Laravel5.0学习--01 入门
查看>>
时间戳解读
查看>>
sbin/hadoop-daemon.sh: line 165: /tmp/hadoop-hxsyl-journalnode.pid: Permission denied
查看>>
@RequestMapping 用法详解之地址映射
查看>>
254页PPT!这是一份写给NLP研究者的编程指南
查看>>
《Data Warehouse in Action》
查看>>
String 源码浅析(一)
查看>>
Spring Boot 最佳实践(三)模板引擎FreeMarker集成
查看>>
Fescar 发布 0.2.3 版本,支持 Redis 和 Apollo
查看>>
Google MapReduce到底解决什么问题?
查看>>