<mvc:annotation-driven />与<context:annotation-config />

  Spring家族的配置中这两个配置的意义,说具体点其实根据标签的shecma就能看出来,mvc,主要就是为了Spring MVC来用的,提供Controller请求转发,json自动转换等功能,而context这个主要是解决spring容器的一些注解。

  从百度参考了两个帖子:

    http://blog.csdn.net/sxbjffsg163/article/details/9955511

    http://blog.sina.com.cn/s/blog_872758480100wtfh.html

  

<mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。
后面,我们处理响应ajax请求时,就使用到了对json的支持。
后面,对action写JUnit单元测试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,来完成测试,取的时候要知道是<mvc:annotation-driven />这一句注册的这两个bean。

<context:annotation-config> declares support for general annotations such as @Required@Autowired@PostConstruct, and so on.

<mvc:annotation-driven /> is actually rather pointless. It declares explicit support for annotation-driven MVC controllers (i.e.@RequestMapping@Controller, etc), even though support for those is the default behaviour.

My advice is to always declare <context:annotation-config>, but don‘t bother with <mvc:annotation-driven /> unless you want JSON support via Jackson.

在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册

AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、

PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。

注册这4个 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。

例如:

如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下

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

如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor

如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。

如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。同样,传统的声明方式如下:

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

一般来说,这些注解我们还是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供<context:annotation-config/>的简化配置方式,自动帮你完成声明。

不过,呵呵,我们使用注解一般都会配置扫描包路径选项

<context:component-scan base-package=”XX.XX”/>

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

时间: 2024-08-07 08:38:40

<mvc:annotation-driven />与<context:annotation-config />的相关文章

struts2的配置文件中的&lt;param name="allowedTypes"&gt;&lt;/param&gt;

<action name="upload" class="action.MulUpload"> <!-- 设置文件的类型 --> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/JPEG,image/JPG</param> </interceptor-ref> <

Spring MVC注解配置结合Hibernate的入门教程及其代码实例

原文:Spring MVC注解配置结合Hibernate的入门教程及其代码实例 源代码下载地址:http://www.zuidaima.com/share/1787210045197312.htm 1.概述 本文旨在搭建Spring MVC+Hibernate开发框架,通过一个简单的demo讲解Spring MVC的相关配置文件,以及通过注解方式实现简单功能. 开发框架:Spring+Spring MVC+Hibernate(Spring所用的版本为3.0.5). 数据库:MySQL(数据库名称

iOS 开发之JS与Native交互

最近项目中用到了JS与OC交互的,所以我就来讲一下JS与OC交互的详细过程,以及在做项目的时候遇到的问题,跟大家分享一下. 1:关于交互实现方式的选择. 网上讨论比较多的有一个第三方库WebViewJavascriptBridge,个人不建议用,因为本身我们在做H5交互的时候就是给前端增加了工作量,而这种处理方式就需要前端要配置两套代码,一套给安卓,一套给iOS,而且不利于调试.所以我最后选择用系统的JavaScriptCore框架,JavaScriptzCore内部有五个框架,分别是: #im

[Object]面向对象编程(自己总结版)

[Object]面向对象编程(自己总结版) 2014-06-06 面向对象编程 感受:原理javascript也可以用完全面向对象的方式编程,兴奋得一个晚上都没睡好觉. 有种越深入越想编程的状态,有种上瘾的快感. 1,面向对象方式编程 Js代码   function Aa(){ //属性设置 this.property1:value1,//例如this.nameinput = $("input['name'=name]"); this.property2:value2, this.pr

根据C# 事件思想来实现 php 事件

事件定义 当我们使用委托场景时,我们很希望有这样两个角色出现:广播者和订阅者.我们需要这两个角色来实现订阅和广播这种很常见的场景. 广播者这个角色应该有这样的功能:包括一个委托字段,通过调用委托来发出广播.而订阅者应该有这样的功能:可以通过调用 += 和 -= 来决定何时开始或停止订阅. 事件就是描述这种场景模式的一个词.事件是委托的一个子集,为了满足“广播/订阅”模式的需求而生. C#中事件简单实现 using System; namespace ConsoleApplication2 { c

在0~N个数字中,取指定个数的不重复数字,要求这些数字的和为指定值,求所有结果

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApp1 { 7 class Program { 8 static void Main(string[] args) { 9 10 // 防止出现随机值无法组合 11 while (ResDic.Count == 0) { 12 13 Con = 10; 14 // 初

Caffe源码-Solver类

Solver类简介 Net类中实现了网络的前向/反向计算和参数更新,而Solver类中则是对此进行进一步封装,包含可用于逐次训练网络的Step()函数,和用于求解网络的优化解的Solve()函数,同时还实现了一些存储.读取网络模型快照的接口函数. solver.cpp源码 template<typename Dtype> void Solver<Dtype>::SetActionFunction(ActionCallback func) { action_request_funct

jstl param url redirect import

import标签 import标签用来导入其他页面 属性: * url :引入页面的路径 * context :工程名 * var :将引入的页面保存到一个变量中 * scope :保存到一个作用域中. 代码: <c:import var="i" url="/jstl/forEach.jsp" context="/day13" scope="page"></c:import> ${ i } url标签:

jsp:&lt;c:redirect&gt; 和&lt;c:param&gt; 标签

redirect 标签使用来进行页面之间的重定向,它和传统 JSP 程序中的<jsp:redirect>标记功能相类似.param 标签是和 redirect 一起使用的,它用来进行参数值的传递.redirect 标签使用的格式如下: <c:redirect url="url" [context="context"] />在 redirect 标签体中指定 param 参数的使用格式如下: <c:redirect url="u

linux kernel with param

Linux kernel support pass param to kernel, this params can be assigned at load time by insmod or modprobe.  or later read from /etc/modprobe.conf file. There are two macro : module_param and module_param_array, To declare an param or array param ,use