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

前端之家收集整理的这篇文章主要介绍了java – Spring Data JPA没有类型的限定bean …找到依赖项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

我的配置:

factorybean">
        MysqL5InnoDBDialect

用户实体:

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中阅读更多相关信息

原文链接:https://www.f2er.com/spring/431341.html

猜你在找的Spring相关文章