【Spring】bean的生命周期

1.具体的生命周期过程
  • bean对象创建(调用无参构造器)

  • 给bean对象设置属性

  • bean对象初始化之前操作(由bean的后置处理器负责)

  • bean对象初始化(需在配置bean时指定初始化方法)

  • bean对象初始化之后操作(由bean的后置处理器负责)

  • bean对象就绪可以使用

  • bean对象销毁(需在配置bean时指定销毁方法)

  • IOC容器关闭

2.User类:
package com.zh.spring.pojo;

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) {
        //使用setter方法进行赋值
        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;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }

    public void initMethod(){
        //初始化方法
        System.out.println("生命周期3:初始化");
    }
    public void destroyMethod(){
        //销毁方法
        System.out.println("生命周期4:销毁");
    }
}

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

3.对应的bean:
    <bean id="user"  class="com.zh.spring.pojo.User" init-method="initMethod" destroy-method="destroyMethod">
        <property name="id" value="1"></property>
        <property name="username" value="admin"></property>
        <property name="age" value="21"></property>
        <property name="password" value="123456"></property>
    </bean>

<!--    将bean的后置处理器放入IOC容器中-->
    <bean id="myBeanPostProcessor" class="com.zh.spring.process.MyBeanPostProcessor"></bean>

注意:初始化时需要通过bean的init-method="initMethod"属性指定初始化的方法

      IOC容器关闭时销毁,需要bean的destroy-method="destroyMethod"属性指定销毁的方法

4.测试类
public class LifeCycleTest {

    @Test
    public void test(){
        //ConfigurableApplicationContext:是ApplicationContext的子接口,其中扩展了刷新和关闭ioc容器的方法
//        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-lifecycle.xml");
        ConfigurableApplicationContext ioc = new ClassPathXmlApplicationContext("spring-lifecycle.xml");
        User user = ioc.getBean(User.class);
        System.out.println(user);
        //要想关闭ioc容器,没有直接的close方法,要使用ConfigurableApplicationContext
        ioc.close();
    }
}
5.bean的后置处理器

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

public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        //此方法在bean的生命周期初始化之前执行
        System.out.println("MyBeanPostProcessor---->后置处理器postProcessBeforeInitialization");
       return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        //此方法在bean的生命周期初始化之后执行
        System.out.println("MyBeanPostProcessor---->后置处理器postProcessAfterInitialization");
        return bean;
    }
}

运行结果:

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

simpleHan

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

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

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

打赏作者

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

抵扣说明:

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

余额充值