微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我的配置:

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

    ecurityConfig.xml" />

    fig />
    factorybean">
        ecurity" />
        vendorAdapter">
            vendor.HibernateJpavendorAdapter" />
        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命名空间的元素

ecurity"
                  entity-manager-factory-ref="myEmf"
                  transaction-manager-ref="transactionManager"/> 

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