国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

【Java 3】如何減少spring的配置?

2019-11-06 07:11:58
字體:
來源:轉載
供稿:網友
java 3】---如何減少sPRing的配置?

前言:

     

    在上一篇博文中,小編介紹到將配置文件拆分,分文件,目的是為了分組,分清那部分是負責那部分,但是這還是沒有減少數量,如果要減少數量怎么做?

      

具體技術梗概:

  方法很簡單,就是將公共的東西抽取出來:

      *通過<bean>標簽將公共的配置提取出來,然后指定<bean>標簽中的abstract屬性為true      *在其他<bean>標簽只能怪指定其parent即可

兩個Bean 有公共的部分,將公共的部分抽取出來就行:  

將公共部分抽象出來,形成一個新的配置文件applicationContext-common.xml

         

配置后如下:

  

詳細的代碼如下:

   ApplicationContext-editor.xml:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"	     xmlns:aop="http://www.springframework.org/schema/aop"	     xmlns:tx="http://www.springframework.org/schema/tx"	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><bean id="customEditors" class="org.springframework.beans.factory.config.CustomEditorConfigurer"><property name="customEditors">	 <map>	     <entry key="java.util.Date">	        <bean  class="com.bjpowernode.spring.UtilDatePropertyEditor">	       <property name="pattern" value="yyyy-MM-dd"/>	                  <!--<property name="pattern" value="yyyy/MM/dd/>  	        <property name="pattern" value="yyyy年MM月dd日/> -->         	       </bean>     	     </entry>	 </map></property></bean><!--  <bean id="utilDatePropertyEditor" class="com.bjpowernode.spring.UtilDatePropertyEditor">  </bean> --></beans>

applicationContext-beans.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"	     xmlns:aop="http://www.springframework.org/schema/aop"	     xmlns:tx="http://www.springframework.org/schema/tx"	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  <bean  id="bean1" class="com.bjpowernode.spring.Bean1">    <property name="strValue" value="Hello_Spring"/>    <!--兩種方式注入數字: 方法1<property name="intValue" value="123"/> -->		    <!-- 方法2 -->		    <property name="intValue">		      <value>123</value>		    </property>    <property name="listValue">		    <list>			     <value >list1</value>			     <value >list2</value>		    </list>      </property>    <property name="setValue">	     <set>	        <value>set1</value>	        <value>set2</value>	     </set>    </property>    <property name="arrayValue">	     <list>	        <value>array1</value>	        <value>array2</value>	     </list>    </property>    <property name="mapValue">	     <map>	       <entry key="k1" value="v1"></entry>	       <entry key="k2" value="v2"></entry>	     </map>    </property>       <property name="dateValue" value="2017-2-27"/>             </bean><bean id="bean2" class="com.bjpowernode.spring.Bean2">	  <property name="bean3" ref="bean3"/>	  <property name="bean4" ref="bean4"/>	   <property name="bean5" ref="bean5"/> </bean><!-- <bean id="bean3" class="com.bjpowernode.spring.Bean3">    <property name="id" value="100"/>	<property name="name" value="Daniel"/>	<property name="sex" value="male"/></bean><bean id="bean4" class="com.bjpowernode.spring.Bean4">    <property name="id" value="100"/>	<property name="name" value="Daniel"/>	<property name="sex" value="male"/>	<property name="age" value="102"/></bean> --><bean id="bean5" class="com.bjpowernode.spring.Bean5">    <property name="passWord" value="1"/></bean></beans>

將公共部分抽取出來形成:applicationContext-common.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"	     xmlns:aop="http://www.springframework.org/schema/aop"	     xmlns:tx="http://www.springframework.org/schema/tx"	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="AbstractBean" abstract="true">    <property name="id" value="100"/>	<property name="name" value="Daniel"/>	<property name="sex" value="male"/>    </bean>  <bean id="bean3" class="com.bjpowernode.spring.Bean3" parent="AbstractBean"/>  <bean id="bean4" class="com.bjpowernode.spring.Bean4" parent="AbstractBean">   <!-- 但是Bean4還有特殊的年齡,所以要單獨的拿出來 -->      <property name="age">          <value>102</value>       </property>  </bean></beans>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 射阳县| 揭东县| 剑阁县| 汕头市| 宁蒗| 饶阳县| 威信县| 合阳县| 吉木萨尔县| 天门市| 肃宁县| 新兴县| 同江市| 盘锦市| 东阳市| 芮城县| 新疆| 涞源县| 龙游县| 肥东县| 怀柔区| 砚山县| 尉犁县| 济南市| 渭源县| 若羌县| 平山县| 丹东市| 通榆县| 通州市| 南召县| 蛟河市| 吉安市| 定兴县| 应用必备| 自贡市| 广丰县| 化州市| 曲麻莱县| 绩溪县| 西乌珠穆沁旗|