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

【Spring】- Bean的生命周期的init和destroy方法的三种方式

程序员文章站 2022-05-24 18:13:39
...

概述

在Spring中,向容器中注入一个Bean的时候可能期望在Bean的创建或者Bean的销毁的时候“做一些事情”,Spring支持三种方式来让我们实现。

实现方法

准备

HelloPrinter类:
public class HelloPrinter {

	public void print() {
		System.out.println("hello");
	}
	
	public void init() {
		System.out.println("init run...");
	}
	
	public void destroy() {
		System.out.println("destroy run...");
	}
}
期望在HelloPrinter被容器创建的时候执行init方法,销毁的时候执行destroy方法。

Application类:
public class Application {
	
	public static void main(String[] args) {
		ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		HelloPrinter printer = (HelloPrinter) context.getBean("helloPrinter");
		printer.print();
		//shutdown容器
		context.registerShutdownHook();
	}
}
这里必须调用context.registerShutdownHook()方法去关闭容器(web应用中则不用)来达到容器销毁Bean的目的。

1. xml配置指定

通过在xml配置文件依赖注入注入Bean的时候添加init-method和destroy-method属性来指定:

<?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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="helloPrinter" class="io.spring.hello.HelloPrinter" init-method="init" destroy-method="destroy"/>

</beans>
容器在创建和销毁Bean的时候会去执行init-method和destroy-method中指定的方法
运行输出:
init run...
hello
七月 06, 2017 10:34:58 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org[email protected]2e817b38: startup date [Thu Jul 06 10:34:58 CST 2017]; root of context hierarchy
destroy run...

同时xml配置的时候还可以在xml的<beans />标签上指定默认的方法名分别为default-init-method和default-destroy-method属性。指定这两个属性之后如果在注入的Bean的<bean />节点上没有显示的指定init-method和destroy-method的话,容器会在Bean的创建和销毁的时候自动的去寻找相应名称的方法并执行,如果没有对应名字的方法则不执行未找到的方法。

例:
<?xml version="1.0" encoding="UTF-8"?>
<beans default-init-method="init" default-destroy-method="destroy" 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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="helloPrinter" class="io.spring.hello.HelloPrinter" />

</beans>
运行输出:
init run...
hello
七月 06, 2017 10:40:23 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org[email protected]2e817b38: startup date [Thu Jul 06 10:40:22 CST 2017]; root of context hierarchy
destroy run...

2. 实现InitializingBean接口和DisposableBean接口

InitializingBean和DisposableBean接口都只有一个方法
public interface InitializingBean {

	void afterPropertiesSet() throws Exception;

}
public interface DisposableBean {

	void destroy() throws Exception;

}

例:
HelloPrinter:
public class HelloPrinter implements InitializingBean, DisposableBean{

	public void print() {
		System.out.println("hello");
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("InitializingBean init method run..");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("afterPropertiesSet run..");
	}
	
}

xml配置如下:
<?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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="helloPrinter" class="io.spring.hello.HelloPrinter" />

</beans>
只需要简单的注入即可。
运行输出:
afterPropertiesSet run..
hello
七月 06, 2017 10:46:56 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org[email protected]2e817b38: startup date [Thu Jul 06 10:46:56 CST 2017]; root of context hierarchy
InitializingBean init method run..

3. 使用@PostConstruct和@PreDestroy注解

@PostConstruct和@PreDestroy注解效果基本等同于init-method和destroy-method。

例:
HelloPrinter:

public class HelloPrinter {

	public void print() {
		System.out.println("hello");
	}

	@PostConstruct
	public void init() {
		System.out.println("init by annotation run...");
	}
	
	@PreDestroy
	public void destroy() {
		System.out.println("destroy by annotation run...");
	}
	
}

xml配置支持注解方式:
<?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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 注解支持 -->
    <context:annotation-config />

    <bean id="helloPrinter" class="io.spring.hello.HelloPrinter" />

</beans>
运行输出:
init by annotation run...
hello
destroy by annotation run...

三种方式并存时候的顺序:

下面例子:
HelloPrinter:
public class HelloPrinter implements InitializingBean, DisposableBean {

	public void print() {
		System.out.println("hello");
	}
	
	public void initByXml() {
		System.out.println("init by xml run...");
	}
	
	public void destroyByXml() {
		System.out.println("detroy bry xml run...");
	}
	
	@PostConstruct
	public void initByAnnotation() {
		System.out.println("init by annotation run...");
	}
	
	@PreDestroy
	public void destroyByAnnotation() {
		System.out.println("destroy by annotation run...");
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("destroy by DisposableBean");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("init by InitializingBean");
	}
	
}
xml配置:
<?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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 注解支持 -->
    <context:annotation-config />

    <bean id="helloPrinter" class="io.spring.hello.HelloPrinter" init-method="initByXml" destroy-method="destroyByXml" />

</beans>

运行输出:
init by annotation run...
init by InitializingBean
init by xml run...
hello
七月 06, 2017 11:21:51 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org[email protected]2e817b38: startup date [Thu Jul 06 11:21:51 CST 2017]; root of context hierarchy
destroy by annotation run...
destroy by DisposableBean
detroy bry xml run...


顺序:
  1. @PostConstruct标记的方法
  2. InitializingBean中的afterPropertiesSet方法
  3. xml的init-method指定的方法
  4. Bean的行为。。。
  5. @PreDestroy标记的方法
  6. DisposableBean中的destroy方法
  7. xml的destroy-method指定的方法

注意:如果有多种方式指定同一个方法(例如xml配置和注解都指定的同一个方法)那么这个方法只会被运行一次。