<mvc:annotation-driven />

<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->

是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。

<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。

并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。

当我们需要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>

<context:annotation-config/>

使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean:

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

  类似地,使用@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/>省去。

备注:

 xmlns:context="http://www.springframework.org/schema/context"  

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

<context:component-scan base-packge="">

<!-- 启动包扫描功能,以便注册带有@Controller、@service、@repository、@Component等注解的类成为spring的bean -->
1、@controller 控制器(注入服务)
2、@service 服务(注入dao)
3、@repository dao(实现dao访问)
4、@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)


  @Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。 
下面写这个是引入component的扫描组件 
<context:component-scan base-package=”com.mmnc”>     其中base-package为需要扫描的包(含所有子包) 
       1、@Service用于标注业务层组件 
       2、@Controller用于标注控制层组件(如struts中的action) 
       3、@Repository用于标注数据访问组件,即DAO组件. 
       4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。    
           @Service public class UserServiceImpl implements UserService { } 
           @Repository public class UserDao { } getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“***”)              
       这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”) 
           @Scope(“prototype”)来改变。可以使用以下方式指定初始化方法和销毁方法(方法名任意): @PostConstruct public void init() { } 

<context:component-scan base-packge="">

<!-- 启动包扫描功能,以便注册带有@Controller、@service、@repository、@Component等注解的类成为spring的bean -->
1、@controller 控制器(注入服务)
2、@service 服务(注入dao)
3、@repository dao(实现dao访问)
4、@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)


  @Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。 
下面写这个是引入component的扫描组件 
<context:component-scan base-package=”com.mmnc”>     其中base-package为需要扫描的包(含所有子包) 
       1、@Service用于标注业务层组件 
       2、@Controller用于标注控制层组件(如struts中的action) 
       3、@Repository用于标注数据访问组件,即DAO组件. 
       4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。    
           @Service public class UserServiceImpl implements UserService { } 
           @Repository public class UserDao { } getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“***”)               这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”) 
           @Scope(“prototype”)来改变。
可以使用以下方式指定初始化方法和销毁方法(方法名任意):
@PostConstruct
public void init() {
}
@PreDestroy
public void destory() {
}

  

注入方式:

把DAO实现类注入到action的service接口(注意不要是service的实现类)中,注入时不要new 这个注入的类,因为spring会自动注入,如果手动再new的话会出现错误,
然后属性加上@Autowired后不需要getter()和setter()方法,Spring也会自动注入。

在接口前面标上@Autowired注释使得接口可以被容器注入,如:

@Autowired
@Qualifier("chinese")
private userService userService;

  当接口存在两个实现类的时候必须使用@Qualifier指定注入哪个实现类,否则可以省略,只写@Autowired。

springMVC的一些配置解析的更多相关文章

  1. SpringMVC的自动配置解析

    https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developin ...

  2. springMVC:为MultipartFilte配置了上传文件解析器,报错或不能使用

    一.问题描述为支持restful风格请求,并且应对可能上传文件的情况,需要在配置hiddenHttpMethodFilter过滤器之前配置MultipartFilter.目的是让MultipartFi ...

  3. [SpringMVC]SpringMVC学习笔记一: springmvc原理及实例解析.

    前言:今天来回顾下SpringMVC的开发原理, 使用图文并茂的方式 来解析其中的内幕, 我相信懂了其中的运行机制后, 对于面试中SpringMVC大家都可以说so easy了. 一, 图示法 第二张 ...

  4. SpringMVC框架入门配置 IDEA下搭建Maven项目

    初衷:本人初学SpringMVC的时候遇到各种稀奇古怪的问题,网上各种技术论坛上的帖子又参差不齐,难以一步到位达到配置好的效果,这里我将我配置的总结写到这里供大家初学SpringMVC的同僚们共同学习 ...

  5. SpringMVC 源码深度解析&lt;context:component-scan&gt;(扫描和注冊的注解Bean)

    我们在SpringMVC开发项目中,有的用注解和XML配置Bean,这两种都各有自己的优势,数据源配置比較经经常使用XML配置.控制层依赖的service比較经经常使用注解等(在部署时比較不会改变的) ...

  6. 学习springMVC实例1——配置和跳转到HelloWorld

    本文让大家迅速掌握springMVC的使用方法,以最简单的方式理解此框架 一.用eclipse新建一个web项目,命名为springMVC1,tomcat的端口号为9090 二.在WEB-INF目录下 ...

  7. 【Eclipse】springMVC介绍与配置

    SpringMCV介绍: Spring MVC是一种基于Java的实现了MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动的,也就是使用 ...

  8. SpringMVC简单项目配置

    一.首先,SpringMVC框架使用分层开发,分层是为了实现“高内聚,低耦合”.采用“分而治之”的思想,把问题划分开来各个解决,易于控制,延展和分配资源,最重要的是有利于后期项目维护.MVC是指Mod ...

  9. [转载]SpringBoot系列: SpringMVC 参数绑定注解解析

    本文转载自 https://www.cnblogs.com/morethink/p/8028664.html, 作者写得非常好, 致谢! SpringMVC 参数绑定注解解析   本文介绍了用于参数绑 ...

随机推荐

  1. C#-反射知识点

    1.反射是基于Sytem.Type的,里面的很多方法是system.reflection里面的 MethodInfo[] tt= t.GetType().GetMethods(); //t是strin ...

  2. deeplink技术的两篇资料

    两篇资料如下: https://zhuanlan.zhihu.com/p/20694818?refer=ouyangchen http://www.sohu.com/a/122694049_49134 ...

  3. 开源 java CMS - FreeCMS2.3会员个人资料

    原文地址:http://javaz.cn/site/javaz/site_study/info/2015/28577.html​ 项目地址:http://www.freeteam.cn/ 个人资料 从 ...

  4. linux程序设计——个人总结

    linux程序设计--个人总结 到今天为止,<linux程序设计>学习基本完毕了.从五月下旬開始接触linux,学习安装Ubuntu14.04,六月份開始学习<linux程序设计&g ...

  5. moble 设备多指手势识别 (tap , double_tap , pinch)

    function(){ elem.addEventListener('touchstart', start , false) elem.addEventListener('touchend', end ...

  6. HBase框架基础(二)

    * HBase框架基础(二) 上一节我们了解了HBase的架构原理和模块组成,这一节我们先来聊一聊HBase的读写数据的过程. * HBase的读写流程及3个机制 HBase的读数据流程: 1.HRe ...

  7. 01背包-第k优解

    The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...

  8. caffe(1) 网络结构层参数详解

    prototxt文件是caffe的配置文件,用于保存CNN的网络结构和配置信息.prototxt文件有三种,分别是deploy.prototxt,train_val.prototxt和solver.p ...

  9. [BJWC2012]冻结 分层图最短路

    昨晚飞行路线之后,这道题就应该能一眼切了 题目当然也不难,跑一遍分层图最短路即可 Code: #include<cstring> #include<algorithm> #in ...

  10. pc端如何引用日期插件

    页面的html部分 <li> <span>出生日期</span> <input type="text" placeholder=" ...