【解决】SpringMVC整合Shiro之 Error creating bean with name 'shiroFilter' defined in class path resource...

完整的部分错误日志:

org.springframework.beans.factory.BeanCreationException: 

Error creating bean with name 'shiroFilter' defined in class path resource [spring_mvc_shiro.xml]: Cannot resolve reference to bean 'securityManager' while setting bean property 'securityManager'; 

 Could not autowire field: private com.guide.user.service.UserService com.guide.web.controller.UserRealm.userService; 

No qualifying bean of type [com.guide.user.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

spring_mvc_shiro.xml

<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
        <property name="hashAlgorithmName" value="md5">property>
        <property name="hashIterations" value="2">property>
        <property name="storedCredentialsHexEncoded" value="true">property>
    bean>  

      
    <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager">bean>  


    <bean id="userRealm" class="com.guide.web.controller.UserRealm">
        <property name="credentialsMatcher" ref="credentialsMatcher">property>  
    bean> 


      
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
        <property name="realm" ref="userRealm">property>  

    bean>  


      
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
          
        <property name="securityManager" ref="securityManager">property>  
          
        <property name="loginUrl" value="">property>  
          

          
        
        <property name="filterChainDefinitions">  
            <value>  
                /user/**=anon
                /js/**=anon
                /css**/=anon

            value>  
        property>  
    bean>  

web.xml

<context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>
            classpath:spring_mybatis.xml,
            classpath:spring_mvc_shiro.xml
        param-value>
context-param>
...
<servlet>
        <servlet-name>springservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring_mvc.xmlparam-value>
        init-param>

        
        <load-on-startup>1load-on-startup>
    servlet>

UserRealm .java

public class UserRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    ...

错误分析:

整合shiro之前程序正常启动,但是整合完之后出现问题,原因就是在项目启动加载web.xml的时候,的执行顺序是在之前的,但是呢,加载注入UserRealm的时候,再次注入UserService就会出现注入失败的情况,原因就是开启注解,扫描包的配置写在spring_mvc.xml中,也就是说我们在访问一个我们还没有注入到Spring容器的bean。所以出现异常。

解决办法是在加载shiro配置文件之前或者之中配置扫描包和开启自动注解。

对应的spring_mvc_shiro.xml文件中加入:

<mvc:annotation-driven />

<context:component-scan base-package="com.guide.*.service,com.guide.*.mapper" />

OK

你可能感兴趣的:(Spring,Shiro)