springMVC 注解扫面顺序问题
2016-06-19 11:23
549 查看
由于服务器启动时的加载配置文件的顺序为web.xml—root-context.xml(Spring的配置文件)—servlet-context.xml(SpringMVC的配置文件),由于root-context.xml配置文件中Controller会先进行扫描装配,但是此时service还没有进行事务增强处理,得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力)
所以在applicationContext.xml一定先扫描service:
然后在context-servlet.xml在扫描controller:
如是没有按照这种扫描顺序,那么可能引起事务失效,或者在spring security的时候,实现FilterInvocationSecurityMetadataSource的类:
无法调用service(NullPointException)来从数据库获取资源认证等。
所以在applicationContext.xml一定先扫描service:
<!-- 扫描注解 --> <context:component-scan base-package="com"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>```
然后在context-servlet.xml在扫描controller:
<!-- 自动扫描Controller --> <context:component-scan base-package="com"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
如是没有按照这种扫描顺序,那么可能引起事务失效,或者在spring security的时候,实现FilterInvocationSecurityMetadataSource的类:
@Autowired public IAuthoritiesService authoritiesService; ...... private void loadResourceDefine() { /* 这里移动到DAO里面,信息从DB获取。 resourceMap = new HashMap<String, Collection<ConfigAttribute>>(); Collection<ConfigAttribute> atts = new ArrayList<ConfigAttribute>(); ConfigAttribute adminCA = new SecurityConfig("ROLE_ADMIN"); atts.add(adminCA); ConfigAttribute userCA = new SecurityConfig("ROLE_USER"); atts.add(userCA); resourceMap.put("/index.jsp", atts);*/ //resourceMap = authoritiesService.getResourceMap();不能这样,空异常。 authoritiesService = (IAuthoritiesService)WebApplicationUtils.getApplicationContext().getBean("authoritiesService"); resourceMap = authoritiesService.getResourceMap(); System.out.println("----------------------> 载入资源:loadResourceDefine()"); }
无法调用service(NullPointException)来从数据库获取资源认证等。
相关文章推荐
- SQL Server误区30日谈 第1天 正在运行的事务在服务器故障转移后继续执行
- 浅析SQL Server中包含事务的存储过程
- Mysql中的事务是什么如何使用
- MySql的事务使用与示例详解
- C#分布式事务的超时处理实例分析
- C#中的事务用法实例分析
- SQL Server的事务操作隔离模式介绍
- MySQL中事务概念的简洁学习教程
- C#处理Access中事务的方法
- 在ASP.NET 2.0中操作数据之六十一:在事务里对数据库修改进行封装
- oracle 合并查询 事务 sql函数小知识学习
- 深入理解Java事务的原理与应用
- sql不常用函数总结以及事务,增加,删除触发器
- mysql的XA事务恢复过程详解
- 在Mysql存储过程中使用事务实例
- mysql存储过程事务管理简析
- php+mysql事务rollback&commit示例
- PHP中的事务使用实例
- SQLServer存储过程中事务的使用方法
- 在Java的JDBC使用中设置事务回滚的保存点的方法