spring如何使用命名空間p簡化bean的配置
這篇文章主要介紹了spring如何使用命名空間p簡化bean的配置,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
一般情況下,我們是這么配置bean的:
<bean id="car1" class="com.gong.spring.beans.Car">
<property name="name" value="baoma"></property>
</bean>
<bean id="car2" class="com.gong.spring.beans.Car">
<property name="name" value="benchi"></property>
</bean>
<bean id="car3" class="com.gong.spring.beans.Car">
<property name="name" value="binli"></property>
</bean>
<bean id="student" class="com.gong.spring.beans.Student">
<property name="name" value="tom"></property>
<property name="age" value="12"></property>
<property name="score" value="98.00"></property>
<property name="cars" ref="cars">
</property>
</bean>
<util:list id="cars">
<ref bean="car1"/>
<ref bean="car2"/>
<ref bean="car3"/>
</util:list>
說明:cars是公用的集合Bean,Student里有name、age、score以及類型為List<Car>的car屬性。
在引入了命名空間之后,我們就可以這么進行配置了:
<bean id="car1" class="com.gong.spring.beans.Car" p:name="baoma"></bean>
<bean id="car2" class="com.gong.spring.beans.Car" p:name="benchi"></bean>
<bean id="car3" class="com.gong.spring.beans.Car" p:name="binli"></bean>
<bean id="student" class="com.gong.spring.beans.Student"
p:name="tom" p:age="12" p:score="98.00" p:cars-ref="cars"></bean>
<util:list id="cars">
<ref bean="car1"/>
<ref bean="car2"/>
<ref bean="car3"/>
</util:list>
相較于原來的,代碼簡潔了很多。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Spring Aop之AspectJ注解配置實現(xiàn)日志管理的方法
下面小編就為大家分享一篇Spring Aop之AspectJ注解配置實現(xiàn)日志管理的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
MapStruct處理Java中實體與模型間不匹配屬性轉換的方法
今天小編就為大家分享一篇關于MapStruct處理Java中實體與模型間不匹配屬性轉換的方法,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Spring Boot Admin(監(jiān)控工具)的使用
今天我們將會講解一個優(yōu)秀的監(jiān)控工具Spring Boot Admin。 它采用圖形化的界面,讓我們的Spring Boot管理更加簡單,需要的朋友可以參考下2020-02-02
SpringBoot框架實現(xiàn)切換啟動開發(fā)環(huán)境和測試環(huán)境
這篇文章主要介紹了SpringBoot框架實現(xiàn)切換啟動開發(fā)環(huán)境和測試環(huán)境,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12

