【问题标题】:Configure Spring Data JPA custom repository impl using Java Config使用 Java Config 配置 Spring Data JPA 自定义存储库 impl
【发布时间】:2015-09-28 07:40:54
【问题描述】:

我正在使用 Spring Boot 和 Spring Data JPA。我想创建一个自定义存储库方法,如参考中所述。我需要在自定义方法中引用 EntityManager。当 CustomRepositoryImpl 类具有 @Autowired/@Inject 字段时,它可以正常工作。我想做的是使用 JavaConfig 配置这个 bean。这可能吗?到目前为止,我的尝试一直失败,这是我的代码:

public interface CustomerRepositoryCustom {

    void resetAll();
}

public class CustomerRepositoryImpl implements CustomerRepositoryCustom {

    //    @Inject
    private EntityManager em;

    public CustomerRepositoryImpl(EntityManager em) {
        this.em = em;
    }

    @Transactional
    @Override
    public void resetAll() {
        // some code
    }
}

在我的@SpringBootApplication 类中,根据定义它是一个@Configuration 类,我有一个像这样的@Bean 定义:

@Bean
public CustomerRepositoryCustom customerRepositoryCustom(EntityManager em) {
    return new CustomerRepositoryImpl(em);
}

这不起作用,这个方法被完全忽略了,我得到以下异常:

Caused by: java.lang.NoSuchMethodException: test.CustomerRepositoryImpl.<init>()

即Spring Data 不查看@Bean 定义,它只是尝试使用不存在的默认构造函数创建自定义 repo bean。

是否可以指示 Spring Data 使用 @Bean 方法?

【问题讨论】:

    标签: java spring-boot spring-data


    【解决方案1】:

    我认为问题是 bean 的名称,它应该命名为 customerRepositoryImpl,在 JavaConfig 的情况下不是,如果你将 javaconfig 更改为这个,它应该可以工作:

    @Bean
    public CustomerRepositoryCustom customerRepositoryImpl(EntityManager em) {
        return new CustomerRepositoryImpl(em);
    }
    

    【讨论】:

    • 是的!这很好用,这样也可以:@Bean(name = "customerRepositoryImpl")。谢谢你。不过,我对解决方案感到非常困惑 - 你怎么知道 bean 名称必须不同?我不记得在 Spring Data (JPA) 文档中读过任何类似的内容,你能解释一下你在哪里找到这个规则吗?
    • 好的,我找到了相关的文档条目,它在 §3.6.1 的“手动接线”部分下。
    【解决方案2】:

    你试过了吗?

    @Repository
    class MyCustomerRepo implements CrudRepository<User,Long>, CustomerRepositoryCustom{
    }
    class CustomerRepositoryCustomImpl implements CustomerRepositoryCustom{
    private EntityManager em;
    
    @Inject
    public CustomerRepositoryImpl(EntityManager em) {
        this.em = em;
    }
    
    @Transactional
    @Override
    public void resetAll() {
        // some code
    }
    }
    

    同时删除你的@bean 定义

    编辑:

    @autowired
    EntityManager em;
    
    @Bean
    public CustomerRepositoryCustom customerRepositoryCustom() {
        return new CustomerRepositoryImpl(em);
    }
    

    【讨论】:

    • 是的,我已经尝试过了,它可以工作,它也可以与 setter 注入一起工作,但这不是我的问题的重点,它仍然需要注释并且没有通过 Java Config 完成。我明确不希望它与@Bean 一起使用。
    • 那么您是否厌倦了在 @SpringBootApplication 类上自动装配 EntityManager 并将其从 bean 定义的参数中删除?根据本文档docs.spring.io/spring/docs/current/spring-framework-reference/…
    • 这应该如何提供帮助?如果我从 bean 定义中删除它,它将如何获取对它的引用,以便它可以在自定义方法中使用?自定义 repo 需要以某种方式获取 EntityManager,并且任何创建它的 @Bean 方法都会被忽略。
    • 查看链接的文档,它传递了一个数据源,而不是自动装配一个 EntityManager
    • 这不起作用。我不需要在 \@Configuration 类中定义 \@Autowired 字段,因为无论如何都会注入 \@Bean 方法参数,因此 EntityManager 将在那里,由容器提供。你显然没有测试你的代码。
    猜你喜欢
    • 2018-10-06
    • 1970-01-01
    • 2018-01-09
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    相关资源
    最近更新 更多