首页 > 编程 > 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>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表