【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 ...
随机推荐
- c/c++学习系列之内存对齐
1.C++内存对齐规则 每个特定平台上的编译器都有自己的默认“对齐系数”(也叫对齐模数).程序员可以通过预编译命令#pragma pack(n),n=1,2,4,8,16来改变这一系数,其中的n就是你 ...
- arcgis【0基础 】【1】 中如何添加MXD
1,第一种方法 MapControl 直接添加 if (!axMapControl1.CheckMxFile(FileName)) { MessageBox.Show("文件不合法&quo ...
- Mvc 多级控制器 路由重写 及 多级Views目录 的寻找视图的规则
1.那么我们再来看我们需要的访问方式,如下图 如果我们要访问Admin下的TestController里面的Index页面,那么我们输入Test/Index,这个肯定不行的.因为TestControl ...
- mvc 连接数据库但单复值得问题
1. The model backing the ‘MusicStoreDBContext‘ context has changed since the database was created. ...
- {g2o}Installation Notes:ccmake
main reference: http://www.cnblogs.com/gaoxiang12/p/3776107.html "注意libqglviewer-qt4-dev只在ubunt ...
- php关于精准计算的模块 BCMath
Php: BCMath bc是Binary Calculator的缩写.bc*函数的参数都是操作数加上一个可选的 [int scale],比如string bcadd(string $left_ope ...
- 正则表达说明—Pattern API
字符类 [abc] 匹配a.b.c任意一个字符 [^abc] 匹配除了a.b.c外的任意一个字符 [a-zA-Z] 匹配a-z或A ...
- 在Windows 10删除百度云盘的盘符
1点击微软图标不放,然后点击R 打开运行命令 2输入 Regedit 进入注册表 3找到以下路径:HKEY_LOCAL_MACHINE SOFTWARE Microsoft Windows ...
- 将sql 查询结果导出到excel
在平时工作中经常会遇到,sql 查询数据之后需要发送给业务人员,每次都手工执行脚本然后拷贝数据到excel中,比较耗时耗力,可以考虑自动执行查询并将结果邮件发送出来. 分两步实现: 1.执行查询将结果 ...
- 简单明了理解Java移位运算符
无须多言: @Test public void intro() { assertThat("应该相等", -1 >> 1, equalTo(-1)); assertTh ...