码迷,mamicode.com
首页 > 编程语言 > 详细

Spring注解@PostConstruct与@PreDestroy

时间:2015-11-23 12:58:35      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

关于在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.springframework.context.support.ClassPathXmlApplicationContext@205756: startup date [Sat Mar 16 17:06:30 CST 2013]; root of context hierarchy
I‘m  init  method  using implements InitializingBean interface....123

 

Spring注解@PostConstruct与@PreDestroy

标签:

原文地址:http://www.cnblogs.com/hwaggLee/p/4987867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!