SpringMvc and shiro integration, shiro realm cannot be automatically injected, springmvcrealm

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.