在Spring Boot应用程序中使用多个数据源配置时,无法从AttributeConverter内部的属性获取值

问题描述

我通过引用以下链接创建了具有多个数据源的spring boot应用程序: https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7

并通过引用以下链接在实体级别添加了加密: https://github.com/sunitk/generic-jpa-converter-encrypt-decrypt

使用多个数据源配置时,我无法在Attribute Converter中获取属性值。它为空。

但是使用单个数据源(属性),我可以获取属性值。

请让我知道如何通过多个数据源配置而不是认的单个数据源属性获取属性值?

解决方法

使用@Convert(converter = Xxx.class)与特定的数据源无关。您应该能够在每个@Entity中使用转换器。

这是一个有效的演示:multi-datasource-converter

它在多个数据源上使用您引用的转换器。

代码段

application.properties

foo.datasource.jdbcUrl=jdbc:h2:mem:foo
foo.datasource.username=sa
foo.datasource.password=
foo.datasource.driver-class-name=org.h2.Driver

bar.datasource.jdbcUrl=jdbc:h2:mem:bar
bar.datasource.username=sa
bar.datasource.password=
bar.datasource.driver-class-name=org.h2.Driver

配置数据源,选择@Primary,然后将两个EntityManagerFactoryBuilders都指向正确的包

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "fooEntityManagerFactory",transactionManagerRef = "fooTransactionManager",basePackageClasses = FooRepository.class)
public class FooJpaConfiguration {

    @Primary
    @Bean(name = "fooDataSource")
    @ConfigurationProperties(prefix = "foo.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "fooEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            EntityManagerFactoryBuilder builder,@Qualifier("fooDataSource") DataSource dataSource) {
        return builder.dataSource(dataSource).packages("com.example.demo.foo").persistenceUnit("foo")....
....


@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "barEntityManagerFactory",transactionManagerRef = "barTransactionManager",basePackageClasses = BarRepository.class )
public class BarJpaConfiguration {

    @Bean(name = "barDataSource")
    @ConfigurationProperties(prefix = "bar.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "barEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(
            EntityManagerFactoryBuilder builder,@Qualifier("barDataSource") DataSource dataSource) {
        return builder.dataSource(dataSource).packages("com.example.demo.bar").persistenceUnit("bar")

并在您的实体中使用转换器

package com.example.demo.foo;
...
@Entity
public class Foo {
    @Id
    private Long id;
    private String name;
    @Convert(converter = StringEncryptDecryptConverter.class)
    private String secret;

....
package com.example.demo.bar;
...
@Entity
public class Bar {
    @Id
    private Long id;
    private String name;
    @Convert(converter = StringEncryptDecryptConverter.class)
    private String secret;