Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring引导和批处理项目中设置数据源时,BeanCurrentlyIncremationException_Java_Spring Boot_Spring Batch_Datasource - Fatal编程技术网

Java 在Spring引导和批处理项目中设置数据源时,BeanCurrentlyIncremationException

Java 在Spring引导和批处理项目中设置数据源时,BeanCurrentlyIncremationException,java,spring-boot,spring-batch,datasource,Java,Spring Boot,Spring Batch,Datasource,我在maven多模块项目中使用SpringBoot和Batch来解析CSV文件并将数据存储在MySQL数据库中 当使用我的BatchLauncher类(下面共享)运行批处理模块时,我得到了一个BeancurrentlyIncrementationException,它是由我用于配置MySQL数据库的getDataBase()引起的。(单击此处查看日志) 当我删除此方法时,Spring Boot会自动选择H2类型的嵌入式数据库(用于日志) BatchLauncher类: @Slf4j public

我在maven多模块项目中使用SpringBoot和Batch来解析CSV文件并将数据存储在MySQL数据库中

当使用我的
BatchLauncher
类(下面共享)运行批处理模块时,我得到了一个
BeancurrentlyIncrementationException
,它是由我用于配置MySQL数据库的
getDataBase()
引起的。(单击此处查看日志)

当我删除此方法时,Spring Boot会自动选择H2类型的嵌入式数据库(用于日志)

BatchLauncher类:

@Slf4j
public class BatchLauncher {

    public static void main(String[] args) {
        try {
            Launcher.launchWithConfig("My Batch", BatchConfig.class, false);
        }catch (Exception ex) {
        log.error(ex.getMessage());
        }
    }
}
@Slf4j
public class Launcher {

    private Launcher() {}

    public static void launchWithConfig(String batchName, Class<?> configClass, boolean oncePerDayMax) throws JobExecutionException, BatchException {
        try {
            // Check the spring profiles used
            log.info("Start batch \"" + batchName + "\" with profiles : " + System.getProperty("spring.profiles.active"));

            // Load configuration
            @SuppressWarnings("resource")
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(configClass);

            JobLauncher jobLauncher = context.getBean(JobLauncher.class);
            Job job = context.getBean(Job.class);

            //Authorize only one execution of each job per day
            JobParameters jobParameters = new JobParameters();
            JobExecution execution = jobLauncher.run(job, jobParameters);

            if(!BatchStatus.COMPLETED.equals(execution.getStatus())) {
                throw new BatchException("Unknown error while executing batch : " + batchName);
            }
        }catch (Exception ex){
            log.error("Exception",ex);
            throw new BatchException(ex.getMessage());
        }
    }
}
@Slf4j
@Configuration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@EnableBatchProcessing
@ComponentScan(basePackages = {
        "fr.payet.flad.batch.tasklet",
        "fr.payet.flad.batch.mapper"
})
@Import({CoreConfig.class})
public class BatchConfig {


    private StepBuilderFactory steps;
    private JobBuilderFactory jobBuilderFactory;
    private ReadInputTasklet readInputTasklet;

    public BatchConfig(StepBuilderFactory steps, JobBuilderFactory jobBuilderFactory, ReadInputTasklet readInputTasklet) {
        this.steps = steps;
        this.jobBuilderFactory = jobBuilderFactory;
        this.readInputTasklet = readInputTasklet;
    }

    @Bean
    public DataSource getDataBase(){
        return DataSourceBuilder
                .create()
                .driverClassName("com.mysql.jdbc.Driver")
                .url("jdbc:mysql://localhost:3306/myDb?useSSL=false")
                .username("myuser")
                .password("mypwd")
                .build();
    }

    @Bean
    public Step readInputStep() {
        return steps.get("readInputStep")
                .tasklet(readInputTasklet)
                .build();
    }

    @Bean
    public Job readCsvJob() {
        return jobBuilderFactory.get("readCsvJob")
                .incrementer(new RunIdIncrementer())
                .flow(readInputStep())
                .end()
                .build();
    }
}
启动器类:

@Slf4j
public class BatchLauncher {

    public static void main(String[] args) {
        try {
            Launcher.launchWithConfig("My Batch", BatchConfig.class, false);
        }catch (Exception ex) {
        log.error(ex.getMessage());
        }
    }
}
@Slf4j
public class Launcher {

    private Launcher() {}

    public static void launchWithConfig(String batchName, Class<?> configClass, boolean oncePerDayMax) throws JobExecutionException, BatchException {
        try {
            // Check the spring profiles used
            log.info("Start batch \"" + batchName + "\" with profiles : " + System.getProperty("spring.profiles.active"));

            // Load configuration
            @SuppressWarnings("resource")
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(configClass);

            JobLauncher jobLauncher = context.getBean(JobLauncher.class);
            Job job = context.getBean(Job.class);

            //Authorize only one execution of each job per day
            JobParameters jobParameters = new JobParameters();
            JobExecution execution = jobLauncher.run(job, jobParameters);

            if(!BatchStatus.COMPLETED.equals(execution.getStatus())) {
                throw new BatchException("Unknown error while executing batch : " + batchName);
            }
        }catch (Exception ex){
            log.error("Exception",ex);
            throw new BatchException(ex.getMessage());
        }
    }
}
@Slf4j
@Configuration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@EnableBatchProcessing
@ComponentScan(basePackages = {
        "fr.payet.flad.batch.tasklet",
        "fr.payet.flad.batch.mapper"
})
@Import({CoreConfig.class})
public class BatchConfig {


    private StepBuilderFactory steps;
    private JobBuilderFactory jobBuilderFactory;
    private ReadInputTasklet readInputTasklet;

    public BatchConfig(StepBuilderFactory steps, JobBuilderFactory jobBuilderFactory, ReadInputTasklet readInputTasklet) {
        this.steps = steps;
        this.jobBuilderFactory = jobBuilderFactory;
        this.readInputTasklet = readInputTasklet;
    }

    @Bean
    public DataSource getDataBase(){
        return DataSourceBuilder
                .create()
                .driverClassName("com.mysql.jdbc.Driver")
                .url("jdbc:mysql://localhost:3306/myDb?useSSL=false")
                .username("myuser")
                .password("mypwd")
                .build();
    }

    @Bean
    public Step readInputStep() {
        return steps.get("readInputStep")
                .tasklet(readInputTasklet)
                .build();
    }

    @Bean
    public Job readCsvJob() {
        return jobBuilderFactory.get("readCsvJob")
                .incrementer(new RunIdIncrementer())
                .flow(readInputStep())
                .end()
                .build();
    }
}

解决方案是创建一个自定义的
DataSourceConfiguration
类,用
@Configuration
注释,在该类中,我设置了自己的数据库,如下所示:

@Bean
public DataSource getDataBase(){
    return DataSourceBuilder
            .create()
            .driverClassName("com.mysql.jdbc.Driver")
            .url("jdbc:mysql://localhost:3306/myDB?useSSL=false")
            .username("myUser")
            .password("myPwd")
            .build();
}
这可能有助于: