我想使用spring-data-jpa和CrudRepository<T, ID extends Serializable>
。但我不能让它自动装配。 (同一个包中的所有其他服务都正确连接):
@Service
public class UserService {
@Resource
private UserRepo repo;
//this does neither work
//@Autowired
//private CrudRepository<User, Long> repo;
}
public interface UserRepo extends CrudRepository<User, Long> {
}
@Entity
public class User {
@Id
private Long id;
}
结果:
引起:org.springframework.beans.factory.BeanCreationException: 创建名为'userService'的bean时出错:注入资源 依赖失败;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 为依赖项找到[UserRepo]类型的限定bean:期望在 至少有1个bean有资格作为autowire候选者 依赖。依赖注释: {@ javax.annotation.Resource(shareable = true,mappedName =,description =, name =,type = class java.lang.Object,lookup =, authenticationType = CONTAINER)}
这里可能有什么问题?
从文档中我认为它应该可以在不编写实现的情况下工作:
在典型的Java应用程序中,您希望编写一个类 实现CustomerRepository。但这就是Spring Data JPA的原因 如此强大:你不必写一个实现 存储库接口。 Spring Data JPA创建了一个实现 在运行应用程序时飞行。
更新了SpringConfig:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories("my.package")
public class AppConfig {
@Bean
public LocalContainerEntityManagerFactoryBean emf() throws ClassNotFoundException, PropertyVetoException {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource());
emf.setPackagesToScan("my.package");
emf.setJpaVendorAdapter(jpaAdapter());
emf.setJpaProperties(jpaProterties());
return emf;
}
}
结果:emf丢失了,这很奇怪,因为我已经有了工作DAO服务,我可以毫无问题地自动装配EntityManager
和EMF
。
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1a6e658': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:336)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:632)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:442)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1094)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:989)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:276)
... 50 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
... 58 more
答案 0 :(得分:16)
您应该验证以下内容:
1)使用UserRepo
注释@Repository
。
2)你的spring beans xml文件应包含在其中:
<jpa:repositories base-package="your.repository.package"></jpa:repositories>
3)我建议使用@Autowired
代替@Resource
<强>更新强>
你似乎已经完成了我的前三步,现在你已经领先一步了。现在,请记住,在Java Config方法中,名称 DO 很重要。将emf()
更改为entityManagerFactory()
(更“标准”),或将entity-manager-factory-ref
设置为emf
。例如:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws ClassNotFoundException, PropertyVetoException {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource());
emf.setPackagesToScan("my.package");
emf.setJpaVendorAdapter(jpaAdapter());
emf.setJpaProperties(jpaProterties());
return emf;
}
答案 1 :(得分:1)
UserRepo不需要注释:
@Repository
答案 2 :(得分:1)
在applicationContext配置中一定是个问题。这是我过去使用它的那个:
<jpa:repositories
base-package="package.containing.your.repositories"
factory-class="org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean"
transaction-manager-ref="transactionManager"
entity-manager-factory-ref="entityManagerFactory" />
<bean
id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<bean
id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
<bean
id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:persistenceUnitName="PU"
p:jpaVendorAdapter-ref="hibernateJpaVendorAdapter"
p:jpaProperties="classpath:hibernate.properties" />
<util:property-path id="sessionFactory" path="entityManagerFactory.sessionFactory" />
答案 3 :(得分:0)
使用XML和注释配置spring数据JPA
//This is the two way to configure spring data JPA
@EnableJpaRepositories (basePackages= "Repository Bean is located in package", entityManagerFactoryRef= "Here is your configuration of EntityManagerFactoryBean")
<jpa:repositories base-package= "Repository Bean where package"
Entity-manager-factory-ref= "Here is your configured EntityManagerFactoryBean" ></jpa:repositories>
// Sample code:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages="com.zxg.springdata",
entityManagerFactoryRef="factoryBean")
public class SpringDataConfig {
@Bean
public LocalContainerEntityManagerFactoryBean factoryBean (
ComboPooledDataSource dataSource, JpaVendorAdapter jpaVendorAdapter
){
LocalContainerEntityManagerFactoryBean factoryBean = new
LocalContainerEntityManagerFactoryBean ();
FactoryBean.setDataSource (dataSource);
FactoryBean.setJpaVendorAdapter (jpaVendorAdapter);
FactoryBean.setPackagesToScan ("com.zxg.springdata");
Return factoryBean;
}
}