當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
【Spring】- Bean生命周期
生活随笔
收集整理的這篇文章主要介紹了
【Spring】- Bean生命周期
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
Spring Bean的生命周期:
Bean生命周期相關接口介紹:
- BeanNameAware: Bean可獲取在BeanFactory中的名稱
- BeanFactoryAware:Bean可獲取所在的BeanFactory對象
- ApplicationContextAware:可獲取Bean所在的ApplicationContext
- InitializingBean:Bean屬性設置完成后的自定義操作接口:例如設置自定義的初始化方法init-method
- DisposableBean:銷毀Bean的相關資源
Spring配置文件: 配置簡單的Bean和1個Bean后置處理器,并且設置自定義的初始化方法:init-method="defineInitMethod"
<bean id="concreteBean" class="com.zhiwei.lifecycle.ConcreteBean" init-method="defineInitMethod" ><property name="name" value="squirrel"/> </bean><bean class="com.zhiwei.lifecycle.BeanPostProcessorCase"/>Bean對象,實現相關生命周期接口:
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*/@Overridepublic void setBeanName(String name) {System.err.println("BeanNameAware:ConcreteBean實例對象在BeanFactory的名稱:"+name);}/*** BeanFactoryAware:獲取Bean所在的Beanfactory*/@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {boolean flag = beanFactory.containsBean("concreteBean"); //使用BeanFactory的方法判斷Bean是否存在System.err.println("BeanfactoryAware:BeanFactory是否存在concreteBean定義:"+flag);}/*** ApplicationContextAware:獲取Bean實例所在的高級BeanFactory容器:ApplicationContext*/@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {boolean flag = applicationContext.containsBean("concreteBean");System.err.println("ApplicationContextAware:ApplicationContext是否存在concreteBean定義:"+flag);}/*** InitializingBean:Bean屬性設置完成之后的自定義初始化工作*/@Overridepublic void afterPropertiesSet() throws Exception {System.err.println("ConcreteBean完成屬性設置,正在進行初始化........");}/*** DisposableBean:銷毀Bean相關的資源:destroy-method屬性*/@Overridepublic void destroy() throws Exception {System.err.println("IOC容器正在銷毀Bean資源........");}/*** 自定義初始化方法:init-method屬性*/public void defineInitMethod(){System.err.println("Bean正在自定義初始化......");} }Bean后置處理器:
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初始化之后被調用*/@Overridepublic Object postProcessAfterInitialization(Object object, String id) throws BeansException {System.err.println("已經完成" + object + "實例初始化:,id =" + id + ",時間:" + new Date());return object;} }測試類:
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資源........轉載于:https://my.oschina.net/yangzhiwei256/blog/3017084
總結
以上是生活随笔為你收集整理的【Spring】- Bean生命周期的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【树型DP】加分二叉树
- 下一篇: docker in all