如何在春季JPA中注入CrudRepository?
我想利用 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 有资格作为此依赖项的自动连接候选项。Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, lookup=, authenticationType=CONTAINER)}
这里可能出了什么问题?
从文档中,我认为它应该在不编写实现的情况下工作:
在典型的 Java 应用程序中,您需要编写一个实现 CustomerRepository 的类。但这就是Spring Data JPA如此强大的原因:您不必编写存储库接口的实现。Spring Data JPA 会在您运行应用程序时动态创建一个实现。
更新的弹簧配置:
@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;
}
}
结果:电动势丢失,这很奇怪,因为我已经有工作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