在Spring中,從BeanFactory或ApplicationContext取得的實(shí)例為Singleton,也就是預(yù)設(shè)為每個(gè)Bean的別名只維持一個(gè)實(shí)例。因此會(huì)存在資源共享的問(wèn)題。如需要,則可以將其設(shè)置為”singleton=false”。
在這里要注意的是此singleton的設(shè)置只是針對(duì)單一的配置文件而言,也就是說(shuō),如果存在多個(gè)BeanFactory或ApplicationContext,即使將singleton設(shè)置為false,也會(huì)存在資源共享的問(wèn)題。
下面來(lái)說(shuō)下Bean的生命周期:
l Bean的建立
有BeanFactory讀取Bean定義文件,并生成各個(gè)Bean實(shí)例
l 屬性注入
執(zhí)行相關(guān)的Bean屬性依賴注入
l BeanNameAware的setBeanName()
如果Bean類有實(shí)現(xiàn)BeanNameAware接口,則執(zhí)行他的setBeanName()方法。
l BeanFactoryAware的setBeanFactory()
如果Bean類有實(shí)現(xiàn)BeanFactoryAware接口,則執(zhí)行他的setBeanFactory()方法
l BeanPostProcessors的processBeforeInitialization()
如果任何的BeanPostProcessors實(shí)例與Bean實(shí)例關(guān)聯(lián),則執(zhí)行BeanPostProcessors實(shí)例的processBeforeInitialization()方法
l initializingBean的afterPropertiesSet()
l 如果Bean類已實(shí)現(xiàn)了org.springframework.beans.factory.InitializingBean接口,則執(zhí)行它的afterPropertiesSet()方法。
l Bean定義問(wèn)教案中定義的init-method
可以在Bean定義文件中使用”init-method”屬性設(shè)定方法名稱,進(jìn)入這個(gè)階段,就會(huì)執(zhí)行initBean()方法。
l BeanPostProcessors的processAfterInitialization()
如果任何的BeanPostProcessors實(shí)例與Bean實(shí)例關(guān)聯(lián),則執(zhí)行BeanPostProcessors實(shí)例的processAfterInitialization()方法
l DisposableBean的destroy()
在容器關(guān)閉時(shí),如果Bean類有實(shí)現(xiàn)org.springframework.beans.factory.
DisposabelBean接口,則執(zhí)行它的destroy()方法
l Bean定義文件中定義的destroy-method
可以在Bean定義文件中使用” destroy-method”屬性設(shè)定方法名稱,進(jìn)入這個(gè)階段,就會(huì)執(zhí)行destroyBean()方法。
以上是BeanFactory管理Bean的方法,如果采用ApplicationContext管理,則會(huì)稍有不同。使用ApplicationContext來(lái)生成和管理Bean實(shí)例的話,在執(zhí)行BeanFactoryAware的SetBeanFactory()階段之后,若Bean類上有實(shí)現(xiàn)ApplicationContextAware接口,則執(zhí)行其setApplicationContext()方法,接著才繼續(xù)進(jìn)行BeanPostProcessors的processBeforeInitialization()及之后的流程。
下面就給出一簡(jiǎn)單demo,以供參考:
public class CommonBean implements BeanNameAware, BeanFactoryAware,
ApplicationContextAware, BeanPostProcessor {
public void setBeanName(String name) {
System.out.println("This is setBeanName method!");
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("This is setBeanFactory method!");
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("This is setApplicationContext method!");
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("This is postProcessAfterInitialization method!");
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("This is postProcessBeforeInitialization method!");
return bean;
}
public void initBean(){
System.out.println("This is initBean method!");
}
public void destroyBean(){
System.out.println("This is destroyBean method!");
}
}
在context.getBean(“commonBean”)執(zhí)行時(shí),輸出結(jié)果:
This is setBeanName method!
This is setBeanFactory method!
This is setApplicationContext method!
This is initBean method!
This is postProcessBeforeInitialization method!
This is postProcessAfterInitialization method!
本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/prince2270/archive/2008/09/27/2986231.aspx
posted on 2009-08-26 14:28
super_nini 閱讀(219)
評(píng)論(0) 編輯 收藏