尊重原创原文链接:http://blog.csdn.net/u014277445/article/details/52282697

 Spring MVC项目中通常会有二个配置文件,spring-servlet.xml和applicationContext.xml二个配置文件,通常会出现以下几个配置:

  • <context:annotation-config /> 它的作用是隐式地向 Spring 容器注册

    • AutowiredAnnotationBeanPostProcessor、
    • CommonAnnotationBeanPostProcessor、
    • PersistenceAnnotationBeanPostProcessor、
    • RequiredAnnotationBeanPostProcessor

        这4个BeanPostProcessor。其作用是如果你想在程序中使用注解,就必须先注册该注解对应的类,如下图所示:

    • 依赖的类-注解: 
      • CommonAnnotationBeanPostProcessor @Resource 、@PostConstruct、@PreDestroy
      • PersistenceAnnotationBeanPostProcessor @PersistenceContext
      • AutowiredAnnotationBeanPostProcessor @Autowired
      • RequiredAnnotationBeanPostProcessor @Required

    当然也可以自己进行注册:

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
  • 1
  • 2
  • <context:component-scan base-package="com.*" >

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

      在这里有一个比较有意思的问题,就是扫描是否需要在二个配置文件都配置一遍,我做了这么几种测试:

    • 只在applicationContext.xml中配置如下 
      <context:component-scan base-package="com.login" /> 
      启动正常,但是任何请求都不会被拦截,简而言之就是@Controller失效

    • 只在spring-servlet.xml中配置上述配置启动正常,请求也正常,但是事物失效,也就是不能进行回滚

    • 在applicationContext.xml和spring-servlet.xml中都配置上述信息 
      启动正常,请求正常,也是事物失效,不能进行回滚

    • 在applicationContext.xml中配置如下<context:component-scan base-package="com.login" />,在spring-servlet.xml中配置如下 
      <context:component-scan base-package="com.sohu.login.web" />此时启动正常,请求正常,事物也正常了。

        结论:在spring-servlet.xml中只需要扫描所有带@Controller注解的类,在applicationContext中可以扫描所有其他带有注解的类(也可以过滤掉带@Controller注解的类)。另外可以在利用context:include-filter和context:exclude-filter 过滤不需要扫描的注解。

  • <mvc:annotation-driven />

      它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter

【Spring和SpringMVC】自动扫描注解类的问题的更多相关文章

  1. 解决Spring和SpringMVC扫描注解类的冲突问题

    原文地址:https://blog.csdn.net/xiaobao5214/article/details/52042041 最正确的配置方式:在主容器中applicationContext.xml ...

  2. Spring MVC常用的注解类

    一.注解类配置 要使用springmvc的注解类,需要在springmvc.xml配置文件中用context:component-scan/扫描:  二.五大重要的注解类 1.RequestMapp ...

  3. Spring 和 SpringMVC 常用注解和配置(@Autowired、@Resource、@Component、@Repository、@Service、@Controller的区别)

    Spring 常用注解 总结内容 一.Spring部分 1.声明bean的注解 2.注入bean的注解 3.java配置类相关注解 4.切面(AOP)相关注解 5.事务注解 6.@Bean的属性支持 ...

  4. Spring自动扫描注解类的冲突问题

    原文地址:http://www.blogjava.net/crazycy/archive/2014/07/12/415738.html Spring MVC项目中通常会有二个配置文件,spring-s ...

  5. 【Spring】SpringMVC非注解配置的两种方式

    目录结构: contents structure [+] SpringMVC是什么 Spring MVC的设计原理 SpringMVC配置的第一种方式 1,复制Jar包 2,Web.xml文件 3,M ...

  6. 【实用小技巧】spring与springmvc自动扫描包重复的问题解决

    spring对应配置文件为: <!-- 配置自动扫描的包,此时要排除Controller --> <context:component-scan base-package=" ...

  7. Spring和SpringMVC的常用注解

    Spring的部分: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component-scan base-package=" ...

  8. 注解和注释以及Spring和SpringMVC常用的注解

    1.两者区别 注解 :参与代码编译,以@开头的.它是给应用程序看的,单独使用注解毫无意义,一定要跟工具一起使用,这个所谓的工具实际就是能读懂注解的应用程序 注释 :对代码没有影响.对代码起到解释.说明 ...

  9. spring ,springmvc的常用标签注解

    一:spring常用的注解: @Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean.@Scope注解 作用域@Lazy ...

随机推荐

  1. 8593 最大覆盖问题 two pointer

    8593 最大覆盖问题 时间限制:50MS  内存限制:1000K提交次数:193 通过次数:88 题型: 编程题   语言: G++;GCC;VC Description 输入格式 第1行是正整数n ...

  2. Mysql 事务隔离级别(图文详解)

    本文由 SnailClimb 和 BugSpeak 共同完成. 事务隔离级别(图文详解) 什么是事务? 事物的特性(ACID) 并发事务带来的问题 事务隔离级别 实际情况演示 脏读(读未提交) 避免脏 ...

  3. hdu4553约会安排(线段树区间合并)

    链接 poj3667的加强版 当时的题解 这里只不过对于女神需要另开算,DS的占用的时间不加在女神身上,女神的时间都要加,清空的时候也都要算. #include <iostream> #i ...

  4. 在webconfig放置固定值

    通常的,为了布置到服务器后修改的方便通常把一些会改变的值放在webconfig: 首先在web.ocnfig中放入如下值 <appSettings> <add key="A ...

  5. 《Head First HTML与CSS》项目实践中学到的东西

    1.组织的重要性. 首先是要建立两个根文件夹,一个存上线页面的资源,一个存测试页面的资源.所有改动内容都在测试页面的文件夹中进行,在这个文件夹中进行测试.W3C语法检测后(HTML检测网站:https ...

  6. Android Studio 升级到3.0后出现编译错误\.gradle\caches\transforms-1\files-1.1\*****-release.aar

    Android Studio 升级到3.0后出现各种编译问题,其中有一个问题是关于资源找不到的问题,百度了半天,也没有相关的文章 C:\Users.gradle\caches\transforms-1 ...

  7. 基于Myeclipse的三大框架(SSH)整合

    文中主要基于Myeclipse进行配置,配置流程为:Hibernate --> Spring --> 整合 --> struts2 -->整合.注意:在此文中,主要讲述基于注解 ...

  8. linux各文件夹的作用(转)

    转自:http://www.cnblogs.com/amboyna/archive/2008/02/16/1070474.html linux下的文件结构,看看每个文件夹都是干吗用的/bin 二进制可 ...

  9. linux 删除文件后空间没有释放的解决办法

    清空没用的文件,当我删除文件后,发现可用空间沒有变化 os:centos4.7 现象: 发现当前磁盘空间使用情况: [root@ticketb ~]# df -hFilesystem          ...

  10. windows下jdk环境变量配置

    JAVA_HOMEC:\Program Files\Java\jdk1.8.0_131 JMETER_HOMEC:\jmeter\jmeter3.2 CLASSPATH%JAVA_HOME%\lib; ...