最近研究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.xml7</param-value>8</context-param>9<listener>10<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>11</listener>1213lt;!-- 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>2324<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