Spring bean初始化和銷燬方法

在Spring框架中,有三種方式可以給bean添加初始化和銷燬方法,分別是實現InitializingBean和DisposableBean接口、xml文檔中配置和使用@PostConstruct和@PreDestroy註解。

Bean代碼如下:

package bea;

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

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

import inter.HelloWorld;

public class Cat implements HelloWorld,InitializingBean,DisposableBean{
	
	private String name;
	private int age;
	public Cat(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Cat(){
		super();
	}
	public String getName() {
		return name;
	}
	@Required
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public void say() {
		// TODO Auto-generated method stub
		System.out.println("name"+this.name+"----"+"hascode"+this.hashCode());
	}
	@Override
	public void destroy() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("接口銷燬");
	}
	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("接口初始化,屬性設置後進行");
	}
	public void init(){
		System.out.println("配置初始化");
	}
	public void des() {
		System.out.println("配置銷燬");
	}
	@PostConstruct
	public void initInter(){
		System.out.println("註解初始");
	}
	@PreDestroy
	public void desInter(){
		System.out.println("註解銷燬");
	}
}
配置文件如下:

<bean id="cat" class="bea.Cat" init-method="init" destroy-method="des">
   						<property name="name" value="tom"></property>
                       <property name="age" value="90"></property>
                       </bean>
測試:

package ser;

import inter.HelloWorld;

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

import bea.Cat;
import bea.SpringHelloWorld;
import bea.Wolf;

public class MyTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext context =
                new ClassPathXmlApplicationContext("beans.xml");     

          Cat cat2 =
                  (Cat) context.getBean("cat");
          cat2.say();

         ((AbstractApplicationContext) context).close();
	}

}
控制檯輸出:

註解初始
接口初始化,屬性設置後進行
配置初始化
@qutowired springHelloWorld
nametom----hascode558863807
六月 20, 2017 8:42:56 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@22ddc2c2: startup date [Tue Jun 20 20:42:55 CST 2017]; root of context hierarchy
註解銷燬
接口銷燬
配置銷燬

注:

1、使用註解,要註冊“CommonAnnotationBeanPostProcessor”,

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

2、使用在xml中配置init-method="init" destroy-method="des",這種方式可以很好的和Spring框架解耦。

3、執行時序:註解>接口>配置


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章