我有测试Spring Data JPA的示例测试程序,但似乎没有生成存储库。
我的配置:
<?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>
用户实体:
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;
}
}
我收到此错误:
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' 用key [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' 用key [0]设置构造函数参数;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 用名字创建bean 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': 设置bean时无法解析对bean'userService'的引用 property'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.NoSuchBeanDefinitionException:没有 找到[com.test.security.UserRepository]类型的限定bean 依赖:预计至少有1个bean有资格成为autowire 这种依赖的候选人。依赖注释: {@ javax.inject.Inject()}
答案 0 :(得分:12)
找不到[com.test.security.UserRepository]类型的限定bean用于依赖:期望至少有一个bean符合此依赖关系的autowire候选资格。依赖注释:{@ 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
并使用jpa名称空间的<repositories>
元素来扫描存储库
<jpa:repositories base-package="com.test.security"
entity-manager-factory-ref="myEmf"
transaction-manager-ref="transactionManager"/>
在Creating Repository Instances
了解详情以下是关于<repositories>
标记的摘录:
的链接指示Spring扫描[
com.test.security
]及其所有子包,以查找扩展Repository
或其子接口之一的接口。对于找到的每个接口,基础结构都会注册特定于持久性技术的FactoryBean
,以创建处理查询方法调用的相应代理
对于Java配置,您可以使用@EnableJpaRepositories
注释实现相同的功能。您可以在同一link as above