mvc:annotation-driven注解的作用

一、<mvc:annotation-driven />注解意义

主要就是为了Spring MVC来用的,提供Controller请求转发,json自动转换等功能

<mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。配置一些messageconverter。即解决了@Controller注解的使用前提配置<context:annotation-config/>是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用。 
<mvc:annotation-driven>会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean,这是Spring MVC为@Controller分发请求所必需的,并且提供了数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持读写XML的支持(JAXB)和读写JSON的支持(默认Jackson)等功能。
我们处理响应ajax请求时,就使用到了对json的支持。
对action写JUnit单元测试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,来完成测试,取的时候要知道是<mvc:annotation-driven />这一句注册的这两个bean。

<!--  spring 可以自动去扫描 base-package下面的包或子包下面的Java文件,如果扫描到有Spring的相关

注解的类,则把这些类注册为Spring的bean -->

<context:component-scan base-package="org.fkit.controller"/>

<!--设置配置方案 -->

  <mvc:annotation-driven/>

<!--使用默认的Servlet来响应静态文件-->

<mvc:default-servlet-handler/>

<!-- 视图解析器 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
<!-- 前缀 -->
<property name="prefix">
<value>/WEB-INF/content/</value>
</property>
<!-- 后缀 -->
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

一开始我在写配置的时候,只写了<context:component-scan/>,并没有使用<mvc:annotation-driven/>,servlet拦截*.do,.do请求可以被正确捕捉和处理。

后来为了解决静态资源访问的问题,servlet改成了拦截所有请求,即/,并添加了默认的servlet,这时候*.do请求不能被控制器捕捉了,页面错误为404。直到添加了<mvc:annotation-driven/>之后,.do请求才又能被正确捕捉和处理。

<context:component-scan base-package="com"></context:component-scan>  
<mvc:annotation-driven/>  
<mvc:resources mapping="/styles/**" location="/WEB-INF/resource/styles/"/>  
<mvc:default-servlet-handler/>  

当我们需要controller返回一个map的json对象时,可以设定<mvc:annotation-driven />,

同时设定<mvc:message-converters> 标签,设定字符集和json处理类,例如:

<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

在使用SpringMVC3时,需要对response到页面的数据进行编码设置则需要自定义注解数据格式化类来对页面传过来的字符串进行格式化。

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >  
        <property name="messageConverters">  
             <list>  
                 <bean class = "org.springframework.http.converter.StringHttpMessageConverter">  
                    <property name = "supportedMediaTypes">  
                         <list>  
                             <value>text/plain;charset=UTF-8</value>  
                         </list>  
                    </property>  
                 </bean>  
             </list>  
        </property>  
    </bean> 

<mvc:annotation-driven />相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean。

所以我们在上面配置的AnnotationMethodHandlerAdapter在<mvc:annotation-driven />后是不起作用的。<mvc:annotation-driven />包括了一个默认的AnnotationMethodHandlerAdapter功能。

SpringMVC 3.1.x以上版本

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">  
	  <property name="formatters">  
	      <list>  
	          <bean class="com.grgbanking.it.common.formatter.TimestampFormatAnnotationFormatterFactory"/>  
	      </list>  
	  </property>  
	</bean> 
	
	<!-- 设置转换器 -->
	<mvc:annotation-driven conversion-service="conversionService">
		<mvc:message-converters>  
        	 <bean class = "org.springframework.http.converter.StringHttpMessageConverter">  
                    <property name = "supportedMediaTypes">  
                         <list>  
                             <value>text/plain;charset=UTF-8</value>  
                         </list>  
                    </property>  
                 </bean>  
        </mvc:message-converters>
	</mvc:annotation-driven>

spring mvc 3.1中annotation-driven所支持的全部配置。

<mvc:annotation-driven  message-codes-resolver ="bean ref" validator="" conversion-service="">  
     
     <mvc:return-value-handlers>  
        <bean></bean>  
    </mvc:return-value-handlers>  
      
    <mvc:argument-resolvers>  
    </mvc:argument-resolvers>  
      
    <mvc:message-converters>  
    </mvc:message-converters>[/color]  
</mvc:annotation-driven>  

其中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, 
我们增加以下配置 

<mvc:annotation-driven validator="validator">  
    <mvc:return-value-handlers>   
           <bean  class="net.zhepu.web.handlers.returnHandler.UserHandlers"></bean>   
    </mvc:return-value-handlers>
</mvc:annotation-driven>  
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进行解析 

<mvc:annotation-driven validator="validator">  
    <mvc:argument-resolvers>  
 <bean class="net.zhepu.web.handlers.argumentHandler.MyCustomerWebArgumentHandler" />  
    </mvc:argument-resolvers>  
  
</mvc:annotation-driven> 
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了。 

二、<context:annotation-config/> 注解

当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如:
  使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>

使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean:

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

类似地,使用@Resource、@PostConstruct、@PreDestroy等注解就必须声明 CommonAnnotationBeanPostProcessor;使用@PersistenceContext注解,就必须声明 PersistenceAnnotationBeanPostProcessor的Bean。
  这样的声明未免太不优雅,而Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用<context:annotation- config/>隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。如下:

<context:annotation-config/> 

另,在我们使用注解时一般都会配置扫描包路径选项:

<context:component-scan base-package="pack.pack"/>

该配置项其实也包含了自动注入上述processor的功能,因此当使用<context:component-scan/>后,即可将<context:annotation-config/>省去。

备注:
在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
	<mvc:resources mapping="/favicon.ico" location="/favicon.ico,classpath:/favicon.ico" />
	<mvc:resources mapping="/public/**" location="/public/,classpath:/public/" />
	<mvc:resources mapping="/platform/**" location="/platform/" />
	
	<bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />
	<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>application/json; charset=UTF-8</value>
				<value>*; charset=UTF-8</value>
			</list>
		</property>
	</bean>
	<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="org.springframework.http.converter.ResourceHttpMessageConverter" />
				<ref bean="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
			</list>
		</property>
	</bean>
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
		<property name="detectHandlersInAncestorContexts" value="true" />
	</bean>
	<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPaths">
			<list>
				<value>/WEB-INF/ftl</value>
				<value>classpath:/ftl</value>
			</list>
		</property>
		<property name="preferFileSystemAccess" value="false" />
		<property name="freemarkerSettings">
			<props>
				<prop key="defaultEncoding">UTF-8</prop>
				<prop key="url_escaping_charset">UTF-8</prop>
				<prop key="locale">zh_CN</prop>
			</props>
		</property>
	</bean>
	<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="cache" value="true" />
		<property name="suffix" value=".ftl" />
		<property name="contentType" value="text/html; charset=UTF-8" />
		<property name="requestContextAttribute" value="rc"></property>
	</bean>
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
</beans>

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后自动注册。 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 55
    点赞
  • 170
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wespten

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值