欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring bean初始化和销毁方法

程序员文章站 2022-05-21 23:02:14
...

在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[email protected]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、执行时序:注解>接口>配置