最近研究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
新聞熱點
疑難解答