Spring注解@PostConstruct与@PreDestroy

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

第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

第二种是:通过 在xml中定义init-method 和  destory-method方法

第三种是: 通过bean实现InitializingBean和 DisposableBean接口

下面演示通过  @PostConstruct 和 @PreDestory

第一种:

1:定义相关的实现类:

package com.myapp.core.annotation.init;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class PersonService {

    private String  message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @PostConstruct
    public void  init(){
        System.out.println("I‘m  init  method  using  @PostConstrut...."+message);
    }

    @PreDestroy
    public void  dostory(){
        System.out.println("I‘m  destory method  using  @PreDestroy....."+message);
    }

}

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

<?xml version="1.0" encoding="UTF-8"?>
<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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<!-- <context:component-scan  base-package="com.myapp.core.jsr330"/> -->

<context:annotation-config />

<bean id="personService" class="com.myapp.core.annotation.init.PersonService">
  <property name="message" value="123"></property>
</bean>

</beans>

其中<context:annotation-config />告诉spring 容器采用注解配置:扫描注解配置;

测试类:

package com.myapp.core.annotation.init;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {

    public static void main(String[] args) {

        ApplicationContext  context = new ClassPathXmlApplicationContext("resource/annotation.xml");

        PersonService   personService  =  (PersonService)context.getBean("personService");

        personService.dostory();
    }

}

测试结果:

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

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

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

只需要修改配置文件为:

<?xml version="1.0" encoding="UTF-8"?>
<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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<!-- <context:component-scan  base-package="com.myapp.core.jsr330"/> -->

<!-- <context:annotation-config /> -->

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean id="personService" class="com.myapp.core.annotation.init.PersonService">
          <property name="message" value="123"></property>
</bean>

</beans>

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

----------------------------------------------------------

第二种:

1:在xml中配置 init-method和 destory-method方法

只是定义spring 容器在初始化bean 和容器销毁之前的所做的操作基于xml的配置只是一种方式:

直接上xml中配置文件:

   <bean id="personService" class="com.myapp.core.beanscope.PersonService" scope="singleton"  init-method="init"  destroy-method="cleanUp">

   </bean>

定义PersonService类:

package com.myapp.core.beanscope;

public class PersonService  {
   private String  message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void init(){
        System.out.println("init");
    }
    //  how  validate the  destory method is  a question
    public void  cleanUp(){
        System.out.println("cleanUp");
    }
}

相应的测试类:

package com.myapp.core.beanscope;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {
  public static void main(String[] args) {

      AbstractApplicationContext  context =new  ClassPathXmlApplicationContext("SpringBeans.xml");

    PersonService  person = (PersonService)context.getBean("personService");

    person.setMessage("hello  spring");

    PersonService  person_new = (PersonService)context.getBean("personService");

    System.out.println(person.getMessage());
    System.out.println(person_new.getMessage());
    context.registerShutdownHook();

}
} 

测试结果:

init
hello  spring
hello  spring
cleanUp

可以看出 init 方法和 clean up方法都已经执行了。

context.registerShutdownHook(); 是一个钩子方法,当jvm关闭退出的时候会调用这个钩子方法,在设计模式之 模板模式中 通过在抽象类中定义这样的钩子方法由实现类进行实现,这里的实现类是AbstractApplicationContext,这是spring 容器优雅关闭的方法。 

----------------------------------------------------------

第三种:

1:定义相应类实现InitializingBean ,DisposableBean 接口

package com.myapp.core.annotation.init;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class PersonService  implements InitializingBean,DisposableBean{

    private String  message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public void destroy() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("I‘m  init  method  using implements InitializingBean interface...."+message);

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("I‘m  init  method  using implements DisposableBean interface...."+message);

    }

}

2:定义相应的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<!-- <context:component-scan  base-package="com.myapp.core.jsr330"/> -->

<!-- <context:annotation-config /> -->

<!-- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean id="personService" class="com.myapp.core.annotation.init.PersonService">
          <property name="message" value="123"></property>
</bean>
 -->

<bean id="personService" class="com.myapp.core.annotation.init.PersonService">
          <property name="message" value="123"></property>
</bean>

</beans>

3:测试类:

package com.myapp.core.annotation.init;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {

    public static void main(String[] args) {

        AbstractApplicationContext  context = new ClassPathXmlApplicationContext("resource/annotation.xml");

        PersonService   personService  =  (PersonService)context.getBean("personService");

             context.registerShutdownHook();
    }

}

4:输出测试结果:

I‘m  init  method  using implements DisposableBean interface....123
三月 16, 2013 5:06:34 下午 org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org[email protected]205756: startup date [Sat Mar 16 17:06:30 CST 2013]; root of context hierarchy
I‘m  init  method  using implements InitializingBean interface....123
时间: 2024-08-06 07:56:31

Spring注解@PostConstruct与@PreDestroy的相关文章

注解@PostConstruct与@PreDestroy讲解及实例

从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注解(Annotion):@PostConstruct和@PreDestroy.这两个注解被用来修饰一个非静态的void()方法.写法有如下两种方式: @PostConstruct Public void someMethod() {} 或者 public @PostConstruct void someMethod(){} 被@PostConstruct修饰的方法会在服务器加载Servle的时候运行,并且只会被

Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解

原创整理不易,转载请注明出处:Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解 代码下载地址:http://www.zuidaima.com/share/1772661373422592.htm 本文介绍了使用Spring注解注入属性的方法.使用注解以前,注入属性通过类以及配置文件来实现.现在,注入属性可以通过引入@Autowired注解,或者@Resource,@Qualifi

Spring学习(11)---JSR-250标准注解之 @Resource、@PostConstruct、@PreDestroy

1)@Resource(JSR-250标准注解,推荐使用它来代替Spring专有的@Autowired注解) Spring 不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了.@Resource有两个属性是比较重要的,分别

Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的

本文介绍了使用spring注解注入属性的方法. 使用注解以前,注入属性通过类以及配置文件来实现.现在,注入属性可以通过引入@Autowired注解,或者@Resource,@Qualifier,@PostConstruct,@PreDestroy等注解来实现. 使用注解以前我们是怎样注入属性的 类的实现: 1. public class UserManagerImpl implements UserManager { 2. private UserDao userDao; 3. public v

通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二种是:通过 在xml中定义init-method 和  destory-method方法 第三种是:通过bean实现InitializingBean和 DisposableBean接口 下面演示通过  @PostConstruct 和 @PreDestory 1:定义相关的实现类: package

8 -- 深入使用Spring -- 2...4 使用@PostConstruct和@PreDestroy定制生命周期行为

8.2.4 使用@PostConstruct和@PreDestroy定制生命周期行为 @PostConstruct 和 @PreDestroy 同样位于javax.annotation 包下,也是来自Java EE 规范的两个Annotation,Spring直接借鉴了它们,用于定制Spring容器中Bean的生命周期行为. @PostConstruct : 类似于<bean.../>元素中init-method属性的功能.用于修饰方法使之成为Bean的初始化方法: @PreDestroy :

Java开发之@PostConstruct和@PreDestroy注解

从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreDestroy.这两个注解被用来修饰一个非静态的void()方法.而且这个方法不能有抛出异常声明. 使用方式,例如: 1 @PostConstruct //方式1 2 public void someMethod(){ 3 ... 4 } 5 6 public @PostConstruct void someMethod(){ //方式2 7 ...

Spring还使用基于 JSR-250 注释,它包括 @PostConstruct, @PreDestroy 和 @Resource 注释

@PostConstruct 和 @PreDestroy 注释: 你可以使用 @PostConstruct 注释作为初始化回调函数的一个替代,@PreDestroy 注释作为销毁回调函数的一个替代,其解释如下示例所示. java public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public String getMe

Spring 注解详解03

@Controller @Service @Autowired @RequestMapping @RequestParam @ModelAttribute @Cacheable @CacheFlush @resource @PostConstruct @PreDestroy @repository @component (不推荐使用) @scope @SessionAttributes @InitBinder @Required @qualifier // @Controller 例如 @Con