【Spring学习】Bean生命周期

       我理解的Bean生命周期包括两个方面:

  • Bean何时创建,何时销毁
  • Bean从创建到销毁的执行流程

一、Bean创建与销毁

       Bean的创建时机主要由几个配置项共同来决定,包括:

  • scope属性,决定是Bean是单例模式(singleton)还是多例模式(prototype),默认为单例singleton;
  • lazy-init属性,只对单例模式有效,决定是否延时加载,默认为false,表示在容器初始化时,就会生成单例;
  • RequestMapping属性,这个注解MVC中才有,当有该属性时,lazy-init属性会失效(其实不是简单的冲突,而是RequestMapping会在另外的逻辑处理中生成单例);

1.scope属性

       主要有两个枚举,singleton与prototype,默认值为singleton。

1.1配置scope,如果不配置,默认值都为singleton
  • xml配置
<bean id="loginService" class="com.springapp.mvc.service.impl.LoginServiceImpl" scope="prototype"/>
  • 注解配置,直接使用@Scope注解
@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)
1.2 枚举含义
  • singleton,表示单例模式,这个很好理解,表示在容器中,只存在该Bean的一个实例,每次请求Bean时,返回的都是同一个。例:
//例1.2.1
@Component
public class Person {
    public Person(){
        System.out.println("Person 初始化...");
    }
    public static void main(String args[]){
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("mvc-dispatcher-servlet.xml");
        System.out.println("容器初始化完成");
        Person person1 = (Person) beanFactory.getBean("person");
        Person person2 = (Person) beanFactory.getBean("person");
        System.out.println(person1.equals(person2));//同一个实例,打印true
    }
}

输出:
Person 初始化...
容器初始化完成
true
  • prototype,表示每次请求Bean时,都会新建一个Bean实例,例:
//例1.2.1
//上例中,在类前加一个注解:
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)

输出:
容器初始化完成
Person 初始化...
Person 初始化...
false

2.lazy-init属性

       一个布尔型的枚举,就两个值true与false,默认值为true。这个配置只在scope=singleton时有效

2.1 配置lazy-init,默认值为true
  • xml中配置
<bean id="loginService" class="com.springapp.mvc.service.impl.LoginServiceImpl" lazy-init="true"/>
  • 注解配置,使用@Lazy注解
//在类前使用
@Lazy
2.2 枚举含义
  • lazy-init=false,表示容器在初始化时,就会创建单例Bean,这个是默认配置,所以例1.2.1中,先打印“Person 初始化…”,再打印“容器初始化完成”
  • lazy-init=true,在初次getBean时,才会去创建Bean,例
//例2.2.1
//在Person类前添加注解@Lazy

输出:
容器初始化完成
Person 初始化...
true

3.配置嵌套

       主要指singleton与prototype两种不同Bean的相互引用,更具体的来说,其实是singleton对prototype的引用。因为相互引用一共就四种方式,有三种结果非常明确,即:

  • prototype或singleton引用singleton,因为singleton在容器中只会有一个实例,所以结果肯定都是引用同一个类;
  • prototype引用prototype,重新创建的实例再次注入新的依赖,肯定还是会重新创建的。

       所以,只剩singleton对prototype的引用,一开始我就理解错误,以为prototype每次都会变,如下实例:

//例3.1

@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class Apple {
    public Apple(){
        System.out.println("Apple 初始化...");
    }
}

@Component
public class Person {
    @Resource
    Apple apple;

    public Person(){
        System.out.println("Person 初始化...");
    }
    public void eat(){
        System.out.println("Apple hashcode="+apple.hashCode());
    }
    public static void main(String args[]){
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("mvc-dispatcher-servlet.xml");
        System.out.println("容器初始化完成");
        Person person1 = (Person) beanFactory.getBean("person");
        Person person2 = (Person) beanFactory.getBean("person");
        person1.eat();
        person2.eat();
    }
}

输出:
Person 初始化...
Apple 初始化...
容器初始化完成
Apple hashcode=10745331
Apple hashcode=10745331

       本来以为,每次获取Person的Bean时,都会重新注入Apple,结果证明注入只会在Bean创建时进行一次,后续getBean时,发现单例Person存在,直接就会返回该实例,不会重新进行注入操作。
       但是如果有这种需求,就是需要每次都重新创建新的Apple,如何实现呢?在网上查询了一下资料,需要使用lookup-method属性配置。

