【Spring和SpringMVC】自动扫描注解类的问题
尊重原创原文链接: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】自动扫描注解类的问题的更多相关文章
- 解决Spring和SpringMVC扫描注解类的冲突问题
原文地址:https://blog.csdn.net/xiaobao5214/article/details/52042041 最正确的配置方式:在主容器中applicationContext.xml ...
- Spring MVC常用的注解类
一.注解类配置 要使用springmvc的注解类,需要在springmvc.xml配置文件中用context:component-scan/扫描:  二.五大重要的注解类 1.RequestMapp ...
- Spring 和 SpringMVC 常用注解和配置(@Autowired、@Resource、@Component、@Repository、@Service、@Controller的区别)
Spring 常用注解 总结内容 一.Spring部分 1.声明bean的注解 2.注入bean的注解 3.java配置类相关注解 4.切面(AOP)相关注解 5.事务注解 6.@Bean的属性支持 ...
- Spring自动扫描注解类的冲突问题
原文地址:http://www.blogjava.net/crazycy/archive/2014/07/12/415738.html Spring MVC项目中通常会有二个配置文件,spring-s ...
- 【Spring】SpringMVC非注解配置的两种方式
目录结构: contents structure [+] SpringMVC是什么 Spring MVC的设计原理 SpringMVC配置的第一种方式 1,复制Jar包 2,Web.xml文件 3,M ...
- 【实用小技巧】spring与springmvc自动扫描包重复的问题解决
spring对应配置文件为: <!-- 配置自动扫描的包,此时要排除Controller --> <context:component-scan base-package=" ...
- Spring和SpringMVC的常用注解
Spring的部分: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component-scan base-package=" ...
- 注解和注释以及Spring和SpringMVC常用的注解
1.两者区别 注解 :参与代码编译,以@开头的.它是给应用程序看的,单独使用注解毫无意义,一定要跟工具一起使用,这个所谓的工具实际就是能读懂注解的应用程序 注释 :对代码没有影响.对代码起到解释.说明 ...
- spring ,springmvc的常用标签注解
一:spring常用的注解: @Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean.@Scope注解 作用域@Lazy ...
随机推荐
- 103 Binary Tree Zigzag Level Order Traversal 二叉树的锯齿形层次遍历
给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行).例如:给定二叉树 [3,9,20,null,null,15,7], 3 ...
- C8051系列
C8051F500处理器按AEC-Q100测试标准设计具有寛工作电压(1.8~5.5V).寛工作温度范围(-40~125).抗干扰能力强并内置CAN2.0B及LIN2.1总线控制器,非常适合汽车电子及 ...
- JavaMailSender怎么发送163和qq邮件
https://blog.csdn.net/Tracycater/article/details/73441010 引入Maven依赖包 <dependency> <groupId& ...
- jvm内存溢出的三种情况以及解决办法
1 前言相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各类问题经验的积累以及对问题根源的探索,终于有了一个比较深 ...
- IE浏览器兼容background-size
background-size是CSS3新增的属性,IE8以下不支持,通过滤镜实现background-size效果 background-size:contain; // 缩小图片来适应元素的尺寸( ...
- Android笔记--Bitmap(三) 针对不用Android版本的位图管理
Bitmap(三) | Android不同版本的相应操作 在不同的Android版本中.位图的存储方式是不同的. 1.小于等于 Android 2.2 (API level 8) 垃圾收集器回收内存时 ...
- mount_cd9660:/dev/acd0: Input/output error
mount -t cd9660 /dev/acd0 /cdrom g_vfs_done():acd0[READ(offset32768, length=204]error =5 mount_cd966 ...
- JavaEE汇总
1.简述Spring. a) Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,其目的是解决企业应用开发的复杂性,能够使用基本的JavaBean代替EJB,并提供了 ...
- java代码(生成日历时间)
package test; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; p ...
- Hermite 矩阵的特征值不等式
将要学习 关于 Hermite 矩阵的特征值不等式. Weyl 定理 以及推论. Weyl 定理 Hermann Weyl 的如下定理是大量不等式的基础,这些不等式要么涉及两个 Hermite 矩 ...