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)
3月24
一直在用Servlet却从来没有深入去了解,有一天同事问在web.xml文件中 context-param 与 init-param 有啥区别?大家都知道在
web.xml里面可以定义两种参数:
1、application范围内的参数,存放在servletcontext中,在web.xml中配置如下:
<context-param>
<param-name>context/param</param-name>
<param-value>avalible during application</param-value>
</context-param>
2、servlet范围内的参数,只能在servlet的init()方法中取得,在web.xml中配置如下:
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>tk.hecks.controller.MainServlet</servlet-class>
<init-param>
<param-name>param1</param-name>
<param-value>avalible in servlet init()</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
web.xml里面可以定义两种参数:
1、application范围内的参数,存放在servletcontext中,在web.xml中配置如下:
<context-param>
<param-name>context/param</param-name>
<param-value>avalible during application</param-value>
</context-param>
2、servlet范围内的参数,只能在servlet的init()方法中取得,在web.xml中配置如下:
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>tk.hecks.controller.MainServlet</servlet-class>
<init-param>
<param-name>param1</param-name>
<param-value>avalible in servlet init()</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
3月24
Servlet的生命周期其实分为三个阶段:
1、初始化阶段 调用init()方法
2、响应客户请求阶段 调用service()方法
3、终止阶段 调用destroy()方法
Servlet初始化阶段:
在下列时刻Servlet容器装载Servlet:
1、Servlet容器启动时自动装载某些Servlet,实现它只需要在web.XML文件中的<Servlet></Servlet>之间添加如下代码:
<loadon-startup>1</loadon-startup>
2、在Servlet容器启动后,客户首次向Servlet发送请求
3、Servlet类文件被更新后,重新装载Servlet
Servlet被装载后,Servlet容器创建一个Servlet实例并且调用Servlet的init()方法进行初始化。在Servlet的整个生命周期内,init()方法只被调用一次。
1、初始化阶段 调用init()方法
2、响应客户请求阶段 调用service()方法
3、终止阶段 调用destroy()方法
Servlet初始化阶段:
在下列时刻Servlet容器装载Servlet:
1、Servlet容器启动时自动装载某些Servlet,实现它只需要在web.XML文件中的<Servlet></Servlet>之间添加如下代码:
<loadon-startup>1</loadon-startup>
2、在Servlet容器启动后,客户首次向Servlet发送请求
3、Servlet类文件被更新后,重新装载Servlet
Servlet被装载后,Servlet容器创建一个Servlet实例并且调用Servlet的init()方法进行初始化。在Servlet的整个生命周期内,init()方法只被调用一次。
3月21
其实首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关。即不会因为 filter 写在 listener 的前面而会先加载 filter。最终得出的结论是:listener -> filter -> servlet
同时还存在着这样一种配置节:context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息,那么 context-param 配置节是不是应该写在 listener 配置节前呢?实际上 context-param 配置节可写在任意位置,因此真 正的加载顺序为:context-param -> listener -> filter -> servlet
对于某类配置节而言,与它们出现的顺序是有关的。以 filter 为例,web.xml 中当然可以定义多个 filter,与 filter 相关的一个配置节是 filter-mapping,这里一定要注意,对于拥有相同 filter-name 的 filter 和 filter-mapping 配置节而言,filter-mapping 必须出现在 filter 之后,否则当解析到 filter-mapping 时,它所对应的 filter-name 还未定义。web 容器启动时初始化每个 filter 时,是按照 filter 配置节出现的顺序来初始化的,当请求资源匹配多个 filter-mapping 时,filter 拦截资源是按照 filter-mapping 配置节出现的顺序来依次调用 doFilter() 方法的。
同时还存在着这样一种配置节:context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息,那么 context-param 配置节是不是应该写在 listener 配置节前呢?实际上 context-param 配置节可写在任意位置,因此真 正的加载顺序为:context-param -> listener -> filter -> servlet
对于某类配置节而言,与它们出现的顺序是有关的。以 filter 为例,web.xml 中当然可以定义多个 filter,与 filter 相关的一个配置节是 filter-mapping,这里一定要注意,对于拥有相同 filter-name 的 filter 和 filter-mapping 配置节而言,filter-mapping 必须出现在 filter 之后,否则当解析到 filter-mapping 时,它所对应的 filter-name 还未定义。web 容器启动时初始化每个 filter 时,是按照 filter 配置节出现的顺序来初始化的,当请求资源匹配多个 filter-mapping 时,filter 拦截资源是按照 filter-mapping 配置节出现的顺序来依次调用 doFilter() 方法的。






