本文介绍了多个jpa:xml配置中的存储库,如何使用Spring java配置使用@EnableJPARepositories进行配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我研究过并找到了关于如何使用带有多个数据源的spring数据jpa,这些数据源指的是在xml配置中配置多个jpa:存储库,如下所示:

I have researched and found an explaination and sample code as to how to use spring data jpa with multiple datasources which refers to configuring multiple jpa:repositories in the xml configuration as follows:

<jpa:repositories base-package="org.springframework.data.jpa.repository.sample"
    entity-manager-factory-ref="entityManagerFactory">
    <repository:exclude-filter type="assignable" expression="org.springframework.data.jpa.repository.sample.AuditableUserRepository" />
</jpa:repositories>
<jpa:repositories base-package="org.springframework.data.jpa.repository.sample"
    entity-manager-factory-ref="entityManagerFactory-2"
    transaction-manager-ref="transactionManager-2">
    <repository:include-filter type="assignable" expression="org.springframework.data.jpa.repository.sample.AuditableUserRepository" />
</jpa:repositories>

你们如何申报两者上面的jpa:使用java配置和@EnableJpaRepositories注释的存储库配置?

How would you declare both of the above jpa:repositories configurations using java configuration and the @EnableJpaRepositories annotation?

注释似乎只支持一组属性(即仅一个jpa:仅存储库),并且无法多次声明注释。

The annotation seems to support only one set of attributes (i.e. for one jpa:repository only) and it is not possible to declare the annotation multiple times.

推荐答案

我创建了一个最小多数据源项目,以帮助我找出如何做到这一点。那里有7个Java类和其他配置,所以我只会在这个答案中发布密钥提取。您可以从GitHub获取完整的项目:

This can be seen in the branch of the demo project here: https://github.com/gratiartis/multids-demo/tree/1-unnamed-entitymanager-beans

这是因为在该示例中,Spring连接了与foodb数据库相关的bean,而Bar不是该数据库中的实体。不幸的是,BarRepository已与Foo实体管理器连接。

This is because in that example, Spring has wired up the beans relating to the "foodb" database, and Bar is not an entity in that database. Unfortunately the BarRepository has been wired up with the Foo entity manager.

我们通过在每个config类中命名所有bean来解决此问题。 ie

We resolve this issue by naming all our beans in each of config class. i.e.

@Bean(name = "fooDataSource") public DataSource dataSource() { .. }
@Bean(name = "fooEntityManager") public EntityManager entityManager() { .. }

此时如果你是要在项目中运行测试,您可能会看到警告,例如:

At this point if you were to run the tests in the project, you might see warnings such as:

 No bean named 'entityManagerFactory' is defined.

这是因为... drumroll ...我们没有带默认名称的EntityManagerFactory entityManagerFactory的。我们有一个名为fooEntityManagerFactory,另一个名为barEntityManagerFactory。 Spring正在寻找具有默认名称的东西,因此我们需要指示它以不同方式连接。

This is because ... drumroll ... we do not have an EntityManagerFactory with the default name "entityManagerFactory". We have one called "fooEntityManagerFactory" and another called "barEntityManagerFactory". Spring is looking for something with a default name, so we need to instruct it to wire things up differently.

事实证明,这非常简单。我们只需要在每个@Configuration类的@EnableJpaRepositories注释中添加正确的引用。

As it turns out, this is incredibly simple to do. We just need to put the correct references in the @EnableJpaRepositories annotation for each @Configuration class.

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "fooEntityManagerFactory",
        transactionManagerRef = "fooTransactionManager",
        basePackages = {"com.sctrcd.multidsdemo.integration.repositories.foo"})
public class FooConfig {
    // ...
}

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "barEntityManagerFactory",
        transactionManagerRef = "barTransactionManager",
        basePackages = { "com.sctrcd.multidsdemo.integration.repositories.bar" })
public class BarConfig {
    // ...
}

如您所见,这些@EnableJpaRepositories注释中的每一个都定义了一个特定的名为EntityManagerFactory和Platfo rmTransactionManager。它们还指定哪些存储库应与这些bean连接。在该示例中,我将存储库放在特定于数据库的包中。也可以通过名称来定义每个单独的存储库,方法是将includeFilters添加到注释中,但是通过按数据库隔离存储库,我相信事情最终应该更具可读性。

As you can see, each of these @EnableJpaRepositories annotations defines a specific named EntityManagerFactory and PlatformTransactionManager. They also specify which repositories should be wired up with those beans. In the example, I have put the repositories in database-specific packages. It is also possible to define each individual repository by name, by adding includeFilters to the annotation, but by segregating the repositories by database, I believe that things should end up more readable.

此时,您应该有一个使用Spring Data存储库的工作应用程序来管理两个独立数据库中的实体。随意从上面的链接中获取项目并运行测试以查看这种情况。希望这个答案对更多人有用,因为我花了相当多的时间来尽可能干净地使用尽可能少的代码来完成这项工作。欢迎任何改进答案或演示项目的想法。

At this point you should have a working application using Spring Data repositories to manage entities in two separate databases. Feel free to grab the project from the link above and run the tests to see this happening. Hopefully this answer is useful to more folks, as I have spent a decent amount of time working out to do this as cleanly as possible with as little code as I could manage. Any ideas for improvement of the answer or demo project are welcome.

这篇关于多个jpa:xml配置中的存储库,如何使用Spring java配置使用@EnableJPARepositories进行配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 08:33