From ded90209164532f92a7c3fd6c9e379953c3ad626 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 19 Nov 2013 09:03:31 +0000 Subject: [PATCH] Allow Batch datbase initializer to be disabled --- .../batch/BatchDatabaseInitializer.java | 34 ++++++++++------- ...tRepositoryConfigurationSourceSupport.java | 2 +- .../jdbc/EmbeddedDataSourceConfiguration.java | 6 ++- .../batch/BatchAutoConfigurationTests.java | 38 ++++++++++++++++++- 4 files changed, 62 insertions(+), 18 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java index 330a75cf63..e145e90f5e 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java @@ -21,6 +21,7 @@ import javax.sql.DataSource; import org.springframework.batch.support.DatabaseType; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.context.EnvironmentAware; import org.springframework.core.env.Environment; @@ -45,6 +46,9 @@ public class BatchDatabaseInitializer implements EnvironmentAware { @Autowired private ResourceLoader resourceLoader; + @Value("${spring.batch.initializer.enabled:true}") + private boolean enabled = true; + private RelaxedPropertyResolver environment; @Override @@ -54,20 +58,22 @@ public class BatchDatabaseInitializer implements EnvironmentAware { @PostConstruct protected void initialize() throws Exception { - String platform = DatabaseType.fromMetaData(this.dataSource).toString() - .toLowerCase(); - if ("hsql".equals(platform)) { - platform = "hsqldb"; - } - if ("postgres".equals(platform)) { - platform = "postgresql"; + if (this.enabled) { + String platform = DatabaseType.fromMetaData(this.dataSource).toString() + .toLowerCase(); + if ("hsql".equals(platform)) { + platform = "hsqldb"; + } + if ("postgres".equals(platform)) { + platform = "postgresql"; + } + ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); + String schemaLocation = this.environment.getProperty("schema", + DEFAULT_SCHEMA_LOCATION); + schemaLocation = schemaLocation.replace("@@platform@@", platform); + populator.addScript(this.resourceLoader.getResource(schemaLocation)); + populator.setContinueOnError(true); + DatabasePopulatorUtils.execute(populator, this.dataSource); } - ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); - String schemaLocation = this.environment.getProperty("schema", - DEFAULT_SCHEMA_LOCATION); - schemaLocation = schemaLocation.replace("@@platform@@", platform); - populator.addScript(this.resourceLoader.getResource(schemaLocation)); - populator.setContinueOnError(true); - DatabasePopulatorUtils.execute(populator, this.dataSource); } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/AbstractRepositoryConfigurationSourceSupport.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/AbstractRepositoryConfigurationSourceSupport.java index f3ad634b8c..5d566bfd76 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/AbstractRepositoryConfigurationSourceSupport.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/AbstractRepositoryConfigurationSourceSupport.java @@ -111,7 +111,7 @@ public abstract class AbstractRepositoryConfigurationSourceSupport implements List basePackages = AutoConfigurationUtils .getBasePackages(this.beanFactory); if (basePackages.isEmpty()) { - logger.warn("Unable to find repository base packages. If you need Repositories please define " + logger.info("Unable to find repository base packages. If you need Repositories please define " + "a @ComponentScan annotation or else disable *RepositoriesAutoConfiguration"); } return basePackages; diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java index b134694288..a88b718e23 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java @@ -20,6 +20,7 @@ import javax.annotation.PreDestroy; import javax.sql.DataSource; import org.springframework.beans.factory.BeanClassLoaderAware; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; @@ -38,6 +39,9 @@ public class EmbeddedDataSourceConfiguration implements BeanClassLoaderAware { private ClassLoader classLoader; + @Value("${spring.datasource.name:testdb}") + private String name = "testdb"; + @Override public void setBeanClassLoader(ClassLoader classLoader) { this.classLoader = classLoader; @@ -47,7 +51,7 @@ public class EmbeddedDataSourceConfiguration implements BeanClassLoaderAware { public DataSource dataSource() { EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseConnection.get(this.classLoader).getType()); - this.database = builder.build(); + this.database = builder.setName(this.name).build(); return this.database; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java index 92e289202d..b1ef393864 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java @@ -19,7 +19,12 @@ package org.springframework.boot.autoconfigure.batch; import java.util.Collection; import java.util.Collections; +import javax.sql.DataSource; + +import org.junit.After; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; @@ -31,13 +36,15 @@ import org.springframework.batch.core.job.AbstractJob; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.repository.JobRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.TestUtils; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration; -import org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; +import org.springframework.jdbc.BadSqlGrammarException; +import org.springframework.jdbc.core.JdbcTemplate; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; /** @@ -49,6 +56,16 @@ public class BatchAutoConfigurationTests { private AnnotationConfigApplicationContext context; + @Rule + public ExpectedException expected = ExpectedException.none(); + + @After + public void close() { + if (this.context != null) { + this.context.close(); + } + } + @Test public void testDefaultContext() throws Exception { this.context = new AnnotationConfigApplicationContext(); @@ -57,6 +74,8 @@ public class BatchAutoConfigurationTests { PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertNotNull(this.context.getBean(JobLauncher.class)); + assertEquals(0, new JdbcTemplate(this.context.getBean(DataSource.class)) + .queryForList("select * from BATCH_JOB_EXECUTION").size()); } @Test @@ -72,6 +91,21 @@ public class BatchAutoConfigurationTests { "job", new JobParameters())); } + @Test + public void testDisableSchemaLoader() throws Exception { + this.context = new AnnotationConfigApplicationContext(); + TestUtils.addEnviroment(this.context, "spring.datasource.name:batchtest", + "spring.batch.initializer.enabled:false"); + this.context.register(TestConfiguration.class, BatchAutoConfiguration.class, + EmbeddedDataSourceConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + assertNotNull(this.context.getBean(JobLauncher.class)); + this.expected.expect(BadSqlGrammarException.class); + new JdbcTemplate(this.context.getBean(DataSource.class)) + .queryForList("select * from BATCH_JOB_EXECUTION"); + } + @EnableBatchProcessing protected static class TestConfiguration { }