4 lookup-method属性

       这个属性用于singleton的Bean中获取prototype的Bean,实现每次调用都重新创建新的Bean。

4.1 原理

       用户自己指定一个Bean注入时的方法,然后使用Bean时调用这个方法获取。Spring容器会自动用cglib替换方法体,动态调用getBean获取Bean并返回。看看源码:

//1.解析lookup-method属性并注册
//org.springframework.beans.factory.xml.BeanDefinitionParserDelegate#parseLookupOverrideSubElements
/**
* Parse lookup-override sub-elements of the given bean element.
*/
public void parseLookupOverrideSubElements(Element beanEle, MethodOverrides overrides) {
    NodeList nl = beanEle.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (isCandidateElement(node) && nodeNameEquals(node, LOOKUP_METHOD_ELEMENT)) {
            Element ele = (Element) node;
            String methodName = ele.getAttribute(NAME_ATTRIBUTE);
            String beanRef = ele.getAttribute(BEAN_ELEMENT);
            //新建lookup重载对象
            LookupOverride override = new LookupOverride(methodName, beanRef);
            override.setSource(extractSource(ele));
            //注册到Bean定义中
            overrides.addOverride(override);
        }
    }
}

//2.调用lookup-method方法时,调用cglib替换后的代码,再去调用getBean,具体方法没去研究,有时间看看
//org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.LookupOverrideMethodInterceptor#intercept
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable {
    // Cast is safe, as CallbackFilter filters are used selectively.
    LookupOverride lo = (LookupOverride) getBeanDefinition().getMethodOverrides().getOverride(method);
    Object[] argsToUse = (args.length > 0 ? args : null);  // if no-arg, don't insist on args at all
    if (StringUtils.hasText(lo.getBeanName())) {
        //有beanName,优先使用beanName匹配
        return this.owner.getBean(lo.getBeanName(), argsToUse);
    }
    else {
        //没有配置beanName,尝试使用类型去匹配,如果匹配到多个,会抛异常
        return this.owner.getBean(method.getReturnType(), argsToUse);
    }
}
4.2 配置lookup-method
  • xml配置
<bean id="person" class="com.springapp.mvc.service.Person">
    <lookup-method bean="apple" name="getApple"/>
</bean>
  • 注解配置,使用@Lookup在方法前配置
@Lookup
public Apple getApple(){
    //方法体由Spring自动实现
    return null;
}

配置后,测试一下:

//例4.1
@Component
public class Person {

    public Person(){
        System.out.println("Person 初始化...");
    }
    public void eat(){
        Apple apple = getApple();
        System.out.println("Apple hashcode="+apple.hashCode());
    }
    public static void main(String args[]){
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("mvc-dispatcher-servlet.xml");
        System.out.println("容器初始化完成");
        Person person1 = (Person) beanFactory.getBean("person");
        Person person2 = (Person) beanFactory.getBean("person");
        person1.eat();
        person2.eat();
    }

    @Lookup
    public Apple getApple(){
        return null;
    }
}

测试输出:
Person 初始化...
容器初始化完成
Apple 初始化...
Apple hashcode=1528848756
Apple 初始化...
Apple hashcode=719205737

       对比例子4.1与3.1,可以看到实现了每次调用Apple都重新创建的功能,当然,有人可能会问,这样写与自己去实现getApple的方法体,去new一个对象有什么区别呢?还是为了解耦,这样写的好处就是Apple的创建依然交给Spring,Person不依赖于Apple的实现。

二、Bean的执行流程

1.流程图

       首先上一张老图,这里主要是用BeanFactory来初始化容器时,创建Bean会经历的流程。如果使用ApplicationContext,则会有很多额外处理与功能。
这里写图片描述

2.Spring源码,Bean的创建与初始化

