spring的分散配置

分散配置

a)   引入外部bean来读取外部配置文件这样在<value>${name}</value>可以使用这个占位符来注入值了

[html]  view plain copy
  1. <!--第一种,引入外部bean来读取外部配置文件 -->  
  2.   
  3. <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  4.   
  5.     <!--引入单个文件-->  
  6.   
  7. property name="location">  
  8.   
  9.         <value>person.properties</value>  
  10.   
  11.     </property>  
  12.   
  13. <!--引入多个文件-->  
  14.   
  15.     <property name="locations">  
  16.   
  17.         <list>  
  18.   
  19.             <value>person.properties</value>  
  20.   
  21.         </list>  
  22.   
  23.     </property>  
  24.   
  25. </bean>  


b)   使用spring自带的context工具来读取外部文件

首先需要引入spring的context的命名空间和连接地址 。然后使用

<context:property-placeholderlocation="person.properties"/>来引入文件

c)   定制编辑器后处理类

i.   在配置文件中配置

[html]  view plain copy
  1. <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">  
  2.   
  3.       <property name="customEditors">  
  4.   
  5.          <map>  
  6.   
  7.              <!-- key指向的是:需要转换的类 -->  
  8.   
  9.              <entry key="cn.csdn.service.Address">  
  10.   
  11.                    <!-- value指向的是 实现的编辑器类 -->  
  12.   
  13.                   <bean class="cn.csdn.editor.AddressEditor"/>  
  14.   
  15.              </entry>  
  16.   
  17.          </map>  
  18.   
  19.       </property>  
  20.   
  21.  </bean>  


ii. 在java中首先需要继承PropertyEditorSupport这个类然后实现setAsText方法,代码如下

[java]  view plain copy
  1. publicclass AddressEditor extends PropertyEditorSupport{  
  2.   
  3.    
  4.   
  5.     @Override  
  6.   
  7.     publicvoid setAsText(String text) throws IllegalArgumentException {  
  8.   
  9.         // TODO Auto-generated method stub  
  10.   
  11.         //java实现转换  
  12.   
  13.         if(!"".equals(text)){  
  14.   
  15.              String args[] = text.split("\\.");  
  16.   
  17.              if(args.length>3){  
  18.   
  19.                  Address address = new Address();  
  20.   
  21.                  address.setProvince(args[0]);  
  22.   
  23.                  address.setCity(args[1]);  
  24.   
  25.                  address.setStreet(args[2]);  
  26.   
  27.                  address.setZipCode(args[3]);  
  28.   
  29.                  setValue(address);  
  30.   
  31.              }else{  
  32.   
  33.                  this.setAsText(null);  
  34.   
  35.              }  
  36.   
  37.         }else{  
  38.   
  39.             this.setAsText(null);  
  40.   
  41.         }  
  42.   
  43. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值