Spring中bean的生命周期

创建时机

scope=“singleton” 单例的bean 是在spring容器启动时创建
scope=“prototype” 原型bean 在获取bean时才会创建

赋值的时机

对象创建完成后做赋值的工作
初始化 – 对象赋值操作完成后 资源加载

销毁

spring容器关闭时 资源释放 只会帮我们销毁单例的bean 原型bean不归他管
bean的作用域 – 默认singleton 单例 全局只会创建一个当前类的对象
scope=“prototype” 原型bean 在获取bean时才会创建

配置文件

<?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">

<!--
    spring默认是单例的bean 但是可以调整作用域
    scope="singleton" 单例的
    scope="prototype" 原型的bean 每获取一次bean 就创建一个对象

    init-method="" 指定类中的初始化方法
    destroy-method="" 指定类中的销毁方法  只会调用所有单例的bean
    以上两个方法 在指定完成后 会被自动调用
    lazy-init="true" 懒加载  设置单例的bean在spring容器这种加载配置文件时 不创建对象 在使用getBean获取对象时再创建对应key的bean

 -->
    <bean id="student" class="cn.kgc.spring.entity.Student" init-method="init" destroy-method="destroy2" lazy-init="true">
        <property name="username" value="张三"></property>
        <property name="password" value="123"></property>
    </bean>

</beans>
文件一旦启动,就会去创建对象
        // 文件一旦启动,就会去创建对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
在单例模式下,两次getBean容器智慧创建一个对象
        // 只会创建一个对象
        Student student1 = (Student)context.getBean("student");
        Student student2 = (Student)context.getBean("student");
        System.out.println(student1==student2);

控制台输出

Student()无参构造被执行!
setUserName()
setPassword()
interface afterPropertiesSet()
true
在ProtoType模式下,每一次getBean都会创建一次对象
    <bean id="student" class="cn.kgc.spring.entity.Student" scope="prototype">
        <property name="username" value="张三"></property>
        <property name="password" value="123"></property>
    </bean>
        // 文件一旦启动,就会去创建对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        Student student1 = (Student)context.getBean("student");
        Student student2 = (Student)context.getBean("student");
        System.out.println(student1==student2); 

控制台输出

Student()无参构造被执行!
setUserName()
setPassword()
interface afterPropertiesSet()
Student()无参构造被执行!
setUserName()
setPassword()
interface afterPropertiesSet()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 对于SpringBean生命周期Spring在容器有5个生命周期阶段,它们分别是:实例化、属性设置、初始化、销毁以及单例实例化,每个阶段的bean都会触发一个对应的回调事件,让开发者能够在每个阶段做一些额外的处理。 ### 回答2: SpringBean 生命周期包括以下几个阶段: 1. Bean 实例化:Spring 在启动会通过反射机制实例化 Bean。这是 Bean 生命周期的开始阶段。 2. Bean 属性设置:Spring 在实例化 Bean 后,会根据配置文件或注解等方式,将属性值注入到 Bean 。 3. Bean 初始化前置处理:如果 Bean 实现了 InitializingBean 接口,那么 Spring 会在属性设置完成后调用 Bean 的 afterPropertiesSet() 方法进行初始化前的处理。 4. Bean 初始化后置处理:如果 Bean 配置了 init-method 方法,或者在配置文件通过 init-method 属性指定了初始化方法,那么 Spring 会在初始化前置处理完成后调用该方法。 5. Bean 使用阶段:在初始化完成后,Bean 就可以被应用程序使用了。 6. Bean 销毁前置处理:如果 Bean 实现了 DisposableBean 接口,那么在关闭应用程序或手动销毁 Bean Spring 会先调用 Bean 的 destroy() 方法进行销毁前的处理。 7. Bean 销毁后置处理:如果 Bean 配置了 destroy-method 方法,或者在配置文件通过 destroy-method 属性指定了销毁方法,那么 Spring 会在销毁前置处理完成后调用该方法。 在整个 Bean 生命周期,开发人员可以通过实现 InitializingBean 和 DisposableBean 接口,或者在配置文件指定 init-method 和 destroy-method 方法,来自定义 Bean 的初始化和销毁过程,并在这些过程进行一些特定的操作。 ### 回答3: SpringBean生命周期可以分为以下阶段: 1. 实例化:Spring通过Bean定义创建Bean的实例。这可以通过构造函数实例化,或者通过工厂方法来实现。 2. 属性赋值:SpringBean的属性值注入到Bean的实例。这可以通过依赖注入(DI)方式进行,也可以通过配置文件或注解来实现。 3. 初始化:在Bean实例化和属性赋值之后,Spring会调用Bean的初始化方法。这可以通过实现InitializingBean接口的afterPropertiesSet()方法,或者使用@PostConstruct注解来实现。 4. 使用:在初始化完成之后,Bean可以被使用,执行业务逻辑。 5. 销毁:当Bean不再需要Spring会调用Bean的销毁方法。这可以通过实现DisposableBean接口的destroy()方法,或者使用@PreDestroy注解来实现。 需要注意的是,在Bean的生命周期,可以通过配置文件或注解来控制Bean的创建和销毁方式。 总的来说,SpringBean生命周期包括实例化、属性赋值、初始化、使用和销毁这几个阶段。通过控制Bean的生命周期,我们可以在合适的机执行一些特定的操作,如初始化资源、释放资源等。这样可以有效地管理Bean的生命周期,提高系统的性能和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值