您的位置:首页 > 编程语言 > Java开发

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: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)来从数据库获取资源认证等。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring mvc 事务