org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter' defined in class path resource [spring/spring-shiro.xml]: Cannot resolve reference to bean 'securityManager' while setting bean property 'securityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityManager' defined in class path resource [spring/spring-shiro.xml]: Cannot resolve reference to bean 'sessionManager' while setting bean property 'sessionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionManager' defined in class path resource [spring/spring-shiro.xml]: Cannot resolve reference to bean 'redisSessionDAO' while setting bean property 'sessionDAO';错误
spring-mvc.xml与spring-context-shiro.xml从属于不同容器。
所以,虽然在spring-mvc.xml中配置了自动扫描注入,但在spring-context-shiro.xml这个容器中,这些bean name是不存在的。
在spring-context-shiro.xml配置对需要使用的包com.shiro自动扫描注入:
<context:component-scan base-package="com.shiro"/>
也有可能是项目配置错误,仔细检查一下,我的就是project structure错误
我们一般使用这种方法获取配置文件:
ClassPathXmlApplicationContext re = new ClassPathXmlApplicationContext("applicationContext.xml");
而由spring自动装配的xml文件,根据web.xml的配置:
<param-name>contextConfigLocation</param-name>
虽然spring-mybatis.xml、spring-context-shiro.xml和spring-mvc.xml配置的param-name是同样的名字,
但spring-mvc.xml的param-name属于servlet类springMVC:
<servlet-name>springMVC</servlet-name>
所以在springMVC以外,是拿不到在spring-mvc.xml中配置的自动注入的。
错误分析:
整合shiro之前程序正常启动,但是整合完之后出现问题,原因就是在项目启动加载web.xml的时候,<context-param>的执行顺序是在<servlet>之前的,但是呢,加载注入UserRealm的时候,再次注入UserService就会出现注入失败的情况,原因就是开启注解,扫描包的配置写在spring_mvc.xml中,也就是说我们在访问一个我们还没有注入到Spring容器的bean。所以出现异常。
解决办法是在加载shiro配置文件之前或者之中配置扫描包和开启自动注解。
对应的spring_mvc_shiro.xml文件中加入:
<mvc:annotation-driven />
<!-- 扫描的包 -->
<context:component-scan base-package="com.shiro" />