博客
关于我
spring的生命周期
阅读量:192 次
发布时间:2019-02-28

本文共 3032 字,大约阅读时间需要 10 分钟。

Spring Bean的生命周期是Spring容器管理bean对象的关键环节。了解这一过程有助于更好地理解Spring的内功,提升开发效率。以下是bean在Spring容器中的生命周期详细说明。

当bean被加载到容器中时,其生命周期便开始了。这一过程分为多个关键步骤:

  • Bean的定义加载与实例化

    容器首先会寻找bean的定义信息,并根据这些信息创建bean实例。如果bean实现了BeanNameAware接口,容器会调用setBeanName()方法,将bean的ID传递给bean。

  • 属性配置

    Spring容器根据bean的定义文件或注解,利用依赖注入的方式为bean的属性进行配置。如果bean实现了BeanFactoryAware接口,容器会调用setBeanFactory()方法,将工厂本身传递给bean。

  • Bean名称设置

    如果bean实现了BeanNameAware接口,容器会调用setBeanName()方法,将bean的ID传递给bean。

  • 工厂传递

    如果bean实现了BeanFactoryAware接口,容器会调用setBeanFactory()方法,将工厂本身传入。

  • 后置处理

    如果bean关联了BeanPostProcessor,容器会在初始化之前调用postProcessBeforeInitialization()方法,之后在初始化完成后调用postProcessAfterInitialization()方法。

  • 初始化处理

    如果bean实现了InitializingBean接口,容器会调用afterPropertiesSet()方法进行初始化操作。

  • 初始化方法调用

    如果bean定义了init-method方法,容器会执行该方法。

  • 定制销毁方法

    如果bean实现了DisposableBean接口或定义了destory-method,容器会在关闭容器时调用相应的方法。

  • 在实际开发中,许多步骤通常不会显式使用,尤其是依赖注入、后置处理等高级功能。但理解这些过程有助于更好地定制和优化bean的创建和销毁逻辑。

    以下是与bean生命周期相关的代码示例:

    public class PersonService implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean,DisposableBean {    private String name;    private Integer age;    public Integer getAge() { return age; }    public void setAge(Integer age) { this.age = age; }    public String getName() { return name; }    public PersonService(String abc) { System.out.println("PersonService 函数"); }    public PersonService() { System.out.println("PersonService 函数"); }    public void setName(String name) { System.out.println("setName(String name) 函数"); this.name = name; }    public void sayHi() { System.out.println("hi " + name); }    public void setBeanName(String arg0) { System.out.println("setBeanName 被调用 值" + arg0); }    public void setBeanFactory(BeanFactory arg0) throws BeansException { System.out.println("setBeanFactory " + arg0); }    public void setApplicationContext(ApplicationContext arg0) throws BeansException { System.out.println("setApplicationContext" + arg0); }    public void init() { System.out.println("我自己的init方法"); }    @PreDestroy    public void mydestory() { System.out.println("释放各种资源"); }    @Override    public void destroy() throws Exception { System.out.println("关闭各种资源"); }}

    以下是与BeanPostProcessor相关的代码示例:

    public class MyBeanPostProcessor implements BeanPostProcessor {    public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {         System.out.println("postProcessAfterInitialization 函数被调用");         return arg0;     }    public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {         System.out.println(arg0 + " 被创建的时间是" + new java.util.Date());         return arg0;     }}

    beans.xml配置文件如下:

    xiaoming

    以下是应用程序启动代码:

    public class App1 {    public static void main(String[] args) {        ApplicationContext ac = new ClassPathXmlApplicationContext("com/hsp/beanlife/beans.xml");        PersonService ps1 = (PersonService) ac.getBean("personService");        ps1.sayHi();    }}

    运行结果展示了Spring容器对bean生命周期的全程管理,包括初始化、后置处理、销毁等环节的调用。通过实际项目,可以清晰地看到Spring如何为bean的创建和销毁提供保障。

    转载地址:http://dfej.baihongyu.com/

    你可能感兴趣的文章
    python 三大框架的 介绍。
    查看>>
    Python 下载的 11 种姿势,一种比一种高级!
    查看>>
    python读取一个文件夹下所有图片_初学Python-找出文件夹下的所有图片
    查看>>
    Python 中 3 个不可思议的返回功能
    查看>>
    python 中 dict 的另一种用法
    查看>>
    Python 中 PIL 读取图片出现异常旋转的解决方法
    查看>>
    python读取word表格内容(1)
    查看>>
    python 中os.path.join 双斜杠的解决办法
    查看>>
    python 中PIL.Image和OpenCV图像格式相互转换
    查看>>
    Python 中Semaphore 信号量对象、Event事件、Condition
    查看>>
    python 中with的使用及样例
    查看>>
    python读取wav文件并播放[pyaudio/wave]
    查看>>
    python读取txt文件的行数
    查看>>
    Python 中内置的最大堆 API
    查看>>
    Python 中只有一个 True 和一个 False 对象吗?
    查看>>
    python读取mtcars数据集并实现以下操作_关于数据处理。。,Python交流,技术交流区,鱼C论坛 - Powered by Discuz!...
    查看>>
    Python 中多线程与多处理之间的区别
    查看>>
    Python 中如何使用 lambda 函数
    查看>>
    Python 中如何创建多行字符串?
    查看>>
    Python 中如何处理异常?
    查看>>