Spring配置文件中parent與abstract的使用
Spring配置文件parent與abstract
其實(shí)在基于spring框架開發(fā)的項(xiàng)目中,如果有多個(gè)bean都是一個(gè)類的實(shí)例,如配置多個(gè)數(shù)據(jù)源時(shí),大部分配置的屬性都一樣,只有少部分不一樣。
這樣的話在配置文件中可以配置和對象一樣進(jìn)行繼承。
例如
<bean id="testParent" abstract="true" class="com.bean.TestBean"> <property name="param1" value="父參數(shù)1"/> <property name="param2" value="父參數(shù)2"/> </bean> <bean id="testBeanChild1" parent="testParent"/> <bean id="testBeanChild2" parent="testParent"> <property name="param1" value="子參數(shù)1"/> </bean>
其中 abstract="true" 的配置表示:此類在Spring容器中不會生成實(shí)例。
parent="testBeanParent" 代表子類繼承了testBeanParent,會生成具體實(shí)例,在子類Bean中配置會覆蓋父類對應(yīng)的屬性。
spring使用parent屬性來減少配置
在基于spring框架開發(fā)的項(xiàng)目中,如果有多個(gè)bean都是一個(gè)類的實(shí)力,如配置多個(gè)數(shù)據(jù)源時(shí),大部分配置的屬性都一樣,只有少部分不一樣,經(jīng)常是copy上一個(gè)的定義,然后修改不一樣的地方。其實(shí)spring bean定義也可以和對象一樣進(jìn)行繼承。
示例如下:
<bean id="testBeanParent" abstract="true" class="com.wanzheng90.bean.TestBean"> <property name="param1" value="父參數(shù)1"/> <property name="param2" value="父參數(shù)2"/> </bean> <bean id="testBeanChild1" parent="testBeanParent"/> <bean id="testBeanChild2" parent="testBeanParent"> <property name="param1" value="子參數(shù)1"/> </bean>
testBeanParent是父bean,其中abstract=“true”表示testBeanParen不會被創(chuàng)建,類似于于抽象類。其中testBeanChild1、testBeanChild2繼承了testBeanParent、,其中testBeanChild2重新對param1屬性進(jìn)行了配置,因此會覆蓋testBeanParent
對param1屬性屬性的配置。
代碼如下:
TestBean
public class TestBean { private String param1; private String param2; public String getParam1() { return param1; } public void setParam1(String param1) { this.param1 = param1; } public String getParam2() { return param2; } public void setParam2(String param2) { this.param2 = param2; } }
App:
public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml"); TestBean testBeanChild1 = (TestBean) context.getBean("testBeanChild1"); System.out.println( testBeanChild1.getParam1()); System.out.println( testBeanChild1.getParam2()); TestBean testBeanChild2 = (TestBean) context.getBean("testBeanChild2"); System.out.println( testBeanChild2.getParam1()); System.out.println( testBeanChild2.getParam2()); } }
app main函數(shù)輸出:
父參數(shù)1
父參數(shù)2
子參數(shù)1
父參數(shù)2
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
servlet之cookie簡介_動力節(jié)點(diǎn)Java學(xué)院整理
Cookie技術(shù)誕生以來,它就成了廣大網(wǎng)絡(luò)用戶和Web開發(fā)人員爭論的一個(gè)焦點(diǎn)。下面這篇文章主要給大家介紹了關(guān)于servlet之cookie簡介的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07搭建 springboot selenium 網(wǎng)頁文件轉(zhuǎn)圖片環(huán)境的詳細(xì)教程
這篇文章主要介紹了搭建 springboot selenium 網(wǎng)頁文件轉(zhuǎn)圖片環(huán)境,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Mybatis配置之typeAlias標(biāo)簽的用法
這篇文章主要介紹了Mybatis配置之typeAlias標(biāo)簽的用法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07springboot項(xiàng)目中引入本地依賴jar包并打包到lib文件夾中
這篇文章主要介紹了springboot項(xiàng)目中引入本地依賴jar包,如何打包到lib文件夾中,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04java中for循環(huán)執(zhí)行的順序圖文詳析
關(guān)于java的for循環(huán)想必大家非常熟悉,它是java常用的語句之一,這篇文章主要給大家介紹了關(guān)于java中for循環(huán)執(zhí)行順序的相關(guān)資料,需要的朋友可以參考下2021-06-06Spring Security Oauth2.0 實(shí)現(xiàn)短信驗(yàn)證碼登錄示例
本篇文章主要介紹了Spring Security Oauth2.0 實(shí)現(xiàn)短信驗(yàn)證碼登錄示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01SpringBoot項(xiàng)目如何設(shè)置權(quán)限攔截器和過濾器
這篇文章主要介紹了使用lombok時(shí)如何自定義get、set方法問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07