spring mvc和spring注解扫描时的注意事项

由于服务器启动时的加载配置文件的顺序为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 expression="org.springframework.stereotype.Controller" type="annotation"/> 
    </context:component-scan>

然后在context-servlet.xml在扫描controller:

<context:component-scan base-package="com" use-default-filters="false" > 
        <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/> 
    </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)来从数据库获取资源认证等。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,351评论 6 342
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,149评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 35,199评论 18 399
  • 又如汉高祖刘邦,在前方打仗的时候,后方仅靠萧何一个人。刘邦和项羽打了七十多回仗,都是打败仗,可是兵源、后勤的补给,...
    老不死的鱼阅读 901评论 0 0
  • 今天上午杨局长视察指出问题,我下午去开会,万望大家做好以下几点: 1、下午的队操比赛,要保证质量,赛出风格水平。 ...
    郁郁岩上松阅读 259评论 0 0

友情链接更多精彩内容