Allow Batch datbase initializer to be disabled

pull/123/merge
Dave Syer 11 years ago
parent caffc28b4c
commit ded9020916

@ -21,6 +21,7 @@ import javax.sql.DataSource;
import org.springframework.batch.support.DatabaseType; import org.springframework.batch.support.DatabaseType;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware; import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
@ -45,6 +46,9 @@ public class BatchDatabaseInitializer implements EnvironmentAware {
@Autowired @Autowired
private ResourceLoader resourceLoader; private ResourceLoader resourceLoader;
@Value("${spring.batch.initializer.enabled:true}")
private boolean enabled = true;
private RelaxedPropertyResolver environment; private RelaxedPropertyResolver environment;
@Override @Override
@ -54,20 +58,22 @@ public class BatchDatabaseInitializer implements EnvironmentAware {
@PostConstruct @PostConstruct
protected void initialize() throws Exception { protected void initialize() throws Exception {
String platform = DatabaseType.fromMetaData(this.dataSource).toString() if (this.enabled) {
.toLowerCase(); String platform = DatabaseType.fromMetaData(this.dataSource).toString()
if ("hsql".equals(platform)) { .toLowerCase();
platform = "hsqldb"; if ("hsql".equals(platform)) {
} platform = "hsqldb";
if ("postgres".equals(platform)) { }
platform = "postgresql"; 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);
} }
} }

@ -111,7 +111,7 @@ public abstract class AbstractRepositoryConfigurationSourceSupport implements
List<String> basePackages = AutoConfigurationUtils List<String> basePackages = AutoConfigurationUtils
.getBasePackages(this.beanFactory); .getBasePackages(this.beanFactory);
if (basePackages.isEmpty()) { 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"); + "a @ComponentScan annotation or else disable *RepositoriesAutoConfiguration");
} }
return basePackages; return basePackages;

@ -20,6 +20,7 @@ import javax.annotation.PreDestroy;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
@ -38,6 +39,9 @@ public class EmbeddedDataSourceConfiguration implements BeanClassLoaderAware {
private ClassLoader classLoader; private ClassLoader classLoader;
@Value("${spring.datasource.name:testdb}")
private String name = "testdb";
@Override @Override
public void setBeanClassLoader(ClassLoader classLoader) { public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader; this.classLoader = classLoader;
@ -47,7 +51,7 @@ public class EmbeddedDataSourceConfiguration implements BeanClassLoaderAware {
public DataSource dataSource() { public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder() EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseConnection.get(this.classLoader).getType()); .setType(EmbeddedDatabaseConnection.get(this.classLoader).getType());
this.database = builder.build(); this.database = builder.setName(this.name).build();
return this.database; return this.database;
} }

@ -19,7 +19,12 @@ package org.springframework.boot.autoconfigure.batch;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import javax.sql.DataSource;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job; import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution; 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.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.TestUtils;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; 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.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
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.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.JdbcTemplate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
/** /**
@ -49,6 +56,16 @@ public class BatchAutoConfigurationTests {
private AnnotationConfigApplicationContext context; private AnnotationConfigApplicationContext context;
@Rule
public ExpectedException expected = ExpectedException.none();
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
public void testDefaultContext() throws Exception { public void testDefaultContext() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
@ -57,6 +74,8 @@ public class BatchAutoConfigurationTests {
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertNotNull(this.context.getBean(JobLauncher.class)); assertNotNull(this.context.getBean(JobLauncher.class));
assertEquals(0, new JdbcTemplate(this.context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION").size());
} }
@Test @Test
@ -72,6 +91,21 @@ public class BatchAutoConfigurationTests {
"job", new JobParameters())); "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 @EnableBatchProcessing
protected static class TestConfiguration { protected static class TestConfiguration {
} }

Loading…
Cancel
Save