Spring學習(十二)-----Spring @PostConstruct和@PreDestroy實例


概要

實現 初始化方法和銷毀方法3種方式:
  1. 實現標識接口 InitializingBean,DisposableBean(不推薦使用,耦合性太高)
  2. 設置bean屬性 Init-method destroy-method
  3. 使用注釋配置后,調用@PostConstruct和@PreDestroy注解

注:@PostConstruct和@PreDestroy 標注不屬於 Spring,它是在J2EE庫- common-annotations.jar。

@PostConstruct 和 @PreDestroy

一個 CustomerService Bean使用 @PostConstruct 和 @PreDestroy 注釋
package com.yiibai.customer.services;

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");
    }
    
}

 

默認情況下,Spring不會意識到@PostConstruct和@PreDestroy注解。要啟用它,要么注冊“CommonAnnotationBeanPostProcessor”,要么在bean配置文件的<context:annotation-config />‘ 指定,

1. CommonAnnotationBeanPostProcessor

<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.yiibai.customer.services.CustomerService">
        <property name="message" value="i'm property message" />
    </bean>
        
</beans>

 

2. <context:annotation-config />

<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.yiibai.customer.services.CustomerService">
        <property name="message" value="i'm property message" />
    </bean>
        
</beans>

 

執行結果

package com.yiibai.common;

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

import com.yiibai.customer.services.CustomerService;

public class App 
{
    public static void main( String[] args )
    {
        ConfigurableApplicationContext context = 
          new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
    
        CustomerService cust = (CustomerService)context.getBean("customerService");
        
        System.out.println(cust);
        
        context.close();
    }
}

輸出結果

Init method after properties are set : im property message
com.yiibai.customer.services.CustomerService@47393f
...
INFO: Destroying singletons in org.springframework.beans.factory.
support.DefaultListableBeanFactory@77158a: 
defining beans [customerService]; root of factory hierarchy
Spring Container is destroy! Customer clean up

 

initIt()方法(@PostConstruct)被調用時,消息屬性設置后 cleanUp() 方法(@PreDestroy)是在context.close()執行后被調用;

下載源代碼 –  http://pan.baidu.com/s/1qX2W6xI


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM