简体   繁体   English

使用Java Config配置Spring Data JPA定制存储库impl

[英]Configure Spring Data JPA custom repository impl using Java Config

I'm using Spring Boot and Spring Data JPA. 我正在使用Spring Boot和Spring Data JPA。 I want to create a custom repository method, as described in the reference. 我想创建一个自定义存储库方法,如参考中所述。 I need a reference to the EntityManager in the custom method. 我需要在自定义方法中引用EntityManager。 When the CustomRepositoryImpl class has an @Autowired/@Inject field, it works fine. 当CustomRepositoryImpl类具有@ Autowired / @ Inject字段时,它可以正常工作。 What I would like to do is to have this bean configured using JavaConfig. 我想做的是使用JavaConfig配置此bean。 Is this possible? 这可能吗? My attempts have been failing so far, here is my code: 到目前为止,我的尝试一直失败,这是我的代码:

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
    }
}

In my @SpringBootApplication class, which is by definition a @Configuration class, I have a @Bean definition like this one: 在我的@SpringBootApplication类(根据定义是@Configuration类)中,我有一个类似@Bean的定义:

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

This doesn't work, this method is completely ignored, and I get the following exception: 这不起作用,此方法被完全忽略,并且出现以下异常:

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

ie Spring Data doesn't look at the @Bean definition, it just tries to create the custom repo bean itself with the default constructor, which doesn't exist. 也就是说,Spring Data不会查看@Bean定义,它只是尝试使用默认构造函数创建自定义回购bean本身,而默认构造函数不存在。

Is it possible to instruct Spring Data to use the @Bean method? 可以指示Spring Data使用@Bean方法吗?

The issue I think is the name of the bean, it should be named customerRepositoryImpl which in the case of JavaConfig it is not, if you change the javaconfig to this, it should work: 我认为的问题是Bean的名称,应将其命名为customerRepositoryImpl ,如果是JavaConfig则不是,如果您将javaconfig更改为此,它将正常工作:

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

Have you tried? 你有没有尝试过?

@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
}
}

also remove your @bean definition 还删除您的@bean定义

edit: 编辑:

@autowired
EntityManager em;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM