springMvc和shiro整合,shiro的realm不能自动注入的问题

阅读更多

最近研究shiro,一开头就遇到了大困难,调试了3小时。
问题描述如下:shiro和spring mvc整合,shiro自定义了realm。
其中自定义的realm里面居然不能使用@Autowired注解标签注入相关的用户service。
百思不得其解,一项项跟踪,发现原来shiro 自定义realm的认证阶段属于filter,当时的spring bean还没有读取进来。

最后通过配置web.xml文件,把spring mvc的xml提高一点优先级,才最终解决了这个问题。

 

	
	
		contextConfigLocation
		
			/WEB-INF/classes/applicationContext-shiro.xml,
			/WEB-INF/classes/spring-mvc.xml
		
	
	
		org.springframework.web.context.ContextLoaderListener
	


	
		springDispatcherServlet
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:spring-mvc.xml
		
		1
	
	
       
		springDispatcherServlet
		/
	

 

注意红色这一项,我把springmvc的配置文件提上去,放到contextConfigLocation中去加载。

这样,就能在filter阶段注入其它已经注册了的bean。

 

具体参考了这篇文章:

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

 

你可能感兴趣的:(spring,mvc,shiro,autowired)