当前位置: 首页 > 知识库问答 >
问题:

如何在Spring JPA中注入CrudRepository?

麹浩瀚
2023-03-14

我想使用spring-data-jpa和crudrepository 。但我无法得到它。(同一包中的所有其他服务都已正确连接):

@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引起:创建名为“用户服务”的bean时出错:注入资源依赖项失败;嵌套异常是org.springframework.beans.factory.nosuchBeanDefinitionException:没有找到用于依赖项的类型为[UserRepo]的合格bean:需要至少有1个bean作为此依赖项的autowire候选bean。依赖项注释:{@javax.annotation.resource(shareable=true,mappdname=,description=,name=,type=class java.lang.object,lookup=,authenticationtype=container)}

更新了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 serices,在这里我可以毫无问题地autowireEntityManageremf

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

共有1个答案

公冶森
2023-03-14

您应该验证以下内容:

1)用@repository注释userrepo

2)spring beans xml文件中应该包含:

<jpa:repositories base-package="your.repository.package"></jpa:repositories>
@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;
    }
 类似资料:
  • 问题内容: 我想利用spring-data-jpa和。但是我无法自动接线。(同一包中的所有其他服务均已正确接线): 结果: 由以下原因引起:org.springframework.beans.factory.BeanCreationException:创建名称为’userService’的bean时出错:注入资源依赖项失败;嵌套的异常是org.springframework.beans.facto

  • 本文向大家介绍如何在controller中注入service?相关面试题,主要包含被问及如何在controller中注入service?时的应答技巧和注意事项,需要的朋友参考一下 在config services方法中配置这个service。 在controller的构造函数中,添加这个依赖注入。  

  • 以下是关于Spring Boot中集成测试的教程:http://www.baeldung.com/spring-boot-testing 但是当我尝试注入MockMvc时,我总是得到: 创建在类路径资源[org/springframework/boot/test/autoconfigure/web/servlet/MockMvcAutoConfiguration.class]中定义的名为“mock

  • 我有一个使用Spring Batch编写的批处理作业 我在下面有一个配置文件: 我有Json线聚合如下: 我想注入对象映射器,不想在JsonLineAggregator类中创建它。我知道如何使用DI实现它吗?

  • 问题内容: 如何在Java Spring框架中注入Map?如果可能,请提供一些示例代码。 以下合法吗? 问题答案: 首先在您的内定义这样的地图: 然后像这样在您的任何bean中使用此Map:

  • 问题内容: 我已经试过了: scriptEl.InnerHtml和scriptEl.InnerText都给出错误: 有没有一种简单的方法可以将脚本注入dom? 问题答案: 由于某种原因,Richard的解决方案对我而言不起作用(insertAdjacentText失败,并带有异常)。但是,这似乎可行: