Spring MVC注解方式serviceとcontrollerのスキャン順序
2249 ワード
WebApplication Contectを取得する方法:
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(Spring MVCのプロファイル)ですので、root-context.xmlの設定ファイルの中でControllerは先にスキャン組み立てを行いますが、この時serviceはまだ事務処理を強化していません。得られたのはそのままのサービスです。
だからappication Contect.xmlで必ず先にserviceをスキャンします。
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(Spring MVCのプロファイル)ですので、root-context.xmlの設定ファイルの中でControllerは先にスキャン組み立てを行いますが、この時serviceはまだ事務処理を強化していません。得られたのはそのままのサービスです。
だからappication Contect.xmlで必ず先にserviceをスキャンします。
<context:component-scan base-package="com" >
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
その後context-servlet.xmlでスキャンcontroler:<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の時にFilter InvocationSecurity Metadata Sourceのクラスを実現することができます。@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(Null Point Exception)を呼び出してデータベースからリソース認証などを取得できませんでした。