Spring框架——bean的生命周期

Spring 中 bean 的生命周期

bean 生命周期

​ bean 的生命周期大致流程如下,后面会为大家解释每个流程具体的作用

在这里插入图片描述

  1. instantiate bean 对象实例化
  2. populate properties 封装属性
  3. 如果Bean实现 BeanNameAware 执行 setBeanName
  4. 如果 Bean 实现 BeanFactoryAware 执行 setBeanFactory,获取 Spring 容器
  5. 如果存在类实现 BeanPostProcessor(后处理Bean),执行 postProcessBeforeInitialization
  6. 如果 Bean 实现 InitializingBean 执行 afterPropertiesSet
  7. 调用<bean init-method="init">指定初始化方法 init
  8. 如果存在类实现 BeanPostProcessor(处理Bean),执行 postProcessAfterInitialization
  9. 如果 Bean 实现 DisposableBean 执行 destroy
  10. 调用<bean destroy-method="customerDestroy">指定销毁方法 customerDestroy

对象实例化

​ bean 的生命周期从对象的实例化开始,当运行到下面的代码时,就会实例化beans5.xmlbean标签所定义的对象,并执行其构造方法

ApplicationContext context = new ClassPathXmlApplicationContext("beans5.xml");

封装属性

​ 接下来,Spring 会根据bean标签下的property标签来为对象赋值,即依赖注入(封装属性),这时实际执行的是实现类中对应属性的set方法,也就是说,想要通过这个方式为对象封装属性,实现类中必须含有对应属性的set方法

private String name;
private String password;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
    System.out.println("set name");
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
    System.out.println("set password");
}
<bean id="user" class="com.lmh.model.User" init-method="myInit" destroy-method="myDestroy">
    <property name="name" value="lmh"></property>
    <property name="password" value="123"></property>
</bean>

BeanNameAware

​ 如果实现类中实现了BeanNameAware接口,那么接下来对象就会执行重写的setBeanName方法

@Override
public void setBeanName(String s) {
	// s:bean.xml 中的 bean id
	System.out.println("set Bean name: " + s);
}

​ 通过输出我们知道,setBeanName函数的参数sbean标签中的id属性的值

BeanFactoryAware

​ 如果实现类中实现了BeanFactoryAware接口,接下来对象会执行重写的setBeanFactory方法

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    // 工厂生产对象后放进容器中去
    System.out.println("Bean Factory: " + beanFactory);
}

​ 此时是 Bean 工厂生产的对象被放进 Spring 容器中的过程

BeanPostProcessor

BeanPostProcessor是 Spring IOC 容器提供的一种处理器接口,可以理解成一种对所有beans.xml中对象均有效的处理器

​ 使用时我们需要自定义处理器类,实现BeanPostProcessor接口,重写其postProcessBeforeInitializationpostProcessAfterInitialization方法

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

public class MyBeanProcessor implements BeanPostProcessor {
    // 对所有对象有效
    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        // 可用于多个对象的共同事情
        System.out.println("Before: " + s + " : " + o);
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        System.out.println("After: " + s + " : " + o);
        return o;
    }
}

​ 其中o为当前执行处理器的对象,s为 bean 的id 属性值,这里postProcessBeforeInitialization方法在 bean 的生命周期中会在上一步后执行,而postProcessAfterInitialization图中 Bean Is Ready To Use 之前执行

InitializingBean

​ 如果实现类中实现了InitializingBean接口,对象接下来会调用重写的afterPropertiesSet方法

@Override
public void afterPropertiesSet() throws Exception {
    System.out.println("6.Pro is seted...");
}

自定义 Init 方法

​ 随后,如果我们在实现类中自定义了 Init 方法,再bean标签中设置了init-method="init"属性后,自定义的 Init 方法会在这时执行

public void myInit() {
    System.out.println("7.My Init");
}
<bean id="user" class="com.lmh.model.User" init-method="myInit">
</bean>

DisposableBean

​ 以上周期执行完毕后,bean 就进入了可以被使用的状态,当 bean 使用结束后,需要销毁对象时,如果实现类中实现了DisposableBean接口,会执行从写的destroy方法

@Override
public void destroy() throws Exception {
    System.out.println("9.Bean is Destroy");
}

自定义 Destroy 方法

​ 如果自定义了 Destroy 方法,会在这是执行,方式和自定义 Init 方法一致,实现类中定义 Destroy 方法,并在bean标签中设置destroy-method="destroy"属性

public void myDestroy() {
    System.out.println("my Destroy");
}
<bean id="user" class="com.lmh.model.User" destroy-method="myDestroy">
</bean>
发布了72 篇原创文章 · 获赞 89 · 访问量 6537

猜你喜欢

转载自blog.csdn.net/scfor333/article/details/104391365