---
title: "通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 "
author: "wisfly"
date: "2016-04-11T16:39:07Z"
tags: []
categories: ["行业应用"]
slug: "spring-postconstruct-predestroy-bean"
draft: false
lastmod: "2020-04-27T18:49:23Z"
id: 2290044
url: "https://wisfly.iteye.com/blog/2290044"
---

转：

 小伙伴们，我开了一家海淘护肤品淘宝店，搜索店铺“禾子蝶的海淘铺”，正品保证，欢迎进店选购哦。谢谢！

 

关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种：

 

[第一种：通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作](http://write.blog.csdn.net/postedit/8681497)

 

第二种是：[通过 在xml中定义init-method 和  destory-method方法](http://blog.csdn.net/topwqp/article/details/8681467)

 

第三种是： [通过bean实现InitializingBean和 DisposableBean接口](http://blog.csdn.net/topwqp/article/details/8681573)

 

 

 

下面演示通过  @PostConstruct 和 @PreDestory

 

1:定义相关的实现类：

 

 

 

**\[java]** [view plain](http://blog.csdn.net/topwqp/article/details/8681497 "view plain") [copy](http://blog.csdn.net/topwqp/article/details/8681497 "copy")

01. package com.myapp.core.annotation.init;
02. import javax.annotation.PostConstruct;
03. import javax.annotation.PreDestroy;
04. public class PersonService {
05. private String  message;
06. public String getMessage() {
07. return message;
08. }
09. public void setMessage(String message) {
10. this.message = message;
11. }
12. @PostConstruct
13. public void  init(){
14. System.out.println("I'm  init  method  using  @PostConstrut...."+message);
15. }
16. @PreDestroy
17. public void  dostory(){
18. System.out.println("I'm  destory method  using  @PreDestroy....."+message);
19. }
20. }

2：定义相关的配置文件：

 

 

**\[html]** [view plain](http://blog.csdn.net/topwqp/article/details/8681497 "view plain") [copy](http://blog.csdn.net/topwqp/article/details/8681497 "copy")

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xmlns:context="http://www.springframework.org/schema/context"
05. xsi:schemaLocation="http://www.springframework.org/schema/beans
06. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
07. http://www.springframework.org/schema/context
08. http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt;
09. &lt;!-- &lt;context:component-scan  base-package="com.myapp.core.jsr330"/&gt; --&gt;
10. &lt;context:annotation-config /&gt;
11. &lt;bean id="personService" class="com.myapp.core.annotation.init.PersonService"&gt;
12. &lt;property name="message" value="123"&gt;&lt;/property&gt;
13. &lt;/bean&gt;
14. &lt;/beans&gt;

其中&lt;context:annotation-config /&gt;告诉spring 容器采用注解配置：扫描注解配置；

测试类：

 

 

 

**\[java]** [view plain](http://blog.csdn.net/topwqp/article/details/8681497 "view plain") [copy](http://blog.csdn.net/topwqp/article/details/8681497 "copy")

01. package com.myapp.core.annotation.init;
02. import org.springframework.context.ApplicationContext;
03. import org.springframework.context.support.ClassPathXmlApplicationContext;
04. public class MainTest {
05. public static void main(String\[] args) {
06. ApplicationContext  context = new ClassPathXmlApplicationContext("resource/annotation.xml");
07. PersonService   personService  =  (PersonService)context.getBean("personService");
08. personService.dostory();
09. }
10. }

测试结果：

I'm  init  method  using  @PostConstrut....123  
I'm  destory method  using  @PreDestroy.....123

 

 

 

其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor

 

类来告诉Spring容器采用的 常用 注解配置的方式：

 

只需要修改配置文件为：

 

 

 

**\[html]** [view plain](http://blog.csdn.net/topwqp/article/details/8681497 "view plain") [copy](http://blog.csdn.net/topwqp/article/details/8681497 "copy")

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xmlns:context="http://www.springframework.org/schema/context"
05. xsi:schemaLocation="http://www.springframework.org/schema/beans
06. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
07. http://www.springframework.org/schema/context
08. http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt;
09. &lt;!-- &lt;context:component-scan  base-package="com.myapp.core.jsr330"/&gt; --&gt;
10. &lt;!-- &lt;context:annotation-config /&gt; --&gt;
11. &lt;bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /&gt;
12. &lt;bean id="personService" class="com.myapp.core.annotation.init.PersonService"&gt;
13. &lt;property name="message" value="123"&gt;&lt;/property&gt;
14. &lt;/bean&gt;
15. &lt;/beans&gt;

同样可以得到以上测试的输出结果。

 

小伙伴们，我开了一家海淘护肤品淘宝店，搜索店铺“禾子蝶的海淘铺”，正品保证，欢迎进店选购哦。谢谢！