- 浏览: 145502 次
- 来自: ...
最新评论
-
安铁辉:
亲,问题解决了吗,我也遇到类似的问题,我是想cygwin+ma ...
nutch1.0 cygwin eclipse问题 -
bk41:
不明白···
Spring中的destroy-method="close"- - -
蓝月儿:
嗯 如果把得到的数据再放到一个列表显示在界面上呢,该怎么放置呢 ...
用SAX API解析XML文档为Java对象 -
zhaotao_king:
没看明白
Spring中的destroy-method="close"- - -
coffeesweet:
java.beans确实应该好好看看,了解一下,楼主的例子很好 ...
关于Spring属性编辑器详解
最近刚在研究Spring的编辑器,发现很有意思,刚好galaxystar起了一个这样贴,我想对PropertyEditor作一个详细的整理会对大家有益,特定启了这个新帖。
所谓的PropertyEditor,顾名思义,就是属性编辑器。由于Bean属性通过配置文档以字符串了方式为属性赋值,所以必须有一个“东东”负责将这个字符串转换为属性的直接对象,如属性的类型为int,那么编辑器要做的工作就是int i = Integer.parseInt("1");
Spring为一般的属性类型提供了默认的编辑器,BeanWrapperImpl是Spring框架中重要的类,它负责对注入的Bean进行包装化的管理,常见属性类型对应的编辑器即在该类中通过以下代码定义:
代码
private void registerDefaultEditors()
{
// Simple editors, without parameterization capabilities.
// The JDK does not contain a default editor for any of these target types.
this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor());
this.defaultEditors.put(Class.class, new ClassEditor());
this.defaultEditors.put(File.class, new FileEditor());
this.defaultEditors.put(InputStream.class, new InputStreamEditor());
this.defaultEditors.put(Locale.class, new LocaleEditor());
this.defaultEditors.put(Properties.class, new PropertiesEditor());
this.defaultEditors.put(Resource[].class,
new ResourceArrayPropertyEditor());
this.defaultEditors.put(String[].class, new StringArrayPropertyEditor());
this.defaultEditors.put(URL.class, new URLEditor());
// Default instances of collection editors.
// Can be overridden by registering custom instances of those as custom editors.
this.defaultEditors.put(Collection.class,
new CustomCollectionEditor(Collection.class));
this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class));
this.defaultEditors.put(SortedSet.class,
new CustomCollectionEditor(SortedSet.class));
this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class));
// Default instances of character and boolean editors.
// Can be overridden by registering custom instances of those as custom editors.
PropertyEditor characterEditor = new CharacterEditor(false);
PropertyEditor booleanEditor = new CustomBooleanEditor(false);
// The JDK does not contain a default editor for char!
this.defaultEditors.put(char.class, characterEditor);
this.defaultEditors.put(Character.class, characterEditor);
// Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor.
this.defaultEditors.put(boolean.class, booleanEditor);
this.defaultEditors.put(Boolean.class, booleanEditor);
// The JDK does not contain default editors for number wrapper types!
// Override JDK primitive number editors with our own CustomNumberEditor.
PropertyEditor byteEditor = new CustomNumberEditor(Byte.class, false);
PropertyEditor shortEditor = new CustomNumberEditor(Short.class, false);
PropertyEditor integerEditor = new CustomNumberEditor(Integer.class, false);
PropertyEditor longEditor = new CustomNumberEditor(Long.class, false);
PropertyEditor floatEditor = new CustomNumberEditor(Float.class, false);
PropertyEditor doubleEditor = new CustomNumberEditor(Double.class, false);
this.defaultEditors.put(byte.class, byteEditor);
this.defaultEditors.put(Byte.class, byteEditor);
this.defaultEditors.put(short.class, shortEditor);
this.defaultEditors.put(Short.class, shortEditor);
this.defaultEditors.put(int.class, integerEditor);
this.defaultEditors.put(Integer.class, integerEditor);
this.defaultEditors.put(long.class, longEditor);
this.defaultEditors.put(Long.class, longEditor);
this.defaultEditors.put(float.class, floatEditor);
this.defaultEditors.put(Float.class, floatEditor);
this.defaultEditors.put(double.class, doubleEditor);
this.defaultEditors.put(Double.class, doubleEditor);
this.defaultEditors.put(BigDecimal.class,
new CustomNumberEditor(BigDecimal.class, false));
this.defaultEditors.put(BigInteger.class,
new CustomNumberEditor(BigInteger.class, false));
}
但是,并非Bean的属性都是这些常见的类型,如果你的Bean需要注入一个自定义类型的属性,而又想享受IoC的好处,那么就只得自己开干,提供一个自定义的PropertyEditor了。
下面,分几个步骤来说明,定义一个自定义PropertyEditor的过程。
1)首先,碰到的问题即是,要如何编辑自己的PropertyEditor,其实需要了解一点java.beans包的知识,在该包中,有一个java.beans.PropertyEditor的接口,它定义了一套接口方法(12个),即通过这些方法如何将一个String变成内部的一个对象,这两个方法是比较重要的:
a)setValue(Object value) 直接设置一个对象,一般不直接用该方法设置属性对象
b)setAsText(String text) 通过一个字符串来构造对象,一般在此方法中解析字符串,将构造一个
类对象,调用setValue(Object)来完成属性对象设置操作。
2)实现所有的接口方法是麻烦的,java.beans.PropertyEditorSupport 适时登场,一般情况下,我们通过扩展这个方便类即可。
3)编写完后,就是在Spring配置文件中注册该属性类型编辑器的问题,Spring提供了专门的注册工具类
org.springframework.beans.factory.config.CustomEditorConfigurer,它负责将属性类型和
属性编辑器关联起来。到时BeanFactory注入Bean的属性时,即会在注册表中查找属性类型对应的编辑器。
下面给出一个小例子,例子先作一个简单描述:
1)Person 需要进行属性注入的Bean,有两个属性 一个是name,一个是address Address是一个类
2)Address Person的属性类型,本身有3个属性。
3)AddressPropertyEditor Address类型对应的属性编辑器。
开工:
1.Person.java
代码
package com.stamen.propedit;
import org.apache.commons.lang.builder.ToStringBuilder;
public class Person {
private String name;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
2.Address.java
代码
package com.stamen.propedit;
import org.apache.commons.lang.builder.ToStringBuilder;
public class Address {
private String street;
private String doorNum;
private String postCode;
public String getDoorNum() {
return doorNum;
}
public void setDoorNum(String doorNum) {
this.doorNum = doorNum;
}
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this.postCode = postCode;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
AddressPropertyEditor.java
代码
package com.stamen.propedit;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import org.springframework.util.StringUtils;
public class AddressPropertyEditor extends PropertyEditorSupport{
//支持的格式为 streeValue,doorNumValue,postCode
public void setAsText(String text)
{
System.out.println("使用自己的编辑器。");
if (text == null || !StringUtils.hasText(text)) {
throw new IllegalArgumentException("老大,不能为空啊!");
}
else
{
String[] strArr = StringUtils.tokenizeToStringArray(text,",");
Address add = new Address();
add.setStreet(strArr[0]);
add.setDoorNum(strArr[1]);
add.setPostCode(strArr[2]);
setValue(add);
}
}
public String getAsText()
{
Address add = (Address)getValue();
return ""+add;
}
}
打开Spring配置文件,添上这两个配置项:
代码
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.stamen.propedit.Address"> <!-- 属性类型 -->
<bean class="com.stamen.propedit.AddressPropertyEditor"/> <!--对应Address的编辑器 -->
</entry>
</map>
</property>
</bean>
<bean id="person" class="com.stamen.propedit.Person">
<property name="name" value="Tom"/>
<property name="address" value="朝阳区,Soho 1601,010101"/>
</bean>
代码有:
package PropertyEditors;
/**
* @author Administrator
*
*/
public class MyExpectType {
private String text;
/**
* @return text
*/
public String getText() {
return text;
}
/**
* @param text 要设置的 text
*/
public MyExpectType(String text) {
this.text = text;
}
}
/******************************************/
package PropertyEditors;
import java.beans.PropertyEditorSupport;
/**
* @author Administrator
*
*/
public class MyExpectTypeEditor extends PropertyEditorSupport{
private String format;
public void setFormat(String format) {
this.format = format;
}
public void setAsTest(String text){
//System.out.println("修改前类型:"+text.getClass().getSimpleName());
if(format != null || format.equals("upperCase"))
text = text.toUpperCase();
MyExpectType myType = new MyExpectType(text);
setValue(myType);
}
}
/********************************************/
package PropertyEditors;
/**
* @author Administrator
*
*/
public class DependsOnExpectType {
private MyExpectType myType;
/**
* @return myType
*/
public MyExpectType getMyType() {
return myType;
}
/**
* @param myType 要设置的 myType
*/
public void setMyType(MyExpectType myType) {
this.myType = myType;
}
}
/***************************************/
package PropertyEditors;
import org.springframework.beans.factory.BeanFactory;
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.support.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
* @author Administrator
*
*/
public class MyEditorUsage {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO 自动生成方法存根
//String path[] = new String[]{"pEditor.xml","plugins.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext("plugins.xml");
DependsOnExpectType someBean = (DependsOnExpectType)ctx.getBean("someBean");
System.out.println("修改后类型:"+someBean .getClass().getSimpleName());
System.out.println("修改效果:"+someBean.getMyType().getText());
}
}
/********************XML文件*******************/
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEANS//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="customEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="PropertyEditors.MyExpectType">
<bean class="PropertyEditors.MyExpectTypeEditor">
<property name="format">
<value>upperCase</value>
</property>
</bean>
</entry>
</map>
</property>
</bean>
<bean id="someBean"
class="PropertyEditors.DependsOnExpectType">
<property name="myType" value="fangbiao"/>
</bean>
</beans>
所谓的PropertyEditor,顾名思义,就是属性编辑器。由于Bean属性通过配置文档以字符串了方式为属性赋值,所以必须有一个“东东”负责将这个字符串转换为属性的直接对象,如属性的类型为int,那么编辑器要做的工作就是int i = Integer.parseInt("1");
Spring为一般的属性类型提供了默认的编辑器,BeanWrapperImpl是Spring框架中重要的类,它负责对注入的Bean进行包装化的管理,常见属性类型对应的编辑器即在该类中通过以下代码定义:
代码
private void registerDefaultEditors()
{
// Simple editors, without parameterization capabilities.
// The JDK does not contain a default editor for any of these target types.
this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor());
this.defaultEditors.put(Class.class, new ClassEditor());
this.defaultEditors.put(File.class, new FileEditor());
this.defaultEditors.put(InputStream.class, new InputStreamEditor());
this.defaultEditors.put(Locale.class, new LocaleEditor());
this.defaultEditors.put(Properties.class, new PropertiesEditor());
this.defaultEditors.put(Resource[].class,
new ResourceArrayPropertyEditor());
this.defaultEditors.put(String[].class, new StringArrayPropertyEditor());
this.defaultEditors.put(URL.class, new URLEditor());
// Default instances of collection editors.
// Can be overridden by registering custom instances of those as custom editors.
this.defaultEditors.put(Collection.class,
new CustomCollectionEditor(Collection.class));
this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class));
this.defaultEditors.put(SortedSet.class,
new CustomCollectionEditor(SortedSet.class));
this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class));
// Default instances of character and boolean editors.
// Can be overridden by registering custom instances of those as custom editors.
PropertyEditor characterEditor = new CharacterEditor(false);
PropertyEditor booleanEditor = new CustomBooleanEditor(false);
// The JDK does not contain a default editor for char!
this.defaultEditors.put(char.class, characterEditor);
this.defaultEditors.put(Character.class, characterEditor);
// Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor.
this.defaultEditors.put(boolean.class, booleanEditor);
this.defaultEditors.put(Boolean.class, booleanEditor);
// The JDK does not contain default editors for number wrapper types!
// Override JDK primitive number editors with our own CustomNumberEditor.
PropertyEditor byteEditor = new CustomNumberEditor(Byte.class, false);
PropertyEditor shortEditor = new CustomNumberEditor(Short.class, false);
PropertyEditor integerEditor = new CustomNumberEditor(Integer.class, false);
PropertyEditor longEditor = new CustomNumberEditor(Long.class, false);
PropertyEditor floatEditor = new CustomNumberEditor(Float.class, false);
PropertyEditor doubleEditor = new CustomNumberEditor(Double.class, false);
this.defaultEditors.put(byte.class, byteEditor);
this.defaultEditors.put(Byte.class, byteEditor);
this.defaultEditors.put(short.class, shortEditor);
this.defaultEditors.put(Short.class, shortEditor);
this.defaultEditors.put(int.class, integerEditor);
this.defaultEditors.put(Integer.class, integerEditor);
this.defaultEditors.put(long.class, longEditor);
this.defaultEditors.put(Long.class, longEditor);
this.defaultEditors.put(float.class, floatEditor);
this.defaultEditors.put(Float.class, floatEditor);
this.defaultEditors.put(double.class, doubleEditor);
this.defaultEditors.put(Double.class, doubleEditor);
this.defaultEditors.put(BigDecimal.class,
new CustomNumberEditor(BigDecimal.class, false));
this.defaultEditors.put(BigInteger.class,
new CustomNumberEditor(BigInteger.class, false));
}
但是,并非Bean的属性都是这些常见的类型,如果你的Bean需要注入一个自定义类型的属性,而又想享受IoC的好处,那么就只得自己开干,提供一个自定义的PropertyEditor了。
下面,分几个步骤来说明,定义一个自定义PropertyEditor的过程。
1)首先,碰到的问题即是,要如何编辑自己的PropertyEditor,其实需要了解一点java.beans包的知识,在该包中,有一个java.beans.PropertyEditor的接口,它定义了一套接口方法(12个),即通过这些方法如何将一个String变成内部的一个对象,这两个方法是比较重要的:
a)setValue(Object value) 直接设置一个对象,一般不直接用该方法设置属性对象
b)setAsText(String text) 通过一个字符串来构造对象,一般在此方法中解析字符串,将构造一个
类对象,调用setValue(Object)来完成属性对象设置操作。
2)实现所有的接口方法是麻烦的,java.beans.PropertyEditorSupport 适时登场,一般情况下,我们通过扩展这个方便类即可。
3)编写完后,就是在Spring配置文件中注册该属性类型编辑器的问题,Spring提供了专门的注册工具类
org.springframework.beans.factory.config.CustomEditorConfigurer,它负责将属性类型和
属性编辑器关联起来。到时BeanFactory注入Bean的属性时,即会在注册表中查找属性类型对应的编辑器。
下面给出一个小例子,例子先作一个简单描述:
1)Person 需要进行属性注入的Bean,有两个属性 一个是name,一个是address Address是一个类
2)Address Person的属性类型,本身有3个属性。
3)AddressPropertyEditor Address类型对应的属性编辑器。
开工:
1.Person.java
代码
package com.stamen.propedit;
import org.apache.commons.lang.builder.ToStringBuilder;
public class Person {
private String name;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
2.Address.java
代码
package com.stamen.propedit;
import org.apache.commons.lang.builder.ToStringBuilder;
public class Address {
private String street;
private String doorNum;
private String postCode;
public String getDoorNum() {
return doorNum;
}
public void setDoorNum(String doorNum) {
this.doorNum = doorNum;
}
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this.postCode = postCode;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
AddressPropertyEditor.java
代码
package com.stamen.propedit;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import org.springframework.util.StringUtils;
public class AddressPropertyEditor extends PropertyEditorSupport{
//支持的格式为 streeValue,doorNumValue,postCode
public void setAsText(String text)
{
System.out.println("使用自己的编辑器。");
if (text == null || !StringUtils.hasText(text)) {
throw new IllegalArgumentException("老大,不能为空啊!");
}
else
{
String[] strArr = StringUtils.tokenizeToStringArray(text,",");
Address add = new Address();
add.setStreet(strArr[0]);
add.setDoorNum(strArr[1]);
add.setPostCode(strArr[2]);
setValue(add);
}
}
public String getAsText()
{
Address add = (Address)getValue();
return ""+add;
}
}
打开Spring配置文件,添上这两个配置项:
代码
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.stamen.propedit.Address"> <!-- 属性类型 -->
<bean class="com.stamen.propedit.AddressPropertyEditor"/> <!--对应Address的编辑器 -->
</entry>
</map>
</property>
</bean>
<bean id="person" class="com.stamen.propedit.Person">
<property name="name" value="Tom"/>
<property name="address" value="朝阳区,Soho 1601,010101"/>
</bean>
评论
3 楼
coffeesweet
2010-05-27
java.beans确实应该好好看看,了解一下,楼主的例子很好,谢谢了
2 楼
fangbiao23
2007-06-21
代码有:
package PropertyEditors;
/**
* @author Administrator
*
*/
public class MyExpectType {
private String text;
/**
* @return text
*/
public String getText() {
return text;
}
/**
* @param text 要设置的 text
*/
public MyExpectType(String text) {
this.text = text;
}
}
/******************************************/
package PropertyEditors;
import java.beans.PropertyEditorSupport;
/**
* @author Administrator
*
*/
public class MyExpectTypeEditor extends PropertyEditorSupport{
private String format;
public void setFormat(String format) {
this.format = format;
}
public void setAsTest(String text){
//System.out.println("修改前类型:"+text.getClass().getSimpleName());
if(format != null || format.equals("upperCase"))
text = text.toUpperCase();
MyExpectType myType = new MyExpectType(text);
setValue(myType);
}
}
/********************************************/
package PropertyEditors;
/**
* @author Administrator
*
*/
public class DependsOnExpectType {
private MyExpectType myType;
/**
* @return myType
*/
public MyExpectType getMyType() {
return myType;
}
/**
* @param myType 要设置的 myType
*/
public void setMyType(MyExpectType myType) {
this.myType = myType;
}
}
/***************************************/
package PropertyEditors;
import org.springframework.beans.factory.BeanFactory;
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.support.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
* @author Administrator
*
*/
public class MyEditorUsage {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO 自动生成方法存根
//String path[] = new String[]{"pEditor.xml","plugins.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext("plugins.xml");
DependsOnExpectType someBean = (DependsOnExpectType)ctx.getBean("someBean");
System.out.println("修改后类型:"+someBean .getClass().getSimpleName());
System.out.println("修改效果:"+someBean.getMyType().getText());
}
}
/********************XML文件*******************/
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEANS//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="customEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="PropertyEditors.MyExpectType">
<bean class="PropertyEditors.MyExpectTypeEditor">
<property name="format">
<value>upperCase</value>
</property>
</bean>
</entry>
</map>
</property>
</bean>
<bean id="someBean"
class="PropertyEditors.DependsOnExpectType">
<property name="myType" value="fangbiao"/>
</bean>
</beans>
1 楼
fangbiao23
2007-06-21
我也做了个相应的例子,但就总是报错:
错误信息为:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someBean' defined in class path resource [plugins.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [PropertyEditors.MyExpectType] for property 'myType'; nested exception is java.lang.IllegalArgumentException: fangbiao
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessException details (1) are:
PropertyAccessException 1:
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [PropertyEditors.MyExpectType] for property 'myType'; nested exception is java.lang.IllegalArgumentException: fangbiao
Caused by: java.lang.IllegalArgumentException: fangbiao
at java.beans.PropertyEditorSupport.setAsText(PropertyEditorSupport.java:166)
at org.springframework.beans.TypeConverterDelegate.convertValue(TypeConverterDelegate.java:268)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:189)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:139)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:772)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:606)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValue(AbstractPropertyAccessor.java:49)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:74)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:57)
at org.springframework.beans.factory.support.AbstractBeanFactory.applyPropertyValues(AbstractBeanFactory.java:836)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1026)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:809)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:425)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:250)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:247)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:161)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:273)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:346)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:92)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:77)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:68)
at PropertyEditors.MyEditorUsage.main(MyEditorUsage.java:25)
错误信息为:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someBean' defined in class path resource [plugins.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [PropertyEditors.MyExpectType] for property 'myType'; nested exception is java.lang.IllegalArgumentException: fangbiao
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessException details (1) are:
PropertyAccessException 1:
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [PropertyEditors.MyExpectType] for property 'myType'; nested exception is java.lang.IllegalArgumentException: fangbiao
Caused by: java.lang.IllegalArgumentException: fangbiao
at java.beans.PropertyEditorSupport.setAsText(PropertyEditorSupport.java:166)
at org.springframework.beans.TypeConverterDelegate.convertValue(TypeConverterDelegate.java:268)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:189)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:139)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:772)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:606)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValue(AbstractPropertyAccessor.java:49)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:74)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:57)
at org.springframework.beans.factory.support.AbstractBeanFactory.applyPropertyValues(AbstractBeanFactory.java:836)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1026)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:809)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:425)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:250)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:247)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:161)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:273)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:346)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:92)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:77)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:68)
at PropertyEditors.MyEditorUsage.main(MyEditorUsage.java:25)
相关推荐
### Spring自定义编辑器详解 #### 一、引言 在Spring框架中,自定义编辑器主要用于处理数据类型的转换问题,特别是在将用户输入的数据转换为Java对象时非常有用。例如,用户通过表单提交了一个日期格式的字符串,...
Spring提供了一种名为属性编辑器(Property Editor)的机制,用于将配置文件中的字符串转换为对应的Java对象。这里我们自定义了一个`Date`类型的属性编辑器`DatePropertyEditor`: ```java import java.beans....
- **Spring属性编辑器**:介绍如何使用Spring的属性编辑器来实现更为灵活的数据类型转换。 - **使用外部属性文件**:探讨如何利用外部属性文件来管理应用程序的配置信息,特别是加密属性文件的使用技巧。 - **国际化...
在"Struts 2+Hibernate+Spring整合开发技术详解"这本书中,作者蒲子明深入浅出地讲解了这三大框架的整合使用。随书源码包含第19至21章的内容,主要涉及的是实际项目中的两个应用场景:BBS论坛系统和Photo System照片...
**UEditor 在线编辑器详解** UEditor 是百度推出的一款开源的富文本在线编辑器,它为用户提供了丰富的文本编辑功能,包括文字样式、图片上传、表格操作、代码高亮等,广泛应用于网站内容管理、博客写作、论坛发帖等...
此外,它还支持属性编辑器,用于在不同数据类型之间进行转换,以及属性值的自动检测和验证。 3. **org.springframework.web.servlet-3.0.5.RELEASE.jar**:这个模块是Spring MVC(Model-View-Controller)的基础,...
Spring中WebDataBinder使用详解 Spring中WebDataBinder是Spring MVC框架中的一种数据绑定机制,用于将Web请求参数绑定到JavaBean的属性上。它通过在控制器类中使用@InitBinder注解来配置Web数据绑定。 ...
5. 校验,数据绑定,BeanWrapper,与属性编辑器 5.1. 简介 5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的...
**JSP在线图文编辑器详解** 在Web开发中,为了提供用户友好的界面来编辑文本内容,常常会使用到在线的图文编辑器。"fck-demo"是一个专门为JSP(JavaServer Pages)平台设计的在线文本编辑器,它极大地简化了用户在...
- 返回到Eclipse主界面,在编辑器中打开Spring XML文件。 - 在XML文件的上下文菜单中选择`Open With` -> `XML Editor` -> `Source Mode`,以便在源代码模式下编辑XML文件。 - 这样做可以确保Eclipse能够正确识别...
**FCK编辑器源码工程详解** FCKeditor是一款经典的开源Web富文本编辑器,它在Web开发领域中被广泛使用,特别是在需要提供用户友好、类似桌面应用的文本编辑体验时。这款编辑器允许用户在网页上进行文字格式化、插入...
总的来说,Spring MVC提供了一种灵活且强大的方式来组织和控制Web应用的流程,包括模型、视图和控制器的分离,以及依赖注入等特性,使开发者可以更专注于业务逻辑,而不是底层的HTTP请求处理。在实际开发中,还可以...
5. 校验,数据绑定,BeanWrapper,与属性编辑器 5.1. 简介 5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建...
- **属性编辑器** (Property Editor): 用于转换不可直接转换的数据类型,如将字符串转换为日期等。 - **数据绑定** (Data Binding): 将 HTTP 请求中的参数与 Java 对象属性之间建立映射关系。 - **BeanWrapper**: -...
- **表单集成**:模板中的表单可以直接与后端实体对象进行绑定,包括属性编辑器、转换服务和验证错误处理等功能。 - **国际化消息显示**:从Spring管理的消息文件中获取国际化消息,并在模板中展示。 - **资源解析...
5.2.2 Spring默认属性编辑器 5.2.3 自定义属性编辑器 5.3 使用外部属性文件 5.3.1 使用外部属性文件 5.3.2 使用加密的属性文件 5.3.3 属性文件自身的引用 5.4 引用Bean的属性值 5.5 国际化信息 5.5.1 基础知识 5.5.2...
- 专门的XML编辑器支持Spring配置文件,如`application.xml`和`beans.xml`,提供语法高亮、错误检测和自动格式化。 6. **Spring Boot Dashboard** - STS内嵌了Spring Boot Dashboard,用于管理和运行Spring Boot...
7. **util**:包含了一些通用的工具类和辅助功能,如集合工厂、属性编辑器等。Schema文件中可能包含一些辅助配置元素,如列表、映射等。 8. **jdbc**:提供了一组模板类和JDBC抽象,以简化数据库访问。Schema文件...
5. 校验,数据绑定,BeanWrapper,与属性编辑器 5.1. 简介 5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建...