分散配置
a) 引入外部bean来读取外部配置文件这样在<value>${name}</value>可以使用这个占位符来注入值了
- <!--第一种,引入外部bean来读取外部配置文件 -->
- <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <!--引入单个文件-->
- property name="location">
- <value>person.properties</value>
- </property>
- <!--引入多个文件-->
- <property name="locations">
- <list>
- <value>person.properties</value>
- </list>
- </property>
- </bean>
b) 使用spring自带的context工具来读取外部文件
首先需要引入spring的context的命名空间和连接地址 。然后使用
<context:property-placeholderlocation="person.properties"/>来引入文件
c) 定制编辑器后处理类
i. 在配置文件中配置
- <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
- <property name="customEditors">
- <map>
- <!-- key指向的是:需要转换的类 -->
- <entry key="cn.csdn.service.Address">
- <!-- value指向的是 实现的编辑器类 -->
- <bean class="cn.csdn.editor.AddressEditor"/>
- </entry>
- </map>
- </property>
- </bean>
ii. 在java中首先需要继承PropertyEditorSupport这个类然后实现setAsText方法,代码如下
- publicclass AddressEditor extends PropertyEditorSupport{
- @Override
- publicvoid setAsText(String text) throws IllegalArgumentException {
- // TODO Auto-generated method stub
- //java实现转换
- if(!"".equals(text)){
- String args[] = text.split("\\.");
- if(args.length>3){
- Address address = new Address();
- address.setProvince(args[0]);
- address.setCity(args[1]);
- address.setStreet(args[2]);
- address.setZipCode(args[3]);
- setValue(address);
- }else{
- this.setAsText(null);
- }
- }else{
- this.setAsText(null);
- }
- }