【问题标题】:Spring Data JPA No qualifying bean of type ... found for dependencySpring Data JPA 没有符合条件的 bean 类型...找到依赖项
【发布时间】:2014-09-24 03:24:33
【问题描述】:

我有示例测试程序来测试 Spring Data JPA,但似乎没有生成存储库。

我的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/jee
            http://www.springframework.org/schema/jee/spring-jee.xsd">

    <import resource="securityConfig.xml" />

    <context:annotation-config />
    <context:component-scan base-package="com.test">
        <context:exclude-filter type="annotation"
                expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <jee:jndi-lookup id="myDataSource" jndi-name="java:comp/env/jdbc/test"/>

    <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="packagesToScan" value="com.test.security" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="myEmf" />
    </bean>
</beans>

用户实体:

package com.test.security;

import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table
public class UserPrincipal implements UserDetails, CredentialsContainer, Cloneable {
    private static final long serialVersionUID = 1L;

    private long id;
....
}

用户资源库:

package com.test.security;

import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<UserPrincipal, Long>
{
    UserPrincipal getByUsername(String username);
}

用户服务:

package com.test.security;

@Service
public class UserService implements UserDetailsService {
    @Inject
    UserRepository userRepository;

    @Override
    @Transactional
    public UserPrincipal loadUserByUsername(String username) {
        UserPrincipal principal = userRepository.getByUsername(username);
        // make sure the authorities and password are loaded
        principal.getAuthorities().size();
        principal.getPassword();
        return principal;
    }
}

我收到此错误:

org.springframework.beans.factory.BeanCreationException: 错误 创建名为“org.springframework.security.filterChains”的bean: 无法解析对 bean 的引用 'org.springframework.security.web.DefaultSecurityFilterChain#0' 同时 使用键 [0] 设置 bean 属性“sourceList”;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 用名字创建bean 'org.springframework.security.web.DefaultSecurityFilterChain#0': 无法解析对 bean 的引用 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' 同时使用键 [3] 设置构造函数参数;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 用名字创建bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': 无法解析对 bean 的引用 'org.springframework.security.authentication.ProviderManager#0' 同时 设置 bean 属性“authenticationManager”;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 用名字创建bean 'org.springframework.security.authentication.ProviderManager#0': 无法解析对 bean 的引用 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0' 在设置构造函数参数时;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 用名字创建bean 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0': FactoryBean 在创建对象时抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 用名字创建bean 'org.springframework.security.authenticationManager':无法解决 参考豆 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0' 同时使用键 [0] 设置构造函数参数;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 用名字创建bean 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': 设置 bean 时无法解析对 bean 'userService' 的引用 属性'userDetailsS​​ervice';嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为“userService”的bean:注入自动装配 依赖失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:不能 自动装配字段:com.test.security.UserRepository com.test.security.UserService.userRepository;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 为 [com.test.security.UserRepository] ​​找到了符合条件的 bean 依赖项:预计至少有 1 个符合自动装配条件的 bean 这种依赖的候选人。依赖注解: {@javax.inject.Inject()}

【问题讨论】:

    标签: java spring jpa spring-data autowired


    【解决方案1】:

    没有为依赖找到类型为 [com.test.security.UserRepository] ​​的合格 bean:预计至少有 1 个 bean 有资格作为此依赖的自动装配候选者。依赖注解:{@javax.inject.Inject()}

    获取 spring data jpa 命名空间(来自 spring-data-jpa jar)

    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation=
                http://www.springframework.org/schema/data/jpa 
                http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    

    并使用 jpa 命名空间的 &lt;repositories&gt; 元素扫描存储库

    <jpa:repositories base-package="com.test.security"
                      entity-manager-factory-ref="myEmf"
                      transaction-manager-ref="transactionManager"/> 
    

    Creating Repository Instances查看更多信息

    这是一个关于&lt;repositories&gt; 标签的sn-p:

    指示 Spring 扫描 [com.test.security] 及其所有子包以查找扩展 Repository 的接口或其子接口之一。对于找到的每个接口,基础架构都会注册特定于持久性技术的FactoryBean,以创建适当的代理来处理查询方法的调用

    这是namespace info的链接

    对于 Java 配置,您可以使用 @EnableJpaRepositories 注释实现相同的目的。您可以在同一 link as above 中阅读更多相关信息

    【讨论】:

    • 难道不是 假设要搜索存储库的接口吗?
    • 不扫描实体管理器工厂的实体。这与 Spring bean/components 无关
    猜你喜欢
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2013-06-28
    • 2014-01-15
    • 2013-12-18
    • 1970-01-01
    相关资源
    最近更新 更多