3月28
Spring中autowire属性
default-autowire="x"
x有5个选择:byName,byType,constructor和autodetect,no
一、spring 自动装配 default-autowire="byName"
Service.java
public class Service
{
Source source;
public void setSource(Source source)
{
this.source = source;
}
}
default-autowire="x"
x有5个选择:byName,byType,constructor和autodetect,no
一、spring 自动装配 default-autowire="byName"
Service.java
public class Service
{
Source source;
public void setSource(Source source)
{
this.source = source;
}
}
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;
}
}
* 自定义属性编辑器,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;
}
}
3月28
Failed to convert property value of type [$Proxy13
Failed to convert property value of type [$Proxy13] to required type
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy13] to required type [tk.hecks.dao.AuthorDaoImp] for property 'authorDaoImp'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy13] to required type [tk.hecks.dao.AuthorDaoImp] for property 'authorDaoImp': no matching editors or conversion strategy found
Caused by:
org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessException details (1) are:
PropertyAccessException 1:
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy13] to required type [tk.hecks.dao.AuthorDaoImp] for property 'authorDaoImp'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy13] to required type [tk.hecks.dao.AuthorDaoImp] for property 'authorDaoImp': no matching editors or conversion strategy found
Caused by:
java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy13] to required type [tk.hecks.dao.AuthorDaoImp] for property 'authorDaoImp': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:815)
Failed to convert property value of type [$Proxy13] to required type
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy13] to required type [tk.hecks.dao.AuthorDaoImp] for property 'authorDaoImp'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy13] to required type [tk.hecks.dao.AuthorDaoImp] for property 'authorDaoImp': no matching editors or conversion strategy found
Caused by:
org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessException details (1) are:
PropertyAccessException 1:
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy13] to required type [tk.hecks.dao.AuthorDaoImp] for property 'authorDaoImp'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy13] to required type [tk.hecks.dao.AuthorDaoImp] for property 'authorDaoImp': no matching editors or conversion strategy found
Caused by:
java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy13] to required type [tk.hecks.dao.AuthorDaoImp] for property 'authorDaoImp': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:815)





