SpringMvc and shiro integration, shiro realm cannot be automatically injected, springmvcrealm
Shiro, a recent study, encountered major difficulties at the beginning, debugging for three hours.
The problem is described as follows: shiro is integrated with spring mvc, And shiro defines realm.
In the custom realm, the @ Autowired annotation label cannot be used to inject related user services.
A piece of information was tracked, and we found that the authentication phase of shiro's custom realm was filter, and the spring bean was not read yet.
Finally, by configuring the web. xml file and increasing the xml of spring mvc to a higher priority, this problem was finally solved.
1 <! -- Configure the spring container listener --> 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 ;! -- The first layer controller of the spring web program, responsible for processing program requests --> 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>
Note the red item. I put the springmvc configuration file in contextConfigLocation to load it.
In this way, other registered beans can be injected in the filter phase.
For details, refer to this article:
Http://blog.csdn.net/godha/article/details/13025099