springMvc和shiro整合,shiro的realm不能自動注入的問題


最近研究shiro,一開頭就遇到了大困難,調試了3小時。
問題描述如下:shiro和spring mvc整合,shiro自定義了realm。
其中自定義的realm里面居然不能使用@Autowired注解標簽注入相關的用戶service。
百思不得其解,一項項跟蹤,發現原來shiro 自定義realm的認證階段屬於filter,當時的spring bean還沒有讀取進來。

最后通過配置web.xml文件,把spring mvc的xml提高一點優先級,才最終解決了這個問題。

 

 1 <!-- 配置spring容器監聽器 -->  
 2 <context-param>  
 3     <param-name>contextConfigLocation</param-name>  
 4     <param-value>  
 5         /WEB-INF/classes/applicationContext-shiro.xml,  
 6         /WEB-INF/classes/spring-mvc.xml  
 7     </param-value>  
 8 </context-param>  
 9 <listener>  
10     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
11 </listener>  
12   
13 lt;!-- spring web程序的第一層控制器, 負責處理程序請求 -->  
14 <servlet>  
15     <servlet-name>springDispatcherServlet</servlet-name>  
16     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
17     <init-param>  
18         <param-name>contextConfigLocation</param-name>  
19         <param-value>classpath:spring-mvc.xml</param-value>  
20     </init-param>  
21     <load-on-startup>1</load-on-startup>  
22 </servlet>  
23   
24       <servlet-mapping>  
25     <servlet-name>springDispatcherServlet</servlet-name>  
26     <url-pattern>/</url-pattern>  
27 </servlet-mapping>  

注意紅色這一項,我把springmvc的配置文件提上去,放到contextConfigLocation中去加載。

這樣,就能在filter階段注入其它已經注冊了的bean。

 

具體參考了這篇文章:

http://blog.csdn.net/godha/article/details/13025099

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM