Java Spring Data JPA 找不到类型的合格 bean ... 为依赖找到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25071503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:33:32  来源:igfitidea点击:

Spring Data JPA No qualifying bean of type ... found for dependency

javaspringjpaspring-dataautowired

提问by Navid

I have sample test program to test Spring Data JPA, but it seems like the repository doesn't get generated.

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

My config:

我的配置:

<?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>

User entity:

用户实体:

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;
....
}

UserRespository:

用户存储库:

package com.test.security;

import org.springframework.data.repository.CrudRepository;

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

UserService:

用户服务:

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;
    }
}

I get this error:

我收到此错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#0': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [3]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot resolve reference to bean 'org.springframework.security.authentication.ProviderManager#0' while setting bean property 'authenticationManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.ProviderManager#0': Cannot resolve reference to bean 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager': Cannot resolve reference to bean 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0' while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': Cannot resolve reference to bean 'userService' while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.test.security.UserRepository com.test.security.UserService.userRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.test.security.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}

org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.filterChains”的 bean 时出错:设置 bean 属性“sourceList”时无法解析对 bean“org.springframework.security.web.DefaultSecurityFilterChain#0”的引用用键 [0]; 嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为 'org.springframework.security.web.DefaultSecurityFilterChain#0' 的 bean 时出错:无法解析对 bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter# 的​​引用0' 使用键 [3] 设置构造函数参数时;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为 'org.springframework.security.web.authentication 的 bean 时出错。UsernamePasswordAuthenticationFilter#0':在设置 bean 属性 'authenticationManager' 时无法解析对 bean 'org.springframework.security.authentication.ProviderManager#0' 的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为 'org.springframework.security.authentication.ProviderManager#0' 的 bean 时出错:无法解析对 bean 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean# 的引用0' 设置构造函数参数时;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0' 的 bean 时出错:FactoryBean 在对象创建时抛出异常;嵌套异常是 org. springframework.beans.factory.BeanCreationException:创建名为 'org.springframework.security.authenticationManager' 的 bean 时出错:在使用键设置构造函数参数时,无法解析对 bean 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0' 的引用 [ 0]; 嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.authentication.dao.DaoAuthenticationProvider#0”的 bean 时出错:设置 bean 属性“userDetailsS​​ervice”时无法解析对 bean“userService”的引用;嵌套异常是 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()} 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@javax.inject.Inject()} 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@javax.inject.Inject()}

采纳答案by Paul Samsotha

No qualifying bean of type [com.test.security.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}

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

Grab the spring data jpa namespace (from spring-data-jpa jar)

获取 spring 数据 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

And use the <repositories>element of the jpa namespace to scan for repositories

并使用<repositories>jpa 命名空间的元素来扫描存储库

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

See more at Creating Repository Instances

创建存储库实例中查看更多信息

Here's a snippet stated about the <repositories>tag:

以下是关于<repositories>标签的一段说明:

Spring is instructed to scan [com.test.security] and all its subpackages for interfaces extending Repositoryor one of its subinterfaces. For each interface found, the infrastructure registers the persistence technology-specific FactoryBeanto create the appropriate proxies that handle invocations of the query methods

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

Here's the link for the namespace info

这是命名空间信息的链接

For Java config, you could achieve the same thing with the @EnableJpaRepositoriesannotation. You can read more about that in the same link as above

对于 Java 配置,您可以使用@EnableJpaRepositories注释实现相同的目的。您可以在与上述相同的链接中阅读更多相关信息