Spring_Beans 的生命周期理解

如果你有太多具有相同名称的初始化或者销毁方法的 Bean,那么你不需要在每一个 bean 上声明初始化方法和销毁方法。框架使用 元素中的 default-init-method 和 default-destroy-method 属性提供了灵活地配置这种情况,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"  
    default-init-method="init" 
    default-destroy-method="destroy"
    >
    <bean id="helloWorld" class="com.zhw.test.HelloSpring">
       <property name="message" value="Hello World!"/>
   </bean> 

<!-- Bean's Life cycle -->
    <bean id="life_cycle" 
       class="com.zhw.test.HelloSpring">
       <property name="message" value="Hello World!"/>
   </bean>

理解运行原理

  • 当在main.class 中调用类似于** AbstractApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml");的加载bean 代码时,它是从第一个 id=helloworld 的bean开始调用init,接下来再调用id=life_cycle的bean**的init,
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容