spring揭秘閱讀筆記

applicationContext 繼承關(guān)系

image.png

beanFactory,BeanDefinitionRegistry,BeanDefintion 繼承關(guān)系

image.png

關(guān)于抽象或繼承class的定義

image.png

springbean 生命周期

image.png

ThreadScope默認沒有注冊, 需要手工注冊該 scope
Scope threadScope = new ThreadScope();
beanFactory.registerScope("thread",threadScope);

用CustomScopeConfigurer 配置注冊scope

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="thread" value="com.foo.ThreadScope"/>
</map>
</property>
</bean>

更多Scope相關(guān)的實例,可以參照同一站點的一篇文章“More fun with Spring scopes”(http://jroller.
com/eu/entry/more_fun_with_spring_scopes)

factoryMethod的使用

靜態(tài)工廠的使用

<bean id="bar" class="...StaticBarInterfaceFactory" factory-method="getInstance"/> 

非靜態(tài)工廠使用

<bean id="barFactory" class="...NonStaticBarInterfaceFactory"/> <!-- 要先實例化 -->
8 <bean id="bar" factory-bean="barFactory" factory-method="getInstance"/>

prototype scope bean 陷阱

@autowire
FXNewsBean fx; // 如果當前bean 是singleScope的, 該 fx在當前bean里永遠只有一份。 但其與其他bean 不共用。如果當前bean 每次都 想用新的, 參見以下代碼

public class MockNewsPersister implements IFXNewsPersister,BeanFactoryAware {
private BeanFactory beanFactory;

public void setBeanFactory(BeanFactory bf) throws BeansException {
 this.beanFactory = bf;
 }
15 public void persistNews(FXNewsBean bean) {
 persistNews();
 }
public void persistNews()
 { 16 System.out.println("persist bean:"+getNewsBean());
 }
17 public FXNewsBean getNewsBean() {
 return beanFactory.getBean("newsBean");

 }
} 

ObjectFactoryCreatingFactoryBean

public class MockNewsPersister implements IFXNewsPersister {
private ObjectFactory newsBeanFactory;

 public void persistNews(FXNewsBean bean) {
 persistNews();
 }
 public void persistNews()
 {
 System.out.println("persist bean:"+getNewsBean());
 }
 public FXNewsBean getNewsBean() {
 return newsBeanFactory.getObject();
 }
 public void setNewsBeanFactory(ObjectFactory newsBeanFactory) {
 this.newsBeanFactory = newsBeanFactory;
 }
} 

PropertyPlaceholderConfigurer (小心PropertyOverrideConfigurer)

PropertyPlaceholderConfigurer不單會從其配置的properties文件中加載配置項,同時還會檢
查Java的System類中的Properties,可以通過setSystemPropertiesMode()或者setSystemPropertiesModeName()來控制是否加載或者覆蓋System相應(yīng)Properties的行為。PropertyPlaceholderConfigurer提供了SYSTEM_PROPERTIES_MODE_FALLBACK、SYSTEM_PROPERTIES_MODE_NEVER和SYSTEM_
PROPERTIES_MODE_OVERRIDE三種模式。默認采用的是SYSTEM_PROPERTIES_ MODE_FALLBACK,即如
http://showmecode.cn/links/book http://blog.csdn.net/jiongyi1
4.4 容器背后的秘密 69
果properties文件中找不到相應(yīng)配置項,則到System的Properties中查找,我們還可以選擇不檢查System
的Properties或者覆蓋它。更多信息請參照PropertyPlaceholderConfigurer的Javadoc文檔

PropertyEditorSupport
<bean class="org.springframework.bean
 <property name="customEditors">
 <map>
 <entry key="java.util.Date">
 <ref bean="datePropertyEditor"/>
 </entry>
 </map>
</prop
</bean>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容