spring初始化、销毁容器时指定执行的方法

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

  1. 通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
  2. 通过 在xml中定义init-method 和  destory-method方法
  3. 通过bean实现InitializingBean和 DisposableBean接口

 

1@PostConstruct 和 @PreDestroy 方法

1)实现类:

public classPersonService { 

    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)配置:

<?xmlversion="1.0" encoding="UTF-8"?> 

<beansxmlns="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:annotation-config/> 

 

<beanid="personService"class="com.myapp.core.annotation.init.PersonService"> 

  <property name="message"value="123"></property> 

</bean> 

 

</beans>

3)测试:

public static voidmain(String[] args) { 

        ApplicationContext  context = newClassPathXmlApplicationContext("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

 

2在xml中定义init-method 和  destory-method方法

1)实现类:

public classPersonService  { 

    private String  message; 

 

    public String getMessage() { 

        return message; 

    } 

    public void setMessage(String message){ 

        this.message = message; 

    } 

 

    public void init(){ 

       System.out.println("init"); 

    } 

   

    public void cleanUp(){ 

       System.out.println("cleanUp"); 

    } 

}

2)配置:

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

3)测试:

public static voidmain(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

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

 

3实现InitializingBean和 DisposableBean接口

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

public classPersonService  implementsInitializingBean,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 implementsInitializingBean interface...."+message); 

         

    } 

 

    @Override 

    public void afterPropertiesSet() throwsException { 

        // TODO Auto-generated method stub 

        System.out.println("I'm  init method  using implementsDisposableBean interface...."+message);  

    } 

}

2)配置:

<?xmlversion="1.0" encoding="UTF-8"?> 

<beansxmlns="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"> 

 

<beanid="personService"class="com.myapp.core.annotation.init.PersonService"> 

          <property name="message"value="123"></property> 

</bean> 

</beans>

3)测试:

public static voidmain(String[] args) { 

         

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

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

         

        context.registerShutdownHook(); 

}

输出:

I'm  init method  using implementsDisposableBean interface....123 

三月 16, 2013 5:06:34下午 org.springframework.context.support.AbstractApplicationContext doClose 

INFO: Closingorg.springframework.context.support.ClassPathXmlApplicationContext@205756:startup date [Sat Mar 16 17:06:30 CST 2013]; root of context hierarchy 

I'm  init method  using implementsInitializingBean interface....123  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值