org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
protected <T> T doGetBean(
            final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
            throws BeansException {
    ...
    return createBean(beanName, mbd, args);
    ...
}


org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean
/**
    * Central method of this class: creates a bean instance,
    * populates the bean instance, applies post-processors, etc.
    * @see #doCreateBean
    */
@Override
protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
        throws BeanCreationException {

    //查找Bean相关的类
    // Make sure bean class is actually resolved at this point.
    resolveBeanClass(mbd, beanName);

    // Prepare method overrides.
    ...
    //检查重载(字面意思,非对象继承)方法是否存在
    mbd.prepareMethodOverrides();

    // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
    Object bean = resolveBeforeInstantiation(beanName, mbd);
    ...
    //创建Bean
    Object beanInstance = doCreateBean(beanName, mbd, args);
    ...
    return beanInstance;
}


/**
    * Actually create the specified bean. Pre-creation processing has already happened
    * at this point, e.g. checking {@code postProcessBeforeInstantiation} callbacks.
    * <p>Differentiates between default bean instantiation, use of a
    * factory method, and autowiring a constructor.
    * @param beanName the name of the bean
    * @param mbd the merged bean definition for the bean
    * @param args arguments to use if creating a prototype using explicit arguments to a
    * static factory method. This parameter must be {@code null} except in this case.
    * @return a new instance of the bean
    * @throws BeanCreationException if the bean could not be created
    */
//真正创建Bean的地方
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {
    // Instantiate the bean.
    ...
    //创建Bean实例,会调用构造函数
    instanceWrapper = createBeanInstance(beanName, mbd, args);
    ...
    //进行属性设置
    populateBean(beanName, mbd, instanceWrapper);
    ...
    /*
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean
    初始化Bean,按顺序包括:
        1.BeanNameAware,BeanClassLoaderAware,BeanFactoryAware的对应接口方法
        2.BeanPostProcessor的预处理postProcessBeforeInitialization
        3.InitializingBean接口的afterPropertiesSet
        4.自定义的init-method方法
        5.BeanPostProcessor的后处理postProcessAfterInitialization
    */
    exposedObject = initializeBean(beanName, exposedObject, mbd);
    ...
    // Register bean as disposable.
    //注册DisposableBean,只对singleton的Bean处理
    registerDisposableBeanIfNecessary(beanName, bean, mbd);

    return exposedObject;
}

3.测试代码

public class Person implements BeanNameAware,BeanFactoryAware,BeanClassLoaderAware,InitializingBean,DisposableBean,ApplicationContextAware {

    public Person(){
        System.out.println("Person 初始化...");
    }
    public static void main(String args[]){
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("mvc-dispatcher-servlet.xml");
        System.out.println("容器初始化完成");
        Person person = (Person) beanFactory.getBean("person");
        person = null;
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println("BeanClassLoaderAware的setBeanClassLoader()方法注入Classloader,hashcode="+classLoader.hashCode());
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("BeanFactoryAware的setBeanFactory()方法注入BeanFactory,hashcode="+beanFactory.hashCode());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //ApplicationContext来初始化容器,会有很多额外处理与功能
        System.out.println("ApplicationContextAware的setApplicationContext()方法注入ApplicationContext,hashcode="+applicationContext.hashCode());
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("BeanNameAware的setBeanName()方法name,name="+name);
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean的destory()方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean的afterPropertiesSet()方法");
    }

    @PostConstruct
    public void postConstruct(){
        System.out.println("@PostConstruct注解");
    }

    public void initMethod() {
        System.out.println("init-method配置");
    }

}

测试输出:
Person 初始化...
BeanNameAware的setBeanName()方法name,name=person
BeanClassLoaderAware的setBeanClassLoader()方法注入Classloader,hashcode=918884489
BeanFactoryAware的setBeanFactory()方法注入BeanFactory,hashcode=1574407511
ApplicationContextAware的setApplicationContext()方法注入ApplicationContext,hashcode=916347070
BeanPostProcessor的预处理方法,beanName=person
@PostConstruct注解
InitializingBean的afterPropertiesSet()方法
init-method配置
BeanPostProcessor的后处理方法,beanName=person

4.总结

       Spring在Bean的初始化过程中提供了很多可以进行干涉的方法,不过一般不建议使用实现Spring接口的方式,如实现InitializingBean,而使用@PostConstruct注解或自定义init-method配置,因为实现接口会与Spring相耦合。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值