Spring MVC注解方式service和controller的扫描顺序

获得WebApplicationContext的方法: http://panyongzheng.iteye.com/admin/blogs/1477702
xml被扫描顺序参考: [url]http://sence-qi.iteye.com/blog/1328902 [/url]
由于服务器启动时的加载配置文件的顺序为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)来从数据库获取资源认证等。

猜你喜欢

转载自panyongzheng.iteye.com/blog/1477691