javascript
再见面试官:你能说说 Spring 框架中 Bean 的生命周期吗?
首先簡單說一下(以下為一個回答的參考模板)
1、實例化一個Bean--也就是我們常說的new;
2、按照Spring上下文對實例化的Bean進行配置--也就是IOC注入;
3、如果這個Bean已經實現了BeanNameAware接口,會調用它實現的setBeanName(String)方法,此處傳遞的就是Spring配置文件中Bean的id值
4、如果這個Bean已經實現了BeanFactoryAware接口,會調用它實現的setBeanFactory(setBeanFactory(BeanFactory)傳遞的是Spring工廠自身(可以用這個方式來獲取其它Bean,只需在Spring配置文件中配置一個普通的Bean就可以);
5、如果這個Bean已經實現了ApplicationContextAware接口,會調用setApplicationContext(ApplicationContext)方法,傳入Spring上下文(同樣這個方式也可以實現步驟4的內容,但比4更好,因為ApplicationContext是BeanFactory的子接口,有更多的實現方法);
6、如果這個Bean關聯了BeanPostProcessor接口,將會調用postProcessBeforeInitialization(Object obj, String s)方法,BeanPostProcessor經常被用作是Bean內容的更改,并且由于這個是在Bean初始化結束時調用那個的方法,也可以被應用于內存或緩存技術;
7、如果Bean在Spring配置文件中配置了init-method屬性會自動調用其配置的初始化方法。
8、如果這個Bean關聯了BeanPostProcessor接口,將會調用postProcessAfterInitialization(Object obj, String s)方法、;
注:以上工作完成以后就可以應用這個Bean了,那這個Bean是一個Singleton的,所以一般情況下我們調用同一個id的Bean會是在內容地址相同的實例,當然在Spring配置文件中也可以配置非Singleton,這里我們不做贅述。
9、當Bean不再需要時,會經過清理階段,如果Bean實現了DisposableBean這個接口,會調用那個其實現的destroy()方法;
10、最后,如果這個Bean的Spring配置中配置了destroy-method屬性,會自動調用其配置的銷毀方法。
結合代碼理解一下
1、Bean的定義
Spring通常通過配置文件定義Bean。如:
<?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-2.0.xsd”><bean?id=”HelloWorld”?class=”com.pqf.beans.HelloWorld”><property?name=”msg”><value>HelloWorld</value></property> </bean></beans>這個配置文件就定義了一個標識為 HelloWorld 的Bean。在一個配置文檔中可以定義多個Bean。
2、Bean的初始化
有兩種方式初始化Bean。
1、在配置文檔中通過指定init-method 屬性來完成
在Bean的類中實現一個初始化Bean屬性的方法,如init(),如:
public?class?HelloWorld{public?String?msg=null;public?Date?date=null;public?void?init()?{msg=”HelloWorld”;date=new?Date();}…… }然后,在配置文件中設置init-mothod屬性:
2、實現 org.springframwork.beans.factory.InitializingBean接口
Bean實現InitializingBean接口,并且增加 afterPropertiesSet() 方法:
public?class?HelloWorld?implement?InitializingBean?{public?String?msg=null;public?Date?date=null;public?void?afterPropertiesSet()?{msg="向全世界問好!";date=new?Date();}…… }那么,當這個Bean的所有屬性被Spring的BeanFactory設置完后,會自動調用afterPropertiesSet()方法對Bean進行初始化,于是,配置文件就不用指定 init-method屬性了。
3、Bean的調用
有三種方式可以得到Bean并進行調用:
1、使用BeanWrapper
HelloWorld?hw=new?HelloWorld(); BeanWrapper?bw=new?BeanWrapperImpl(hw); bw.setPropertyvalue(”msg”,”HelloWorld”); system.out.println(bw.getPropertyCalue(”msg”));2、使用BeanFactory
InputStream?is=new?FileInputStream(”config.xml”); XmlBeanFactory?factory=new?XmlBeanFactory(is); HelloWorld?hw=(HelloWorld)?factory.getBean(”HelloWorld”); system.out.println(hw.getMsg());3、使用ApplicationConttext
ApplicationContext?actx=new?FleSystemXmlApplicationContext(”config.xml”); HelloWorld?hw=(HelloWorld)?actx.getBean(”HelloWorld”); System.out.println(hw.getMsg());4、Bean的銷毀
1、使用配置文件中的 destory-method 屬性
與初始化屬性 init-methods類似,在Bean的類中實現一個撤銷Bean的方法,然后在配置文件中通過 destory-method指定,那么當bean銷毀時,Spring將自動調用指定的銷毀方法。
2、實現 org.springframwork.bean.factory.DisposebleBean接口
如果實現了DisposebleBean接口,那么Spring將自動調用bean中的Destory方法進行銷毀,所以,Bean中必須提供Destory方法。
圖解
總結
以上是生活随笔為你收集整理的再见面试官:你能说说 Spring 框架中 Bean 的生命周期吗?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GraphQL是什么“渣渣“?它想干掉R
- 下一篇: 听说你还没学Spring就被源码编译劝退