spring容器在初始化Bean时前和后的操作

时间:2022-07-01 19:49:48

在某些情况下,Spring容器在初始化Bean的时候,希望在初始化bean前和销毁bean前进行一些资源的加载和释放的操作。可以通过一下三种方式完成。

  • Bean的方法加上@PostConstruct和@PreDestroy注解
  • 在xml中定义init-method和destory-method方法
  • Bean实现InitializingBean和DisposableBean接口

@PostConstruct和@PreDestroy注解

JavaBean代码

@Component
public class PersonService {
private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

@PostConstruct
public void init() {
System.out.println("PersonService.class init method ...");
}

@PreDestroy
public void cleanUp() {
System.out.println("PersonService.class cleanUp method ...");
}
}

spring配置文件

<?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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"
>


<!-- spring扫描的路径 -->
<context:component-scan base-package="spring.zhujie" />

</beans>

测试代码和结果

  • 测试代码
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-zhujie.xml");
context.registerShutdownHook();
}
  • 运行结果
PersonService.class init method ...
PersonService.class cleanUp method ...

在XML中定义init-method和destory-method方法

JavaBean代码

public class PersonService {
private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public void init() {
System.out.println("PersonService.class init method ...");
}

public void cleanUp() {
System.out.println("PersonService.class cleanUp method ...");
}
}

spring配置文件

<bean class="spring.zhujie.PersonService" init-method="init" destroy-method="cleanUp"/>

测试代码和结果

  • 测试代码
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-xml.xml");
context.registerShutdownHook();
}
  • 运行结果
PersonService.class init method ...
六月 23, 2017 9:
42:06 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7a94c5e7: startup date [Fri Jun 23 21:42:06 CST 2017]; root of context hierarchy
PersonService.class cleanUp method ...

Bean实现InitializingBean和DisposableBean接口

JavaBean代码

public class PersonService implements InitializingBean, DisposableBean {
private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

@Override
public void afterPropertiesSet() throws Exception {
System.out.println("PersonService.class init method ...");
}

@Override
public void destroy() throws Exception {
System.out.println("PersonService.class cleanUp method ...");
}
}

spring配置文件

<bean id="personService" class="spring.zhujie.PersonService" />

测试代码和结果

  • 测试代码
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-interface.xml");
context.registerShutdownHook();
}
  • 运行结果
PersonService.class init method ...
PersonService.class cleanUp method ...