JAVA-Spring Bean的生命周期

简介: JAVA-Spring Bean的生命周期

目录

基本概念

生命周期

Spring Bean的生命周期有哪些?

实例

目录结构

文件内容
MyBeanPostProcess文件

PersonBean文件

spring-config.xml文件

Test2文件

实例运行结果

如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏哦。
我们了解一个对象,作用域和生命周期是。上一篇我们聊到了Spring Bean的作用域,有兴趣的小伙伴可以去看看:JAVA-Spring Bean的作用域

基本概念

生命周期

百科中对自然界生命周期的解释:生命周期
JAVA中对象的生命周期:从对象的创建到销毁的过程。


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


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

实例

目录结构文件内容

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();
    }
}

实例运行结果

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

相关文章
|
2天前
|
安全 Java Spring
Spring框架中的单例Bean是线程安全的吗?
Spring框架中的单例Bean是线程安全的吗?
10 1
|
2天前
|
存储 Java
【Java开发指南 | 第七篇】静态变量生命周期、初始化时机及静态变量相关性质
【Java开发指南 | 第七篇】静态变量生命周期、初始化时机及静态变量相关性质
17 4
|
2天前
|
XML 前端开发 Java
【JavaEE】深入了解Spring中Bean的可见范围(作用域)以及前世今生(生命周期)
【JavaEE】深入了解Spring中Bean的可见范围(作用域)以及前世今生(生命周期)
5 0
|
2天前
|
存储 缓存 Java
【JavaEE】Spring中注解的方式去获取Bean对象
【JavaEE】Spring中注解的方式去获取Bean对象
3 0
|
2天前
|
存储 Java 对象存储
【JavaEE】Spring中注解的方式去存储Bean对象
【JavaEE】Spring中注解的方式去存储Bean对象
7 0
|
2天前
|
存储 Java 对象存储
【JavaEE】DI与DL的介绍-Spring项目的创建-Bean对象的存储与获取
【JavaEE】DI与DL的介绍-Spring项目的创建-Bean对象的存储与获取
9 0
|
2天前
|
消息中间件 安全 Java
在Spring Bean中,如何通过Java配置类定义Bean?
【4月更文挑战第30天】在Spring Bean中,如何通过Java配置类定义Bean?
21 1
|
2天前
|
XML Java 数据格式
Spring Bean
【4月更文挑战第30天】Spring Bean
17 0
|
2天前
|
前端开发 Java 数据格式
【Spring系列笔记】定义Bean的方式
在Spring Boot应用程序中,定义Bean是非常常见的操作,它是构建应用程序的基础。Spring Boot提供了多种方式来定义Bean,每种方式都有其适用的场景和优势。
32 2
|
2天前
|
XML Java 数据格式
谈谈 Spring 中 Bean 的生命周期?
谈谈 Spring 中 Bean 的生命周期?
20 1