在覆盖特定的自动配置时,我对spring-boot的行为有点困惑。
我想部分覆盖BatchAutoConfiguration,但我想,我的问题不是BatchAutoConfiguration特有的。
实际上,我只是想“覆盖”这个类的两个方法:
public BatchDatabaseInitializer batchDatabaseInitializer()
和public ExitCodeGenerator jobExecutionExitCodeGenerator()
。
因此,我编写了以下代码: package 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
中的definend。
唯一的例外是添加@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);
}
}
我希望,我的两个覆盖方法被调用,但正如自动配置报告所述,我的JavabatchAutoConfiguration类有一个
因为缺少“JobLauncher”实例而导致“否定匹配”。正如报告还指出的那样,原始的BatchAutoConfiguration类与同一ConditionalOnBean
项检查具有正匹配。
如果我对该课程发表评论@ConditionalOnBean(JobLauncher.class)
而@ConditionalOnBean(DataSource.class)
- 方法发表batchDatabaseInitializer
,则一切正常。
有没有人对此行为有解释?
由于 汉斯约里
答案 0 :(得分:5)
你正在走向复杂的道路。您唯一需要做的就是创建一个配置,定义您要使用的2个特定bean。
@Configuration
public class MyBatchConfiguration {
@Bean
public ExitCodeGenerator myExitCodeGenerator() {
return new MyExitCodeGenerator();
}
@Bean
public BatchDatabaseInitializer myBatchDatabaseInitializer() {
return new MyBatchDatabaseInitializer();
}
}
这就是你所需要的一切。
答案 1 :(得分:0)
我认为Hansjörg和M.是正确的。 “方法名称”应与特定的“自动配置”使用的名称相同。
原因:我不确定,如果ApplicationContext中存在相同类型是spring省略创建该类型的唯一标准。至少限定词必须相同?!