BatchAutoConfiguration

BatchAutoConfiguration

覆盖特定的自动配置时,我对spring-boot的行为感到有些困惑。

我想部分覆盖BatchAutoConfiguration,但是我想我的问题并不特定于BatchAutoConfiguration。
实际上,我只想“覆盖”此类的两个方法:
public BatchDatabaseInitializer batchDatabaseInitializer()public ExitCodeGenerator jobExecutionExitCodeGenerator()

因此,我编写了以下代码:
    软件包ch.test.autoconfig.autoconfigure;

import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration;
import org.springframework.boot.autoconfigure.batch.BatchDatabaseInitializer;
import org.springframework.boot.autoconfigure.batch.BatchProperties;
import org.springframework.boot.autoconfigure.batch.JobExecutionExitCodeGenerator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcOperations;

import javax.sql.DataSource;

/**
 * I'm using the same annotations as defined in BatchAutoConfiguration...
 */
@Configuration
@ConditionalOnClass({ JobLauncher.class, DataSource.class, JdbcOperations.class })
@AutoConfigureAfter(HibernateJpaAutoConfiguration.class)
@ConditionalOnBean(JobLauncher.class)
@EnableConfigurationProperties(BatchProperties.class)

// ... but I add @AutoConfigureBefore(BatchAutoConfiguration.class) to take precedence over BatchAutoConfiguration
@AutoConfigureBefore(BatchAutoConfiguration.class)
public class JavabatchAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnBean(DataSource.class)
    public BatchDatabaseInitializer batchDatabaseInitializer() {
        System.out.println("Entering overwritten batchDatabaseInitializer");
        return new BatchDatabaseInitializer();
    }

    @Bean
    @ConditionalOnMissingBean
    public ExitCodeGenerator jobExecutionExitCodeGenerator() {
        System.out.println("Entering overwritten jobExecutionExitCodeGenerator");
        return new JobExecutionExitCodeGenerator();
    }
}


如代码中所述,我在类和方法中使用完全相同的注释,因为它们在BatchAutoConfiguration.class中定义。
唯一的例外是添加@AutoConfigureBefore(BatchAutoConfiguration.class)。由于这应该优先于BatchAutoConfiguration,因此我假设这将“覆盖”原始文件。

要测试的是,我使用以下pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">


    <modelVersion>4.0.0</modelVersion>

    <groupId>ch.test.autoconfig</groupId>
    <artifactId>auto_config_test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
    </dependencies>
</project>


我正在通过主要方法使用此简单作业:

package ch.test.autoconfig;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/**
 * Created by U802552 on 01.06.2015.
 */
@SpringBootApplication
@EnableBatchProcessing
public class MainJob {

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;

    @Bean
    protected Tasklet tasklet() {
        return new Tasklet() {
            public RepeatStatus execute(StepContribution contribution,
                                        ChunkContext context) {
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Job job() throws Exception {
        return this.jobs.get("job").start(step1()).build();
    }

    @Bean
    protected Step step1() throws Exception {
        return this.steps.get("step1").tasklet(tasklet()).build();
    }

    public static void main(String[] args) throws Exception {
        // switch on  Auto-Configuration-Report
        System.setProperty("debug","true");
        SpringApplication.run(MainJob.class, args);
    }
}


我希望可以调用我的两个覆盖的方法,但是正如Auto-Configuration-Report所述,我的JavabatchAutoConfiguration类具有一个
由于缺少“ JobLauncher”实例,因此出现“否定匹配”。正如该报告还指出的那样,原始的BatchAutoConfiguration类对于同一ConditionalOnBean检查具有正匹配项。

如果我在课程上评论@ConditionalOnBean(JobLauncher.class),在@ConditionalOnBean(DataSource.class)方法上评论batchDatabaseInitializer,那么一切都会按预期进行。

有人对此行为有解释吗?

谢谢
汉斯约格

最佳答案

您正在使其变得复杂。您唯一需要做的就是创建一个配置,该配置定义要使用的2个特定的bean。

@Configuration
public class MyBatchConfiguration {

    @Bean
    public ExitCodeGenerator myExitCodeGenerator() {
        return new MyExitCodeGenerator();
    }

    @Bean
    public BatchDatabaseInitializer myBatchDatabaseInitializer() {
        return new MyBatchDatabaseInitializer();
    }

}


这就是您所需要的。

07-24 15:04