尝试为Spring Batch模式和应用程序配置单独的数据源时出现Spring Boot Start Application Error

时间:2019-04-02 19:18:58

标签: spring-batch spring-cloud-task

我们的应用程序使用Oracle 11.2作为数据库。因为不想将“春季批处理元数据”表与常规应用程序表混合使用,所以创建了一个新的架构。但是,当尝试配置两个单独的数据源时,请始终低于错误:

       //configuration first datasource

        @Configuration
        @EnableBatchProcessing
        public class BatchConfig{
            private static final Logger logger = LoggerFactory.getLogger(ReutersMarketDataReadConfig.class);

            ..

            @Bean
            @ConfigurationProperties(prefix = "spring.batch.datasource")
            public DataSource getBatchDataSource() {
            return DataSourceBuilder.create().build();
            }
        ....
        }


        //second data source
        @Configuration
        @EnableTransactionManagement
        @EnableJpaRepositories(
            entityManagerFactoryRef = "appEntityManagerFactory",
            transactionManagerRef = "appTransactionManager",
            basePackages = {"com.xyz.abc.repository" }
        )
        public class ApplicationDBConfig {

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

            @Primary
            @Bean(name = "appEntityManagerFactory")
            public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
                @Qualifier("appDataSource") DataSource dataSource) {
            return builder.dataSource(dataSource).packages("com.xyz.abc.model").persistenceUnit("app").build();
            }

            @Primary
            @Bean(name = "appTransactionManager")
            public PlatformTransactionManager transactionManager(
                @Qualifier("appEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
            return new JpaTransactionManager(entityManagerFactory);
            }
        }

错误:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
[30m2019-04-02 14:56:16,706[0;39m [1;31mERROR[0;39m [[34mrestartedMain[0;39m] [33morg.springframework.boot.SpringApplication[0;39m: Application run failed
            org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jobInvokerController': Unsatisfied dependency expressed through field 'processLiborFeedJob'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'processLiborFeedJob' defined in class path resource [com/db/sts/marketdata/batch/config/ReutersMarketDataReadConfig.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'taskBatchExecutionListener' defined in class path resource [org/springframework/cloud/task/batch/configuration/TaskBatchAutoConfiguration$TaskBatchExecutionListenerAutoconfiguration.class]: Unsatisfied dependency expressed through method 'taskBatchExecutionListener' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration': Invocation of init method failed; nested exception is java.lang.IllegalStateException: To use the default TaskConfigurer the context must contain no more than one DataSource, found 2
                at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
                at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)

1 个答案:

答案 0 :(得分:0)

发生错误Table 'secondarydf.batch_job_instance' doesn't exist.是因为默认情况下,Spring Batch将使用带有dataSource注释的@Primary bean,而不是您期望的getBatchDataSource bean。< / p>

来自@EnableBatchProcessing注释的Javadoc

  

如果在上下文中定义了多个数据源,则将使用带有主要注释的数据源

相关问题是BATCH-2537。因此,在您的情况下,可以使您的类BatchConfig扩展DefaultBatchConfigurer并用您要用于批处理的类覆盖setDataSource