Spring-MVC注解

<context:component-scan/> 
该配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用<context:component-scan base-package="xxx.xxx.xxx"/> 后,就可以将 <context:annotation-config/> 移除了。

 

<context:annotation-config/> 

隐式地向Spring容器中注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 及 equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor 
对这个结果类做个解释: 
1、如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。 
2、如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。 
3、如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。 
4、如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。 

 

<mvc:annotation-driven/> 

隐式地向Spring容器中注册RequestMappingHandlerMapping,RequestMappingHandlerAdapter,ExceptionHandler,

同上,使得我们的@RequestMapping,@RequestParam,@ExceptionHandler这些注解生效,在之前这些都得手动配置才行。

同时还可配置一些messageconverter,对Controller返回结果进行相应的系列化。

 

引发的乱码问题:

因StringHttpMessageConverter自带的是ISO-8859-1编码格式,故都会在spring-mvc.xm重新指定编码格式。

<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>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                    <property name="writeAcceptCharset" value="false" />
                </bean>
                <ref bean="GsonHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/>
            </list>
        </property>
    </bean>

而使用<mvc:annotation-driven/> 注解隐式的注册了RequestMappingHandlerMapping,其中的排序order=0,在DispatcherServlet的handlerMappings排序第一位。对应的handlerAdapters首个是RequestMappingHandlerAdapter,重新手动注入RequestMappingHandlerAdapter,这样会出现2个而非覆盖。所以只能使用<mvc:message-converters>进行指定系列化的编码格式。

<mvc:annotation-driven>
		<mvc:message-converters>
			<ref bean="StringHttpMessageConverter" />
			<ref bean="GsonHttpMessageConverter" />
		</mvc:message-converters>
	</mvc:annotation-driven>

几种常用的HandlerMapping

//扫描具体的包下使用了@Controller注解的类
<bean class=" org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter "/>



//扫描具体的包下使用了@Controller注解的类 version 3.2 Deprecated
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
   
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>


//是通过配置mappings的参数,显示的指定url和Controller关联
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <value>
                /service/**=JerseyProxyController
            </value>
        </property>
        <property name="order" value="10000"/>
    </bean>
<bean id="JerseyProxyController" class="com.controller.JerseyProxyController">



//比如:http://8800/login.do  url访问 必须以“/”开头
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/login.do" class="com.controller.HelloController">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值