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.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,6 +58,7 @@ public class BatchDatabaseInitializer implements EnvironmentAware {
@PostConstruct
protected void initialize() throws Exception {
if (this.enabled) {
String platform = DatabaseType.fromMetaData(this.dataSource).toString()
.toLowerCase();
if ("hsql".equals(platform)) {
@ -70,4 +75,5 @@ public class BatchDatabaseInitializer implements EnvironmentAware {
populator.setContinueOnError(true);
DatabasePopulatorUtils.execute(populator, this.dataSource);
}
}
}

@ -111,7 +111,7 @@ public abstract class AbstractRepositoryConfigurationSourceSupport implements
List<String> 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;

@ -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;
}

@ -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 {
}

Loading…
Cancel
Save