在设置一个对象类型的属性时:
- 使用
ref
引用已经存在的对象
- 使用
bean
标签创建一个匿名对象
- 还有一种方式是给
value
赋值一个字符串,Spring 自动的把这个字符串转换为对象
第三种方式的实现需要以下几步:
- 需要提供一个把 String 转换为对象的类,称之为自定义编辑器(CustomEditor)
- 然后把这个自定义编辑器注册到 Spring Container
- 使用字符串配置对象属性
下面就以为 Address 实现一个自定义编辑器 AddressEditor 为例,介绍具体的实现细节。
1. User
1 2 3 4 5 6 7 8 9 10
| package com.xtuer.beans;
public class User { private int id; private String username; private String password; private Address address; }
|
2. Address
1 2 3 4 5 6 7 8 9 10
| package com.xtuer.beans;
public class Address { private int id; private String country; private String province; private String street; }
|
3. AddressEditor
自定义编辑器需要实现 java.beans.PropertyEditor 接口,java.beans.PropertyEditorSupport 提供了 PropertyEditor 的默认实现,我们只需要继承 PropertyEditorSupport
并实现 setAsText()
方法就可以了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.xtuer.beans.editor;
import com.xtuer.beans.Address; import java.beans.PropertyEditorSupport;
public class AddressEditor extends PropertyEditorSupport {
@Override public void setAsText(String text) throws IllegalArgumentException { String[] tokens = text.split("\\|");
Address address = new Address(); address.setId(Integer.parseInt(tokens[0])); address.setCountry(tokens[1]); address.setProvince(tokens[2]); address.setStreet(tokens[3]);
setValue(address); } }
|
4. spring-beans.xml 里注册自定义编辑器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="com.xtuer.beans.Address" value="com.xtuer.beans.editor.AddressEditor"/> </map> </property> </bean> ... </beans>
|
5. 使用字符串配置 Address 对象属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="com.xtuer.beans.Address" value="com.xtuer.beans.editor.AddressEditor"/> </map> </property> </bean>
<bean id="user" class="com.xtuer.beans.User"> <property name="username" value="Alice"/> <property name="password" value="Passw0rd"/> <property name="address" value="2|China|BeiJing|WangJin"/> </bean> </beans>
|
6. 测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import com.xtuer.beans.User; import com.xtuer.util.CommonUtils; import com.xtuer.beans.editor.AddressEditor; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AddressEditorTest { private static ApplicationContext context;
@BeforeClass public static void setup() { context = new ClassPathXmlApplicationContext("spring-beans.xml"); }
@Test public void test() { AddressEditor editor = new AddressEditor(); editor.setAsText("2|China|BeiJing|WangJin"); CommonUtils.output(editor.getValue()); }
@Test public void testAddressEditor() { User user = context.getBean("user", User.class); CommonUtils.output(user); } }
|