[英]Spring Data JPA No qualifying bean of type … found for dependency
我有测试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属性'sourceList'时无法解析对bean'org.springframework.security.web.DefaultSecurityFilterChain#0'的引用用键[0]; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.security.web.DefaultSecurityFilterChain#0'的bean时出错:无法解析对bean的引用'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#使用键[3]设置构造函数参数时为0'; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0'的bean时出错:无法解析对bean的引用'org.springframework.security.authentication.ProviderManager#设置bean属性'authenticationManager'时为0'; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.security.authentication.ProviderManager#0'的bean时出错:无法解析对bean的引用'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#设置构造函数参数时为0'; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0'的bean时出错:FactoryBean在创建对象时抛出异常; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.security.authenticationManager'的bean时出错:设置时无法解析bean'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0'的引用带键[0]的构造函数参数; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0'的bean时出错:在设置bean属性'userDetailsService'时无法解析对bean'userService'的引用; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'userService'的bean时出错:注入自动连接的依赖项失败; 嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:com.test.security.UserRepository com.test.security.UserService.userRepository; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[com.test.security.UserRepository]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。 依赖注释:{@ javax.inject.Inject()}
找不到[com.test.security.UserRepository]类型的限定bean用于依赖:预期至少有1个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"/>
这是一个关于<repositories>
标签的片段:
Spring被指示扫描[
com.test.security
]及其所有子包,以查找扩展Repository
或其子接口之一的接口。 对于找到的每个接口,基础结构注册特定于持久性技术的FactoryBean
以创建处理查询方法调用的相应代理。
这是命名空间信息的链接
对于Java配置,您可以使用@EnableJpaRepositories
注释实现相同的@EnableJpaRepositories
。 您可以在上面的相同链接中阅读更多相关信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.