Spring Bean的生命周期配置
Spring 初始化bean或销毁bean时,有时需要做一些处理工作,因此Spring可以在创建和销毁的时候调用bean的两个生命周期方法
init-method
和destory-method
其中销毁只有在单例模式下有效。
Spring Bean 的完整生命周期
- instantiate bean对象实例化
- populate properties 封装属性
- 如果 Bean 实现 BeanNameAware 执行 setBeanName
- 如果 Bean 实现 BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext
- 如果存在实现类 BeanPostProcess(后处理Bean),执行 postProcessBeforeInitialization
- 如果 Bean 实现 InitializingBean 执行 afterPropertiesSet
- 调用 <bean init-method=“init”> 指定初始化方法 init
- 如果存在类实现 BeanPostProcess,执行 postProcessAfterInitialization
- 执行业务处理
- 如果 Bean 实现 DisposableBean 执行 destory
- 调用 <bean destory-method=“destory”> 指定销毁方法 destory
下面通过代码演示完整周期
项目路径
spring_lifeCycle
- src
- - main
- - - java
- - - - com.zhangxin9727.lifeCycle
- - - - - Man.java
- - - - - MyBeanPostProcess.java
- - - resources
- - - - applicationContext.xml
- - test
- - - java
- - - - MyTest.java
- pom.xml
详细代码
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<groupId>com.zhangxin9727</groupId>
<artifactId>spring_lifeCycle</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Man.java
package com.zhangxin9727.lifeCycle;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class Man implements BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean {
private String name;
public Man() {
System.out.println("第一步:对象实例化···");
}
public void setName(String name) {
System.out.println("第二步:设置属性···");
this.name = name;
}
@Override
public void setBeanName(String s) {
System.out.println("第三步:设置bean的名称···");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("第四步:了解工厂的信息···");
}
@Override
public void afterPropertiesSet() {
System.out.println("第六步:属性设置后···");
}
public void setup() {
System.out.println("第七步:对象初始化方法···");
}
public void work() {
System.out.println("第九步:业务方法···");
}
@Override
public void destroy() {
System.out.println("第十步:Spring销毁方法");
}
public void teardown() {
System.out.println("第十一步:对象销毁方法···");
}
}
MyBeanPostProcessor.java
package com.zhangxin9727.lifeCycle;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("第五步:初始化前方法···");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("第八步:初始化后方法···");
return bean;
}
}
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="man" class="com.zhangxin9727.lifeCycle.Man" scope="singleton" init-method="setup" destroy-method="teardown">
<property name="name" value="Alice"/>
</bean>
<bean class="com.zhangxin9727.lifeCycle.MyBeanPostProcessor"/>
</beans>
MyTest.java
import com.zhangxin9727.lifeCycle.Man;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Man man = (Man) applicationContext.getBean("man");
man.work();
applicationContext.close();
}
}