Commit 2377bd9e authored by Dave Syer's avatar Dave Syer

Don't load BatchAutoConfiguration at all if there is no JobLauncher

Fixes gh-204
parent b1db714c
...@@ -54,6 +54,7 @@ import org.springframework.util.StringUtils; ...@@ -54,6 +54,7 @@ import org.springframework.util.StringUtils;
@Configuration @Configuration
@ConditionalOnClass({ JobLauncher.class, DataSource.class, JdbcOperations.class }) @ConditionalOnClass({ JobLauncher.class, DataSource.class, JdbcOperations.class })
@AutoConfigureAfter(HibernateJpaAutoConfiguration.class) @AutoConfigureAfter(HibernateJpaAutoConfiguration.class)
@ConditionalOnBean(JobLauncher.class)
public class BatchAutoConfiguration { public class BatchAutoConfiguration {
@Value("${spring.batch.job.name:}") @Value("${spring.batch.job.name:}")
...@@ -70,7 +71,6 @@ public class BatchAutoConfiguration { ...@@ -70,7 +71,6 @@ public class BatchAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
@ConditionalOnBean(JobLauncher.class)
@ConditionalOnExpression("${spring.batch.job.enabled:true}") @ConditionalOnExpression("${spring.batch.job.enabled:true}")
public JobLauncherCommandLineRunner jobLauncherCommandLineRunner() { public JobLauncherCommandLineRunner jobLauncherCommandLineRunner() {
JobLauncherCommandLineRunner runner = new JobLauncherCommandLineRunner(); JobLauncherCommandLineRunner runner = new JobLauncherCommandLineRunner();
...@@ -82,7 +82,6 @@ public class BatchAutoConfiguration { ...@@ -82,7 +82,6 @@ public class BatchAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
@ConditionalOnBean(JobLauncher.class)
public ExitCodeGenerator jobExecutionExitCodeGenerator() { public ExitCodeGenerator jobExecutionExitCodeGenerator() {
return new JobExecutionExitCodeGenerator(); return new JobExecutionExitCodeGenerator();
} }
......
...@@ -48,6 +48,7 @@ import org.springframework.boot.test.EnvironmentTestUtils; ...@@ -48,6 +48,7 @@ import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.PlatformTransactionManager;
...@@ -89,6 +90,17 @@ public class BatchAutoConfigurationTests { ...@@ -89,6 +90,17 @@ public class BatchAutoConfigurationTests {
.queryForList("select * from BATCH_JOB_EXECUTION").size()); .queryForList("select * from BATCH_JOB_EXECUTION").size());
} }
@Test
public void testNoBatchConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(EmptyConfiguration.class, BatchAutoConfiguration.class,
EmbeddedDataSourceConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertEquals(0, this.context.getBeanNamesForType(JobLauncher.class).length);
assertEquals(0, this.context.getBeanNamesForType(JobRepository.class).length);
}
@Test @Test
public void testDefinesAndLaunchesJob() throws Exception { public void testDefinesAndLaunchesJob() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
...@@ -105,7 +117,8 @@ public class BatchAutoConfigurationTests { ...@@ -105,7 +117,8 @@ public class BatchAutoConfigurationTests {
@Test @Test
public void testDisableLaunchesJob() throws Exception { public void testDisableLaunchesJob() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "spring.batch.job.enabled:false"); EnvironmentTestUtils.addEnvironment(this.context,
"spring.batch.job.enabled:false");
this.context.register(JobConfiguration.class, BatchAutoConfiguration.class, this.context.register(JobConfiguration.class, BatchAutoConfiguration.class,
EmbeddedDataSourceConfiguration.class, EmbeddedDataSourceConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
...@@ -117,7 +130,8 @@ public class BatchAutoConfigurationTests { ...@@ -117,7 +130,8 @@ public class BatchAutoConfigurationTests {
@Test @Test
public void testDisableSchemaLoader() throws Exception { public void testDisableSchemaLoader() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.name:batchtest", EnvironmentTestUtils.addEnvironment(this.context,
"spring.datasource.name:batchtest",
"spring.batch.initializer.enabled:false"); "spring.batch.initializer.enabled:false");
this.context.register(TestConfiguration.class, BatchAutoConfiguration.class, this.context.register(TestConfiguration.class, BatchAutoConfiguration.class,
EmbeddedDataSourceConfiguration.class, EmbeddedDataSourceConfiguration.class,
...@@ -149,6 +163,10 @@ public class BatchAutoConfigurationTests { ...@@ -149,6 +163,10 @@ public class BatchAutoConfigurationTests {
new JobParameters())); new JobParameters()));
} }
@Configuration
protected static class EmptyConfiguration {
}
@EnableBatchProcessing @EnableBatchProcessing
@ComponentScan(basePackageClasses = City.class) @ComponentScan(basePackageClasses = City.class)
protected static class TestConfiguration { protected static class TestConfiguration {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment