如何在spring batch 5.0.1中使用2个数据源

dkqlctbz  于 2023-05-17  发布在  Spring
关注(0)|答案(1)|浏览(130)

我试图在我的Spring批处理项目中添加2个数据源(基于Sping Boot )。一个用于Spring批处理作业存储库。元数据(因为我们不能在版本5中禁用元数据)。我的app.properties是这样的
spring.reader.datasource.url=jdbc:mysql://localhost:3306/springbootcrudexample spring.reader.datasource.driverClassName=com.mysql.jdbc.Driver spring.reader.datasource.username=root spring.reader.datasource.password=
spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=
我的DataSourceConfiguration类如下

@Configuration
public class DataSourceConfiguration {

 @Autowired
    private JobProcessor processor;
    @Autowired
    private JobtWriter writer;
    @Autowired
    private JpaPagingItemReader<OriginDAO> reader;
    
    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource")
    public DataSource domainDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean("readerDataSource")
    @ConfigurationProperties("spring.reader.datasource")
    public DataSource batchDataSource() {
        return DataSourceBuilder.create().build();
    }
}

我的读者很简单:

@Configuration
public class JobReader {

    @Value("${chunkSize}")
    private Integer chunkSize;

    @Bean(destroyMethod="")
    public JpaPagingItemReader<OriginDAO> reader(EntityManagerFactory entityManagerFactory) {
        return new JpaPagingItemReaderBuilder<OriginDAO>()
                .name("dummyname")
                .entityManagerFactory(entityManagerFactory)
                .queryString("select query")
                .pageSize(chunkSize)
                .build();
    }

}

当我尝试运行应用程序时,我的应用程序崩溃了:

2023-05-14 14:58:36.268 ERROR [main] org.springframework.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception with message: jdbcUrl is required with driverClassName.
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:657)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:645)

我不知道我错过了什么。Tnx任何帮助。

7uhlpewt

7uhlpewt1#

我想知道你的构建器方法是空的(.create().build())。尝试替换设置两个数据源的配置类,如下所示:

@Bean(name = "springDataSourceProperties")
    @ConfigurationProperties("spring.datasource")
    @Primary
    public DataSourceProperties springDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "springReaderDataSourceProperties")
    @ConfigurationProperties("spring.reader.datasource")
    public DataSourceProperties springReaderDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "springDataSource")
    @Primary
    public DataSource dataSource(@Qualifier("springDataSourceProperties") DataSourceProperties springDataSourceProperties) {
        return DataSourceBuilder.create()
                                .driverClassName(springDataSourceProperties.getDriverClassName())
                                .url(springDataSourceProperties.getUrl())
                                .password(springDataSourceProperties.getPassword())
                                .username(springDataSourceProperties.getUsername())
                                .build();
    }

    @Bean("readerDataSource")
    public DataSource readerDataSource(@Qualifier("springReaderDataSourceProperties") DataSourceProperties springReaderDataSourceProperties) {
        return DataSourceBuilder.create()
                                .driverClassName(springReaderDataSourceProperties.getDriverClassName())
                                .url(springReaderDataSourceProperties.getUrl())
                                .password(springReaderDataSourceProperties.getPassword())
                                .username(springReaderDataSourceProperties.getUsername())
                                .build();
    }

告诉我你是否适合。

相关问题