一尘不染

如何在Spring JPA中注入CrudRepository?

java

我想利用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。依赖注释:{@
javax.annotation.Resource(shareable = true,mappingName =,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,这很奇怪,因为我已经有了可以正常自动接线EntityManagerEMF没有任何问题的DAO服务。

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

阅读 300

收藏
2020-12-03

共1个答案

一尘不染

您应该验证以下内容:

1)将注解UserRepo@Repository

2)您的spring bean xml文件应包含在其中:

<jpa:repositories base-package="your.repository.package"></jpa:repositories>

3)我建议注入这种类型的豆@Autowired而不是@Resource

更新

看来您完成了我的前三个步骤,现在又领先了一步。现在,请记住,在Java中配置方法名称 DO
事。更改emf()entityManagerFactory()(更“标准”),或设置entity-manager-factory- refemf。例:

@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;
    }
2020-12-03