一、注解意义
主要就是为了Spring MVC来用的,提供Controller请求转发,json自动转换等功能
是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。配置一些messageconverter。即解决了@Controller注解的使用前提配置是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用。 会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean,这是Spring MVC为@Controller分发请求所必需的,并且提供了数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持读写XML的支持(JAXB)和读写JSON的支持(默认Jackson)等功能。 我们处理响应ajax请求时,就使用到了对json的支持。 对action写JUnit单元测试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,来完成测试,取的时候要知道是这一句注册的这两个bean。
/WEB-INF/content/
.jsp
一开始我在写配置的时候,只写了,并没有使用,servlet拦截*.do,.do请求可以被正确捕捉和处理。
后来为了解决静态资源访问的问题,servlet改成了拦截所有请求,即/,并添加了默认的servlet,这时候*.do请求不能被控制器捕捉了,页面错误为404。直到添加了之后,.do请求才又能被正确捕捉和处理。
当我们需要controller返回一个map的json对象时,可以设定,
同时设定 标签,设定字符集和json处理类,例如:
text/plain;charset=UTF-8
在使用SpringMVC3时,需要对response到页面的数据进行编码设置则需要自定义注解数据格式化类来对页面传过来的字符串进行格式化。
text/plain;charset=UTF-8
相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean。
所以我们在上面配置的AnnotationMethodHandlerAdapter在后是不起作用的。包括了一个默认的AnnotationMethodHandlerAdapter功能。
SpringMVC 3.1.x以上版本
text/plain;charset=UTF-8
spring mvc 3.1中annotation-driven所支持的全部配置。
[/color]
其中3.1新增部分如下 return-value-handlers 允许注册实现了HandlerMethodReturnValueHandler接口的bean,来对handler method的特定的返回类型做处理。 HandlerMethodReturnValueHandler接口中定义了两个方法 supportsReturnType 方法用来确定此实现类是否支持对应返回类型。 handleReturnValue 则用来处理具体的返回类型。 例如以下的handlerMethod
@RequestMapping("/testReturnHandlers")
public User testHandlerReturnMethod(){
User u = new User();
u.setUserName("test");
return u;
}
所返回的类型为一个pojo,正常情况下spring mvc无法解析,将转由DefaultRequestToViewNameTranslator 解析出一个缺省的view name,转到 testReturnHandlers.jsp, 我们增加以下配置
public class UserHandlers implements HandlerMethodReturnValueHandler {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public boolean supportsReturnType(MethodParameter returnType) {
Class type = returnType.getParameterType();
if(User.class.equals(type))
{
return true;
}
return false;
}
@Override
public void handleReturnValue(Object returnValue,
MethodParameter returnType, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest) throws Exception {
logger.info("handler for return type users ");
mavContainer.setViewName("helloworld");
}
}
此时再访问 http://localhost:8080/springmvc/testReturnHandlers ,将交由 UserHandlers来处理返回类型为User的返回值。
argument-resolvers 允许注册实现了WebArgumentResolver接口的bean,来对handlerMethod中的用户自定义的参数或annotation进行解析
public class MyCustomerWebArgumentHandler implements WebArgumentResolver {
@Override
public Object resolveArgument(MethodParameter methodParameter,
NativeWebRequest webRequest) throws Exception {
if (methodParameter.getParameterType().equals(MyArgument.class)) {
MyArgument argu = new MyArgument();
argu.setArgumentName("winzip");
argu.setArgumentValue("123456");
return argu;
}
return UNRESOLVED;
}
}
这里我们定义了一个 customer webArgumentHandler,当handler method中参数类型为 MyArgument时生成对参数的类型绑定操作。 注意新注册的webArgumentHandler的优先级最低,即如果系统缺省注册的ArgumentHandler已经可以解析对应的参数类型时,就不会再调用到新注册的customer ArgumentHandler了。
二、 注解
当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean
使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean:
类似地,使用@Resource、@PostConstruct、@PreDestroy等注解就必须声明 CommonAnnotationBeanPostProcessor;使用@PersistenceContext注解,就必须声明 PersistenceAnnotationBeanPostProcessor的Bean。 这样的声明未免太不优雅,而Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。如下:
另,在我们使用注解时一般都会配置扫描包路径选项:
该配置项其实也包含了自动注入上述processor的功能,因此当使用后,即可将省去。
备注: 在配置文件中使用 context 命名空间之前,必须在 元素中声明 context 命名空间。
application/json; charset=UTF-8
*; charset=UTF-8
/WEB-INF/ftl
classpath:/ftl
UTF-8
UTF-8
zh_CN
Spring 3.0.x中使用了annotation-driven后,缺省使用DefaultAnnotationHandlerMapping 来注册handler method和request的mapping关系。 AnnotationMethodHandlerAdapter来在实际调用handlermethod前对其参数进行处理。 并在dispatcherServlet中,当用户未注册自定义的ExceptionResolver时,注册AnnotationMethodHandlerExceptionResolver来对使用@ExceptionHandler标注的异常处理函数进行解析处理(这也导致当用户注册了自定义的exeptionResolver时将可能导致无法处理@ExceptionHandler)。 在spring mvc 3.1中,对应变更为 DefaultAnnotationHandlerMapping -> RequestMappingHandlerMapping AnnotationMethodHandlerAdapter -> RequestMappingHandlerAdapter AnnotationMethodHandlerExceptionResolver -> ExceptionHandlerExceptionResolver 以上都在使用了annotation-driven后自动注册。