3月28
首先我们看下什么是属性编辑器,到底它有什么作用呢?
* 自定义属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入
spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器
* 如何定义属性编辑器?
* 继承PropertyEditorSupport类,覆写setAsText()方法(注意要将处理完成的对象通过PropertyEditorSupport的setValue设置回去)
*向IoC容器中注册自定义的属性编辑器(两种方式:1 在配置文件中注册 2 在程序中注册)
接下来我们就看看一个关于日期的属性编辑器的部署过程:
首先定义一个测试类:
比如:
有一个类里面有一个Date属性
package tk.hecks.property;
import java.util.Date;
public class User {
private String username;
private Date birthday;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
然后继承PropertyEditorSupport,实现自定义的属性编辑器,UtilDatePropertyEditor.java 如下,必须继承java.beans.PropertyEditorSupport类,覆写setAsText()方法,再将处理完的对象setValue回去。
package tk.hecks.property;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class UtilDatePropertyEditor extends PropertyEditorSupport{
private String format;
public UtilDatePropertyEditor(String format) {
this.format=format;
}
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat sdf=new SimpleDateFormat(format);
try {
Date date=sdf.parse(text);
this.setValue(date);
} catch (ParseException e) {
System.out.println(e.getMessage()+"日期的格式不对");
}
}
public void setFormat(String format) {
this.format = format;
}
}
接着我们首先定义配置文件:(采用配置方式)
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
<!--将user中的Date赋值2014-03-28,spring会认为2014-03-28是String,无法转换成Date,会报错!-->
<bean id="user" class="tk.hecks.property.User">
<property name="username" value="heck"/>
<property name="birthday" value="2014-03-28"/>
</bean>
<!-- 所以自定义的属性编辑器 -->
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.property.UtilDatePropertyEditor">
<!--干脆把format也注入,灵活处理格式-->
<property name="format" value="yyyy-MM-dd"/>
</bean>
</entry>
</map>
</property>
</bean>
</beans>
这样就可以完成正确解析了,注意customEditors是Spring的类CustomEditorConfigurer提供的属性,是一个Map,里面存放的都是自定义的编辑器(customEditors),比如这里存放的是UtilDatePropertyEditor日期编辑器,看CustomEditorConfigurer源码就知道了。
接下来我们测试一下:
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;
public class InjectionTest extends TestCase {
private BeanFactory factory;
@Override
protected void setUp() throws Exception {
factory = new ClassPathXmlApplicationContext("applicationContext.xml");
}
public void testInjection1() {
User user= (User)factory.getBean("user");
System.out.println("name:"+user.getUsername()+"\nbirthday:"+user.getBirthday());
}
}
或者用main方法来测试
package tk.hecks.property;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
public class TestPropertyEditor {
public static void main(String[] args) {
// ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext-*.xml");
// User user=(User)ac.getBean("user");
// System.out.println("name:"+user.getUsername()+"\nbirthday:"+user.getBirthday());
//它等价于
ConfigurableBeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext-beans.xml"));
CustomEditorConfigurer configurer=(CustomEditorConfigurer)beanFactory.getBean("customEditorConfigurer");
configurer.postProcessBeanFactory((ConfigurableListableBeanFactory) beanFactory);
User user=(User)beanFactory.getBean("user");
System.out.println("name:"+user.getUsername()+"\nbirthday:"+user.getBirthday());
}
}
输出结果:
name:heck
birthday:Fri Mar 28 00:00:00 CDT 2014
接下来我们在程序中注册自定义的属性编辑器:
package tk.hecks.property;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class TestPropertyEditorResgister {
public static void main(String[] args) {
ConfigurableBeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
beanFactory.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(java.util.Date.class, new UtilDatePropertyEditor("yyyy-MM-dd"));
}
});
User user=(User)beanFactory.getBean("user");
System.out.println("name:"+user.getUsername()+"\nbirthday:"+user.getBirthday());
}
}
来源:Heck's Blog
地址:https://www.heckjj.com/post/420/
转载时须以链接形式注明作者和原始出处及本声明,否则将追究法律责任,谢谢配合!
* 自定义属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入
spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器
* 如何定义属性编辑器?
* 继承PropertyEditorSupport类,覆写setAsText()方法(注意要将处理完成的对象通过PropertyEditorSupport的setValue设置回去)
*向IoC容器中注册自定义的属性编辑器(两种方式:1 在配置文件中注册 2 在程序中注册)
接下来我们就看看一个关于日期的属性编辑器的部署过程:
首先定义一个测试类:
比如:
有一个类里面有一个Date属性
package tk.hecks.property;
import java.util.Date;
public class User {
private String username;
private Date birthday;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
然后继承PropertyEditorSupport,实现自定义的属性编辑器,UtilDatePropertyEditor.java 如下,必须继承java.beans.PropertyEditorSupport类,覆写setAsText()方法,再将处理完的对象setValue回去。
package tk.hecks.property;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class UtilDatePropertyEditor extends PropertyEditorSupport{
private String format;
public UtilDatePropertyEditor(String format) {
this.format=format;
}
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat sdf=new SimpleDateFormat(format);
try {
Date date=sdf.parse(text);
this.setValue(date);
} catch (ParseException e) {
System.out.println(e.getMessage()+"日期的格式不对");
}
}
public void setFormat(String format) {
this.format = format;
}
}
接着我们首先定义配置文件:(采用配置方式)
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
<!--将user中的Date赋值2014-03-28,spring会认为2014-03-28是String,无法转换成Date,会报错!-->
<bean id="user" class="tk.hecks.property.User">
<property name="username" value="heck"/>
<property name="birthday" value="2014-03-28"/>
</bean>
<!-- 所以自定义的属性编辑器 -->
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.property.UtilDatePropertyEditor">
<!--干脆把format也注入,灵活处理格式-->
<property name="format" value="yyyy-MM-dd"/>
</bean>
</entry>
</map>
</property>
</bean>
</beans>
这样就可以完成正确解析了,注意customEditors是Spring的类CustomEditorConfigurer提供的属性,是一个Map,里面存放的都是自定义的编辑器(customEditors),比如这里存放的是UtilDatePropertyEditor日期编辑器,看CustomEditorConfigurer源码就知道了。
接下来我们测试一下:
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;
public class InjectionTest extends TestCase {
private BeanFactory factory;
@Override
protected void setUp() throws Exception {
factory = new ClassPathXmlApplicationContext("applicationContext.xml");
}
public void testInjection1() {
User user= (User)factory.getBean("user");
System.out.println("name:"+user.getUsername()+"\nbirthday:"+user.getBirthday());
}
}
或者用main方法来测试
package tk.hecks.property;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
public class TestPropertyEditor {
public static void main(String[] args) {
// ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext-*.xml");
// User user=(User)ac.getBean("user");
// System.out.println("name:"+user.getUsername()+"\nbirthday:"+user.getBirthday());
//它等价于
ConfigurableBeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext-beans.xml"));
CustomEditorConfigurer configurer=(CustomEditorConfigurer)beanFactory.getBean("customEditorConfigurer");
configurer.postProcessBeanFactory((ConfigurableListableBeanFactory) beanFactory);
User user=(User)beanFactory.getBean("user");
System.out.println("name:"+user.getUsername()+"\nbirthday:"+user.getBirthday());
}
}
输出结果:
name:heck
birthday:Fri Mar 28 00:00:00 CDT 2014
接下来我们在程序中注册自定义的属性编辑器:
package tk.hecks.property;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class TestPropertyEditorResgister {
public static void main(String[] args) {
ConfigurableBeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
beanFactory.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(java.util.Date.class, new UtilDatePropertyEditor("yyyy-MM-dd"));
}
});
User user=(User)beanFactory.getBean("user");
System.out.println("name:"+user.getUsername()+"\nbirthday:"+user.getBirthday());
}
}
来源:Heck's Blog
地址:https://www.heckjj.com/post/420/
转载时须以链接形式注明作者和原始出处及本声明,否则将追究法律责任,谢谢配合!
Spring注入bean
spring中bean按



