SpringMVC+MyBatis - 7 Spring自动扫描注解类的冲突问题
2016-06-17 14:24
465 查看
Spring MVC项目中通常会有二个配置文件,sprng-servlet.xml和applicationContext.xml二个配置文件,通常会出现以下几个配置
1. <context:annotation-config />
它的作用是隐式地向 Spring 容器注册
- AutowiredAnnotationBeanPostProcessor、
- CommonAnnotationBeanPostProcessor、
- PersistenceAnnotationBeanPostProcessor、
- RequiredAnnotationBeanPostProcessor 这4个BeanPostProcessor。
其作用是如果你想在程序中使用注解,就必须先注册该注解对应的类,如下图所示:
当然也可以自己进行注册:
2. <context:component-scan base-package="com.*" >
<context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用
<context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。
在这里有一个比较有意思的问题,就是扫描是否需要在二个配置文件都配置一遍,我做了这么几种测试:
(1)只在applicationContext.xml中配置如下
启动正常,但是任何请求都不会被拦截,简而言之就是@Controller失效
(2)只在spring-servlet.xml中配置上述配置
启动正常,请求也正常,但是事物失效,也就是不能进行回滚
(3)在applicationContext.xml和spring-servlet.xml中都配置上述信息
启动正常,请求正常,也是事物失效,不能进行回滚
(4)在applicationContext.xml中配置如下
在spring-servlet.xml中配置如下
此时启动正常,请求正常,事物也正常了。
结论:在spring-servlet.xml中只需要扫描所有带@Controller注解的类,在applicationContext中可以扫描所有其他带有注解的类(也可以过滤掉带@Controller注解的类)。
3. <mvc:annotation-driven />
它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter
1. <context:annotation-config />
它的作用是隐式地向 Spring 容器注册
- AutowiredAnnotationBeanPostProcessor、
- CommonAnnotationBeanPostProcessor、
- PersistenceAnnotationBeanPostProcessor、
- RequiredAnnotationBeanPostProcessor 这4个BeanPostProcessor。
其作用是如果你想在程序中使用注解,就必须先注册该注解对应的类,如下图所示:
依赖的类 | 注解 |
CommonAnnotationBeanPostProcessor | @Resource 、@PostConstruct、@PreDestroy |
PersistenceAnnotationBeanPostProcessor的Bean | @PersistenceContext |
AutowiredAnnotationBeanPostProcessor Bean | @Autowired |
RequiredAnnotationBeanPostProcessor | @Required |
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/> <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
2. <context:component-scan base-package="com.*" >
<context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用
<context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。
在这里有一个比较有意思的问题,就是扫描是否需要在二个配置文件都配置一遍,我做了这么几种测试:
(1)只在applicationContext.xml中配置如下
<context:component-scan base-package="com.login" />
启动正常,但是任何请求都不会被拦截,简而言之就是@Controller失效
(2)只在spring-servlet.xml中配置上述配置
启动正常,请求也正常,但是事物失效,也就是不能进行回滚
(3)在applicationContext.xml和spring-servlet.xml中都配置上述信息
启动正常,请求正常,也是事物失效,不能进行回滚
(4)在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注解的类)。
3. <mvc:annotation-driven />
它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- 监听器获取Spring配置文件的方法
- springmvc 发送ajax出现中文乱码的解决方法汇总
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- 详解Java的MyBatis框架和Spring框架的整合运用
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)