Spring IOC学习笔记(一)--Bean的生命周期

这篇博客主要介绍了Spring中Bean的生命周期,包括BeanFactory容器的延迟初始化和ApplicationContext的立即初始化策略。文章详细阐述了如何通过反射和CGLIB实例化Bean,并解释了InstantiationStrategy接口的角色。此外,还提到了Spring如何通过Aware接口注入依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Bean的生命周期

BeanFactory容器启动默认采取的是延迟初始化,二ApplicationContext采用的是容器启动后就会实例化所有的Bean。

实例化Bean
设置Bean的相关属性
检查Bean实现的相关的Aware接口,将相关依赖设置到Bean实例中
BeanPostProcessor进行前置处理
检查是否有自定义的init-method方法
BeanPostProcessor进行后置处理
是否实现了Disposable方法
是否自定义了destroy-method

二、实例化Bean

容器内部使用策略模式来实例化Bean实例,通常通过反射或者是CGLIB动态字节码的方式初始化相应的Bean实例。
org.springframework.beans.factory.support.InstantiationStrategy定义了实例化的接口,其直接子类SimpleInstantiationStrategy实现了简单的对象实例化,支持构造器注入的方式,但是不支持方法注入的方式实例化Bean。CGLIBInstantiationStrategy继承了SimpleInstantiationStrategy,支持方法注入实例化Bean。此时容器生成了一个由BeanWrapper包装了的实例对象。通过BeanWrapperImpl这个实现了BeanWrapper接口的对象就可以实例化对象。

三、

当对象完成了并且相关属性设置完成以后,Spring容器会检查当前对象实例是否实现了一系列的一Aware命名结尾的接口,如果是则将这些Aware接口规定的依赖注入给dangqina
JavaBean类

package chapter1.beanlifecycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class Person implements BeanFactoryAware,ApplicationContextAware,
                                InitializingBean,DisposableBean{
    private String name;
    private String phone;

    private BeanFactory beanFactory;
    private String beanName;
    private ApplicationContext applicationContext;
    /**
     * default constructor
     */
    public Person() {

    }

    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getPhone() {
        return phone;
    }


    public void setPhone(String phone) {
        this.phone = phone;
    }


    @Override
    public void setBeanFactory(BeanFactory arg0) throws BeansException {
        System.out.println("Spring call the setBeanFactory to transfer the Instance of BeanFactory");
        this.beanFactory = arg0;
    }


    @Override
    public void setApplicationContext(ApplicationContext arg0) throws BeansException {
        System.out.println("Spring call the setApplicationContext to transfer the ApplicationContext");
        this.applicationContext = arg0;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Spring call the afterPropertiesSet()");
    }


    @Override
    public void destroy() throws Exception {
        System.out.println("Spring call the destory() to call the destory-method of Person bean");
    }

    public void selfDefineDestory() {
        System.out.println("Spring call the destory-method by self definition");
    }

    public void selfDefineInit() {
        System.out.println("Spring call the initialize-method by self definition");
    }
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <bean id="person" class="chapter1.beanlifecycle.Person"  scope="singleton" 
    p:name="Zhang San" p:phone="15900000000" 
    init-method="selfDefineInit" destroy-method="selfDefineDestory"/> 
</beans>

输出结果

信息: Loading XML bean definitions from class path resource [beanlifecycle.xml]
Spring call the setBeanFactory to transfer the Instance of BeanFactory
Spring call the setApplicationContext to transfer the ApplicationContext
Spring call the afterPropertiesSet()
Spring call the initialize-method by self definition
容器初始化成功
chapter1.beanlifecycle.Person@3ecd23d9
现在开始关闭容器!
五月 12, 2018 10:37:44 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@270421f5: startup date [Sat May 12 22:37:44 CST 2018]; root of context hierarchy
Spring call the destory() to call the destory-method of Person bean
Spring call the destory-method by self definition

<4>参考文献

[1] http://www.cnblogs.com/zrtqsk/p/3735273.html
[2]《Spring in Action》chapter1-bean的生命周期

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值