【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 ...
随机推荐
- dubbo-spring
一.需求 某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址:创建两个服务模块进行测试 测试预期结果:订单服务web模块在A服务器,用户服务模块在B服务器,A可以远程调用B的功能. 二.工程 ...
- 二叉查找树之AVL树
定义平衡树节点: class TreeNode { /** * 树节点的值 */ private int val; /** * 树的高度 */ private int height; /** * 左子 ...
- SpringMVC之 HandlerAdapter和handlerMapping
HandlerAdapter字面上的意思就是处理适配器,它的作用用一句话概括就是调用具体的方法对用户发来的请求来进行处理.当handlerMapping获取到执行请求的controller时,Disp ...
- 用Excel生成Sql
用Excel生成Sql: 以如图为例:点击一行数据的后面一个单元格,在上面的fx部分输入=,以等号开头证明这是一个公式.在等号的后面写上想要添加的数据,书写规范是这样:'"&A2&a ...
- android 常用第三方github工程
这里有个汇总的网址:http://androidxy.com/zh/page/latest/-1/0 数据库:greenDao 参考:GreenDao3.2的使用 控件注解:Butterknife 图 ...
- JS 操作对象 事件 样式
1.获取标记对象 css 1 - class 2 - id 3 - 标记选择器 js 1 - class 2 - id 3 - 标记 4 - name + document.getElementByI ...
- java动态代理使用详解
我们都知道AOP的原理就是java的动态代理机制,下面我就对java的动态代理机制进行学习与总结 java动态代理的实现有两个重要的类: Proxy:类 作用就是用来动态创建一个代理对象的类 Invo ...
- org.springframework.beans.factory.BeanCreationException: Could not autowire
由于我在项目中引用了如下代码,增加了 @Configurationpublic class Connection { public @Bean HttpClientConfig httpClie ...
- UVA 1220 Party at Hali-Bula (树形DP)
求一棵数的最大独立集结点个数并判断方案是否唯一. dp[i][j]表示以i为根的子树的最大独立集,j的取值为选和不选. 决策: 当选择i时,就不能选择它的子结点. 当不选i时,它的子结点可选可不选. ...
- Oracle旗下软件官网下载速度过慢解决办法
平常下载Oracle旗下软件官网的产品资源,会发现速度很慢,如下载JDK和mysql时, 这样很浪费我们的时间 解决办法: 复制自己需要下载的资源链接 使用迅雷下载该资源 速度均很快 如下载Mysql ...