Spring之简单例子展示Bean的生命周期

文章详细阐述了Spring框架下Bean的生命周期过程,包括构造器创建对象、依赖注入、初始化前和后的操作、以及销毁方法的调用。通过示例展示了singleton和prototype两种不同作用域对Bean生命周期的影响。同时,文章还介绍了Bean后置处理器BeanPostProcessor的作用,它可以在Bean初始化前后添加额外的操作,并给出了配置和使用示例。
摘要由CSDN通过智能技术生成

①具体的生命周期过程
  • bean对象创建(调用无参构造器)
  • 给bean对象设置属性,依赖注入
  • bean对象初始化之前操作(由bean的后置处理器负责)
  • bean对象初始化(需在配置bean时指定初始化方法)
  • bean对象初始化之后操作(由bean的后置处理器负责)
  • bean对象就绪可以使用
  • bean对象销毁(需在配置bean时指定销毁方法)
  • IOC容器关闭
②修改类User
package com.jxz;

public class User {
    private Integer id;
    private String username;
    private String password;
    private Integer age;
    public User() {
        System.out.println("生命周期:1、使用无参构造器创建对象");
    }
    public User(Integer id, String username, String password, Integer age) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.age = age;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        System.out.println("生命周期:2、依赖注入");
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }

    public void initMethod(){
        System.out.println("生命周期:3、初始化");
    }
    public void destroyMethod(){
        System.out.println("生命周期:5、销毁");
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }
}

注意其中的initMethod()和destroyMethod(),可以通过配置bean指定为初始化和销毁的方法

③配置bean
<!-- 使用init-method属性指定初始化方法 -->
<!-- 使用destroy-method属性指定销毁方法 -->
<bean class="com.jxz.User" scope="singleton" init-method="initMethod" destroy-method="destroyMethod">
    <property name="id" value="1001"></property>
    <property name="username" value="admin"></property>
    <property name="password" value="123456"></property>
    <property name="age" value="23"></property>
</bean>
作用域对生命周期的影响

singleton : 获取IOC容器时执行123, 后续获取直接从4开始,容器关闭调用5

prototype:获取IOC容器不执行123,从容器中获取bean则1, 2, 3, 4 执行,容器关闭不会执行5 , 此作用域对象销毁方法需要用户自己处理。(踩了大坑)

执行 scope=“singleton”

package com.jxz;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LifeCycleTest {
 @Test
 public void testLifeCycle(){
     ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-lifecycle.xml");
//        User user = ioc.getBean(User.class);
//        System.out.println(user);
//        System.out.println("生命周期:4、通过IOC容器获取bean并使用");
//        ioc.close();
 }
}

输出:

生命周期:1、使用无参构造器创建对象
生命周期:2、依赖注入
生命周期:3、初始化

还是上面的代码,scope=“prototype”

输出:

无输出
④测试
package com.jxz;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LifeCycleTest {
    @Test
    public void testLifeCycle(){
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-lifecycle.xml");
        User user = ioc.getBean(User.class);
        System.out.println(user);
        System.out.println("生命周期:4、通过IOC容器获取bean并使用");
        ioc.close();
    }
}

输出:

生命周期:1、使用无参构造器创建对象
生命周期:2、依赖注入
生命周期:3、初始化
User{id=1001, username='admin', password='123456', age=23}
生命周期:4、通过IOC容器获取bean并使用
生命周期:5、销毁
⑤bean的后置处理器

bean的后置处理器会在生命周期的初始化前后添加额外的操作,需要实现BeanPostProcessor接口,且配置到IOC容器中,需要注意的是,容器只能配置一个后置处理器,bean后置处理器不是单独针对某一个bean生效,而是针对IOC容器中所有bean都会执行

创建bean的后置处理器:

package com.jxz.process;

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

public class MyBeanProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("初始化前调用-------------"+ beanName + " = " + bean);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("初始化后调用-------------"+ beanName + " = " + bean);
        return bean;
    }
}

在IOC容器中配置后置处理器:

<bean class="com.jxz.process.MyBeanProcessor"></bean>

输出:

生命周期:1、使用无参构造器创建对象
生命周期:2、依赖注入
初始化前调用-------------com.jxz.User#0 = User{id=1001, username='admin', password='123456', age=23}
生命周期:3、初始化
初始化后调用-------------com.jxz.User#0 = User{id=1001, username='admin', password='123456', age=23}
User{id=1001, username='admin', password='123456', age=23}
生命周期:4、通过IOC容器获取bean并使用
生命周期:5、销毁
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

互联网民工蒋大钊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值