Spring Bean的生命周期配置

Spring Bean的生命周期配置

Spring 初始化bean或销毁bean时,有时需要做一些处理工作,因此Spring可以在创建和销毁的时候调用bean的两个生命周期方法init-methoddestory-method其中销毁只有在单例模式下有效。

Spring Bean 的完整生命周期

  1. instantiate bean对象实例化
  2. populate properties 封装属性
  3. 如果 Bean 实现 BeanNameAware 执行 setBeanName
  4. 如果 Bean 实现 BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext
  5. 如果存在实现类 BeanPostProcess(后处理Bean),执行 postProcessBeforeInitialization
  6. 如果 Bean 实现 InitializingBean 执行 afterPropertiesSet
  7. 调用 <bean init-method=“init”> 指定初始化方法 init
  8. 如果存在类实现 BeanPostProcess,执行 postProcessAfterInitialization
  9. 执行业务处理
  10. 如果 Bean 实现 DisposableBean 执行 destory
  11. 调用 <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();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值