Java Spring数据JPA没有类型为的限定bean。。。找到依赖项
我有一个示例测试程序来测试Spring数据JPA,但似乎没有生成存储库 我的配置:Java Spring数据JPA没有类型为的限定bean。。。找到依赖项,java,spring,jpa,spring-data,autowired,Java,Spring,Jpa,Spring Data,Autowired,我有一个示例测试程序来测试Spring数据JPA,但似乎没有生成存储库 我的配置: 创建下降 org.hibernate.dialogue.mysql5innodbdialogue 用户实体: package com.test.security; import org.springframework.security.core.CredentialsContainer; import org.springframework.security.core.userdetails.UserDet
创建下降
org.hibernate.dialogue.mysql5innodbdialogue
用户实体:
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);
}
我得到这个错误:
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”:无法解析
对bean的引用
'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0'
使用键[0]设置构造函数参数时;嵌套异常是
org.springframework.beans.factory.BeanCreationException:错误
创建名为的bean
'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0':
设置bean时无法解析对bean“userService”的引用
属性“userDetailsService”;嵌套异常是
org.springframework.beans.factory.BeanCreationException:错误
创建名为“userService”的bean:自动连线的注入
依赖关系失败;嵌套异常是
org.springframework.beans.factory.BeanCreationException:无法
autowire字段:com.test.security.UserRepository
com.test.security.UserService.userRepository;嵌套异常是
org.springframework.beans.factory.noSuchBean定义异常:否
为找到类型为[com.test.security.UserRepository]的合格bean
依赖项:至少需要1个符合autowire条件的bean
此依赖项的候选项。依赖项批注:
{@javax.inject.inject()}
找不到[com.test.security.UserRepository]类型的符合依赖项条件的bean:至少需要1个符合此依赖项autowire候选项条件的bean。依赖项注释:{@javax.inject.inject()}
获取spring数据jpa命名空间(来自spring数据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名称空间的
元素扫描存储库
更多信息请访问
下面是一个关于
标记的片段:
Spring被指示扫描[com.test.security
]及其所有子包以查找扩展Repository
的接口或其子接口之一。对于找到的每个接口,基础结构都会注册特定于持久性技术的FactoryBean
,以创建适当的代理来处理查询方法的调用
这是有关
对于Java配置,您可以通过
@EnableJpaRepositories
注释实现同样的效果。您可以在相同的中了解更多信息。假设您不搜索存储库的接口吗?不,它扫描实体管理器faactory的实体。这与SpringBean/组件无关
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;
}
}