Bean生命週期相關接口介紹:java
Spring配置文件: 配置簡單的Bean和1個Bean後置處理器,而且設置自定義的初始化方法:init-method="defineInitMethod"spring
<bean id="concreteBean" class="com.zhiwei.lifecycle.ConcreteBean" init-method="defineInitMethod" > <property name="name" value="squirrel"/> </bean> <bean class="com.zhiwei.lifecycle.BeanPostProcessorCase"/>
Bean對象,實現相關生命週期接口:app
package com.zhiwei.lifecycle; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class ConcreteBean implements BeanNameAware,BeanFactoryAware, ApplicationContextAware,InitializingBean,DisposableBean{ private String name; public ConcreteBean() { System.err.println("ConcreteBean實例對象被建立......."); } public String getName() { return name; } public void setName(String name) { System.err.println("ConcreteBean實例對象默認name屬性注入........."); this.name = name; } /** * BeanNameAware:獲取Bean在BeanFactory的名稱:id */ @Override public void setBeanName(String name) { System.err.println("BeanNameAware:ConcreteBean實例對象在BeanFactory的名稱:"+name); } /** * BeanFactoryAware:獲取Bean所在的Beanfactory */ @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { boolean flag = beanFactory.containsBean("concreteBean"); //使用BeanFactory的方法判斷Bean是否存在 System.err.println("BeanfactoryAware:BeanFactory是否存在concreteBean定義:"+flag); } /** * ApplicationContextAware:獲取Bean實例所在的高級BeanFactory容器:ApplicationContext */ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { boolean flag = applicationContext.containsBean("concreteBean"); System.err.println("ApplicationContextAware:ApplicationContext是否存在concreteBean定義:"+flag); } /** * InitializingBean:Bean屬性設置完成以後的自定義初始化工做 */ @Override public void afterPropertiesSet() throws Exception { System.err.println("ConcreteBean完成屬性設置,正在進行初始化........"); } /** * DisposableBean:銷燬Bean相關的資源:destroy-method屬性 */ @Override public void destroy() throws Exception { System.err.println("IOC容器正在銷燬Bean資源........"); } /** * 自定義初始化方法:init-method屬性 */ public void defineInitMethod(){ System.err.println("Bean正在自定義初始化......"); } }
Bean後置處理器:ide
package com.zhiwei.lifecycle; import java.util.Date; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; /** * BeanPostProcessor:後置處理器(針對全部的Bean實例) 做用:Bean徹底實例化以前能夠修改Bean的屬性 */ public class BeanPostProcessorCase implements BeanPostProcessor { /** * Bean初始化以前被調用 */ public Object postProcessBeforeInitialization(Object object, String id) throws BeansException { System.err.println("正在進行" + object + "實例初始化:,id =" + id + ",時間:" + new Date()); return object; }; /** * Bean初始化以後被調用 */ @Override public Object postProcessAfterInitialization(Object object, String id) throws BeansException { System.err.println("已經完成" + object + "實例初始化:,id =" + id + ",時間:" + new Date()); return object; } }
測試類:post
package com.zhiwei.lifecycle; import org.springframework.context.*; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/zhiwei/lifecycle/applicationContext.xml"); ConcreteBean concreteBean=(ConcreteBean) applicationContext.getBean("concreteBean"); System.out.println(concreteBean.getName()); ConfigurableApplicationContext cac = (ConfigurableApplicationContext) applicationContext; cac.close(); //關閉IOC容器 } }
控制檯打印日誌:測試
ConcreteBean實例對象被建立....... ConcreteBean實例對象默認name屬性注入......... BeanNameAware:ConcreteBean實例對象在BeanFactory的名稱:concreteBean BeanfactoryAware:BeanFactory是否存在concreteBean定義:true ApplicationContextAware:ApplicationContext是否存在concreteBean定義:true 正在進行com.zhiwei.lifecycle.ConcreteBean@6ebc05a6實例初始化:,id =concreteBean,時間:Tue Feb 14 14:50:47 CST 2017 ConcreteBean完成屬性設置,正在進行初始化........ Bean正在自定義初始化...... 已經完成com.zhiwei.lifecycle.ConcreteBean@6ebc05a6實例初始化:,id =concreteBean,時間:Tue Feb 14 14:50:47 CST 2017 squirrel IOC容器正在銷燬Bean資源........