ssm使用shiro启动报错 5C

报错内容
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginRealm': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.warehouse.service.UserLogingService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.warehouse.service.UserLogingService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}

shiro.xml配置

    <!-- 配置Shiro核心Filter  --> 
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!-- 安全管理器 -->
        <property name="securityManager" ref="securityManager" />
        <!-- 未认证,跳转到哪个页面 -->
        <property name="loginUrl" value="/login.html" />
        <!-- 登陆成功页面 -->
        <property name="successUrl" value="/index.html" />
        <!-- 认证后,没有权限跳转页面 -->
        <property name="unauthorizedUrl" value="/unauthorized.html" />

        <property name="filterChainDefinitions">
            <value>
                /WEB-INF/views/login.html* = anon
                /loging.do* = anon
                /WEB-INF/llb/* = anon
                /res/** = anon
                /** = authc
            </value>
        </property>
    </bean>

    <!-- 配置Realm -->
    <bean id="loginRealm" class="com.warehouse.service.LoginRealm">
    </bean>

    <!-- 安全管理器  -->
    <bean id="securityManager" 
        class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="loginRealm" />
        <!-- <property name="cacheManager" ref="shiroCacheManager" /> -->
    </bean>

    <bean id="lifecycleBeanPostProcessor"
        class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

applicationContext.xml配置

applicationContext.xml配置
 <import resource="applicationContext-shiro.xml"/>

    <!-- 开启定时任务 -->
    <task:annotation-driven />

    <!-- MyBatis配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 显式指定Mapper文件位置 -->
        <property name="mapperLocations" value="classpath*:/mybatis/*Mapper.xml" />
        <!-- mybatis配置文件路径 -->
        <property name="configLocation" value="classpath:/mybatis-config.xml" />
        <property name="typeHandlersPackage" value="package com.warehouse.util" />
    </bean>

    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
        <!-- 这个执行器会批量执行更新语句, 还有SIMPLE 和 REUSE -->
        <!-- <constructor-arg index="1" value="BATCH"/> -->
    </bean>
    <!-- 扫描basePackage接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 映射器接口文件的包路径, -->
        <property name="basePackage" value="com.warehouse.dao" />

    </bean>
    <!-- 使用annotation定义事务 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"
        proxy-target-class="true" />

    <!-- 数据源配置, 使用Tomcat JDBC连接池 -->
    <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
        destroy-method="close">
        <!-- Connection Info -->
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!-- Connection Pooling Info -->
        <property name="maxActive" value="${jdbc.pool.maxActive}" />
        <property name="maxIdle" value="${jdbc.pool.maxIdle}" />
        <property name="minIdle" value="0" />
        <property name="defaultAutoCommit" value="false" />
    </bean>

    <!-- production环境 -->
    <beans profile="production">
        <context:property-placeholder
            ignore-unresolvable="true" file-encoding="utf-8"
            location="classpath:config.properties,classpath:jdbc.properties" />
    </beans>

springMVC.xml配置

 <context:component-scan base-package="com.warehouse.*" />
    <!--jsp可以不用添加这个 如果修改成html就必须用这个 -->
    <mvc:default-servlet-handler />
    <mvc:annotation-driven />
    <!--上面这个是为了html可以用 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />
            </list>
        </property>
    </bean>

    <!--模板 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />
            </list>
        </property>
    </bean>

    <mvc:resources mapping="/res/**" location="/res/" />

    <bean id="freemarkerConfig"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/views/" />
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">0</prop>
                <prop key="default_encoding">UTF-8</prop>
                <prop key="number_format">0.##########</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="classic_compatible">true</prop>
                <prop key="template_exception_handler">ignore</prop>
                <prop key="auto_import">ftl/public.ftl as p,ftl/page.ftl as pg
                </prop>
            </props>
        </property>
        <property name="freemarkerVariables">
            <map>
                <entry key="listProduct" value-ref="ProductDiretciv" />
            </map>
        </property>
    </bean>
    <bean id="viewResolver" class="com.warehouse.freemarker.RichFreeMarkerViewResolver">
        <property name="suffix">
            <value>.html</value>
        </property>
        <property name="contentType" value="text/html;charset=UTF-8"></property>
    </bean>

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views" />
        <property name="suffix" value=".*" />
    </bean>

    <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
        p:prefix="/WEB-INF/views/" p:suffix=".html"/> -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />
            </list>
        </property>
    </bean>
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>

@Controller
@Scope("prototype")
public class UserLogingController {

@RequestMapping("loging.do")
@ResponseBody
public String loging(ForeginUser fuser) {

    Subject subject = SecurityUtils.getSubject();

    AuthenticationToken token = new UsernamePasswordToken(fuser.getUsername(), fuser.getPassword());
    try {
        subject.login(token);
        return _HTML_INDEX_;
    } catch (AuthenticationException e) {
        e.printStackTrace();
        return _HTML_LOGIN_;
    }

}

}@Controller
@Scope("prototype")
public class UserLogingController {

@RequestMapping("loging.do")
@ResponseBody
public String loging(ForeginUser fuser) {

    Subject subject = SecurityUtils.getSubject();

    AuthenticationToken token = new UsernamePasswordToken(fuser.getUsername(), fuser.getPassword());
    try {
        subject.login(token);
        return _HTML_INDEX_;
    } catch (AuthenticationException e) {
        e.printStackTrace();
        return _HTML_LOGIN_;
    }

}

}


package com.warehouse.realm;

public class LoginRealm extends AuthorizingRealm {
@Resource
private UserLogingService userlogingService;

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
    return null;
}

@Override
// 认证
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

    UsernamePasswordToken upToken = (UsernamePasswordToken) token;

    ForeginUser resultUser = userlogingService.findByUsername(upToken.getUsername());
    if (resultUser == null) {

        return new SimpleAuthenticationInfo(resultUser, null, getName());
    } else {
        return new SimpleAuthenticationInfo(resultUser, resultUser.getPassword(), getName());
    }

}

}

package com.warehouse.service;
@Service
public class UserLogingServiceImpl implements UserLogingService {
@Resource
private UserLogingMapper userLogingMapeer;

@Override
public ForeginUser checkLoaderInfo(ForeginUser fuser) {
    return userLogingMapeer.findByUser(fuser);
}
@Override
public ForeginUser findByUsername(String username) {
    return userLogingMapeer.findByUsername(username);
}

}

3个回答

你好,刚才根据日志简单了解了一下你的问题,问题应该出现在LoginRealm中,应该是spring注入UserLogingService 失败,
你可以将@Resource注释换成@Autowared,
spring中@Resource注解默认是通过名称注入
而@Autowared默认是通过类型注入的,你可以试试 ( 如果不对,欢迎指正,我是新手...勿喷)

weixin_42374031
徐子楼 @Resource首先通过名称找名字没有在通过类型如果指定了名称就不一样了
一年多之前 回复

UserLogingService 和 loginRealm 应该是后边这里出现了错误。

com.warehouse.service.UserLogingService注入有问题

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问