【发布时间】:2014-09-24 03:24:33
【问题描述】:
我有示例测试程序来测试 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;
....
}
用户资源库:
package com.test.security;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<UserPrincipal, Long>
{
UserPrincipal getByUsername(String username);
}
用户服务:
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' 同时使用键 [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':无法解决 参考豆 '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:不能 自动装配字段:com.test.security.UserRepository com.test.security.UserService.userRepository;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 为 [com.test.security.UserRepository] 找到了符合条件的 bean 依赖项:预计至少有 1 个符合自动装配条件的 bean 这种依赖的候选人。依赖注解: {@javax.inject.Inject()}
【问题讨论】:
标签: java spring jpa spring-data autowired