目录

基本概念

生命周期

Spring Bean的生命周期过程有哪些?

1. 实例化

2. 属性赋值

3. 初始化

4. 使用

5. 销毁

实例

目录结构

文件内容

MyBeanPostProcess文件

PersonBean文件

 spring-config.xml文件

Test2文件

 实例运行结果

如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏哦。


我们了解一个对象,作用域和生命周期是。上一篇我们聊到了Spring Bean的作用域,有兴趣的小伙伴可以去看看:JAVA-Spring Bean的作用域

基本概念

生命周期

百科中对自然界生命周期的解释:生命周期

JAVA中对象的生命周期:从对象的创建到销毁的过程。

Spring项目中,生命周期指的是一个bean对象从出生到死亡的整个过程。我们理解了Spring项目的生命周期的之后,可以利用 Bean 在其存活期间的指定时刻完成一些相关操作。

Spring Bean的生命周期过程有哪些?

javabean有几种生存期各自的特点是什么 javabean生命周期包括_spring

1. 实例化

在 Spring 容器中,Bean 的实例化是通过构造函数或工厂方法来完成的。当容器启动时,它会根据配置文件或注解扫描来创建 Bean 的实例。

2. 属性赋值

在实例化后,Spring 容器会通过依赖注入的方式来设置 Bean 的属性值。这可以通过构造函数、Setter 方法或字段注入来完成。

3. 初始化

初始化是 Bean 生命周期的一个重要阶段。在初始化阶段,Spring 容器会调用特定的回调方法来执行一些初始化操作。有两种常见的方式来定义初始化方法:

  • @PostConstruct 注解:通过在方法上添加 @PostConstruct 注解,可以指定该方法在 Bean 初始化阶段被调用。
  • 实现 InitializingBean 接口:实现 InitializingBean 接口,并重写其中的 afterPropertiesSet() 方法,该方法将在 Bean 初始化阶段被调用。

4. 使用

在初始化完成后,Bean 可以被正常使用,它可以处理业务逻辑、响应请求等。

5. 销毁

当 Spring 容器关闭时,或者通过编程方式销毁 Bean 时,会触发 Bean 的销毁阶段。在销毁阶段,Spring 容器会调用特定的回调方法来执行一些清理操作。有两种常见的方式来定义销毁方法:

  • @PreDestroy 注解:通过在方法上添加 @PreDestroy 注解,可以指定该方法在 Bean 销毁阶段被调用。
  • 实现 DisposableBean 接口:实现 DisposableBean 接口,并重写其中的 destroy() 方法,该方法将在 Bean 销毁阶段被调用。

实例

目录结构

javabean有几种生存期各自的特点是什么 javabean生命周期包括_spring_02

文件内容

MyBeanPostProcess文件

package com.tfjy.test2;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.tfjy.test2
 * @Author: aqiu
 * @Description: 自定义的后处理器
 * @CreateTime: 2023-01-29 20:54
 * @Version: 1.0
 */
public class MyBeanPostProcessor implements BeanPostProcessor {

    //前置处理器,bean对象初始化前的动作
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("5.BeanPostProcessor.postProcessBeforeInitialization方法:bean对象初始化之前的动作");
        return bean;
    }


    //后置处理器,创建bean对象结束后走这里
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("8.BeanPostProcessor#postProcessAfterInitialization方法:bean对象初始化之后的动作");
        return bean;
    }
}

PersonBean文件

package com.tfjy.test2;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.tfjy.test2
 * @Author: aqiu
 * @Description: Bean的生命周期
 * @CreateTime: 2023-01-29 20:53
 * @Version: 1.0
 */
public class PersonBean implements InitializingBean, BeanFactoryAware, BeanNameAware, DisposableBean {

    /**
     * 身份证号
     */
    private Integer no;

    /**
     * 姓名
     */
    private String name;

    //最先走的方法就是调用构造函数,创建具体对象
    public PersonBean() {
        System.out.println("1.调用构造方法:我出生了!");
    }


    public Integer getNo() {
        return no;
    }

    public void setNo(Integer no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("2.设置属性:我的名字叫" + name);
    }

    //BeanNameAware接口,通过这个接口设置bean的id
    @Override
    public void setBeanName(String s) {
        System.out.println("3.调用BeanNameAware#setBeanName方法:我的bean的名字叫做" + s);
    }


    //    BeanFactoryAware 用于注入BeanFactory对象,能访问创建对象的BeanFactory。
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("4.调用BeanFactoryAware#setBeanFactory方法:注入BeanFactory对象");
    }

    //    InitializingBean 接口
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("6.InitializingBean#afterPropertiesSet方法:开始初始化");
    }

    //在创建bean的时候,会走这个对应的初始化方法
    public void init() {
        System.out.println("7.自定义init方法:自定义初始化");
    }


    //销毁一个bean对象
    @Override
    public void destroy() throws Exception {
        System.out.println("9.DisposableBean#destroy方法:开始销毁");
    }

    public void destroyMethod() {
        System.out.println("10.自定义destroy方法:自定义销毁");
    }

    public void work() {
        System.out.println("Bean使用中~~~~");
    }

}

 spring-config.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 name="myBeanPostProcessor" class="com.tfjy.test2.MyBeanPostProcessor" />
    <bean name="personBean" class="com.tfjy.test2.PersonBean"
          init-method="init" destroy-method="destroyMethod">
        <property name="no" value= "666666"/>
        <property name="name" value="妞妞" />
    </bean>

</beans>

Test2文件

import com.tfjy.test2.PersonBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @BelongsProject: demo
 * @Author: aqiu
 * @Description: Bean的生命周期测试类
 * @CreateTime: 2023-01-29 20:55
 * @Version: 1.0
 */
public class Test2 {

    @Test
    public void  test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        PersonBean personBean = (PersonBean) context.getBean("personBean");
        //开始使用
        personBean.work();
        //开始销毁
        ((ClassPathXmlApplicationContext) context).close();
    }

}

 实例运行结果

javabean有几种生存期各自的特点是什么 javabean生命周期包括_生命周期_03