加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – Spring Data JPA没有类型的限定bean …找到依赖项

发布时间:2020-12-15 01:46:10 所属栏目:大数据 来源:网络整理
导读:我有测试Spring Data JPA的示例测试程序,但似乎没有生成存储库. 我的配置: 用户实体: package com.test.security;import org.springframework.security.core.CredentialsContainer;import org.springframework.security.core.userdetails.UserDetails;@Ent

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

我的配置:

用户实体:

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

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

我收到此错误:

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()}

最佳答案

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()}

抓取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

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

在Creating Repository Instances查看更多信息

这是一个关于< repositories>的片段.标签:

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

这是namespace info的链接

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读