Use @ConfigurationProperties for Spring Batch
Fixes gh-1249
This commit is contained in:
		
							parent
							
								
									20e08494ae
								
							
						
					
					
						commit
						90cf722365
					
				| 
						 | 
					@ -29,7 +29,6 @@ import org.springframework.batch.core.launch.JobOperator;
 | 
				
			||||||
import org.springframework.batch.core.launch.support.SimpleJobOperator;
 | 
					import org.springframework.batch.core.launch.support.SimpleJobOperator;
 | 
				
			||||||
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.beans.factory.annotation.Value;
 | 
					 | 
				
			||||||
import org.springframework.boot.ExitCodeGenerator;
 | 
					import org.springframework.boot.ExitCodeGenerator;
 | 
				
			||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
 | 
					import org.springframework.boot.autoconfigure.AutoConfigureAfter;
 | 
				
			||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 | 
					import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 | 
				
			||||||
| 
						 | 
					@ -38,6 +37,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 | 
				
			||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
 | 
					import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
 | 
				
			||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 | 
					import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 | 
				
			||||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
 | 
					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.Bean;
 | 
				
			||||||
import org.springframework.context.annotation.Configuration;
 | 
					import org.springframework.context.annotation.Configuration;
 | 
				
			||||||
import org.springframework.jdbc.core.JdbcOperations;
 | 
					import org.springframework.jdbc.core.JdbcOperations;
 | 
				
			||||||
| 
						 | 
					@ -60,10 +60,11 @@ import org.springframework.util.StringUtils;
 | 
				
			||||||
@ConditionalOnClass({ JobLauncher.class, DataSource.class, JdbcOperations.class })
 | 
					@ConditionalOnClass({ JobLauncher.class, DataSource.class, JdbcOperations.class })
 | 
				
			||||||
@AutoConfigureAfter(HibernateJpaAutoConfiguration.class)
 | 
					@AutoConfigureAfter(HibernateJpaAutoConfiguration.class)
 | 
				
			||||||
@ConditionalOnBean(JobLauncher.class)
 | 
					@ConditionalOnBean(JobLauncher.class)
 | 
				
			||||||
 | 
					@EnableConfigurationProperties(BatchProperties.class)
 | 
				
			||||||
public class BatchAutoConfiguration {
 | 
					public class BatchAutoConfiguration {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Value("${spring.batch.job.names:}")
 | 
						@Autowired
 | 
				
			||||||
	private String jobNames;
 | 
						private BatchProperties properties;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Autowired(required = false)
 | 
						@Autowired(required = false)
 | 
				
			||||||
	private JobParametersConverter jobParametersConverter;
 | 
						private JobParametersConverter jobParametersConverter;
 | 
				
			||||||
| 
						 | 
					@ -82,8 +83,9 @@ public class BatchAutoConfiguration {
 | 
				
			||||||
			JobLauncher jobLauncher, JobExplorer jobExplorer) {
 | 
								JobLauncher jobLauncher, JobExplorer jobExplorer) {
 | 
				
			||||||
		JobLauncherCommandLineRunner runner = new JobLauncherCommandLineRunner(
 | 
							JobLauncherCommandLineRunner runner = new JobLauncherCommandLineRunner(
 | 
				
			||||||
				jobLauncher, jobExplorer);
 | 
									jobLauncher, jobExplorer);
 | 
				
			||||||
		if (StringUtils.hasText(this.jobNames)) {
 | 
							String jobNames = this.properties.getJob().getNames();
 | 
				
			||||||
			runner.setJobNames(this.jobNames);
 | 
							if (StringUtils.hasText(jobNames)) {
 | 
				
			||||||
 | 
								runner.setJobNames(jobNames);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return runner;
 | 
							return runner;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -21,10 +21,6 @@ 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.context.EnvironmentAware;
 | 
					 | 
				
			||||||
import org.springframework.core.env.Environment;
 | 
					 | 
				
			||||||
import org.springframework.core.io.ResourceLoader;
 | 
					import org.springframework.core.io.ResourceLoader;
 | 
				
			||||||
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
 | 
					import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
 | 
				
			||||||
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
 | 
					import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
 | 
				
			||||||
| 
						 | 
					@ -37,10 +33,11 @@ import org.springframework.stereotype.Component;
 | 
				
			||||||
 * @author Dave Syer
 | 
					 * @author Dave Syer
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
@Component
 | 
					@Component
 | 
				
			||||||
public class BatchDatabaseInitializer implements EnvironmentAware {
 | 
					public class BatchDatabaseInitializer {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
 | 
					
 | 
				
			||||||
			+ "batch/core/schema-@@platform@@.sql";
 | 
						@Autowired
 | 
				
			||||||
 | 
						private BatchProperties properties;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Autowired
 | 
						@Autowired
 | 
				
			||||||
	private DataSource dataSource;
 | 
						private DataSource dataSource;
 | 
				
			||||||
| 
						 | 
					@ -48,19 +45,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;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	@Override
 | 
					 | 
				
			||||||
	public void setEnvironment(Environment environment) {
 | 
					 | 
				
			||||||
		this.environment = new RelaxedPropertyResolver(environment, "spring.batch.");
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	@PostConstruct
 | 
						@PostConstruct
 | 
				
			||||||
	protected void initialize() {
 | 
						protected void initialize() {
 | 
				
			||||||
		if (this.enabled) {
 | 
							if (this.properties.getInitializer().isEnabled()) {
 | 
				
			||||||
			String platform = getDatabaseType();
 | 
								String platform = getDatabaseType();
 | 
				
			||||||
			if ("hsql".equals(platform)) {
 | 
								if ("hsql".equals(platform)) {
 | 
				
			||||||
				platform = "hsqldb";
 | 
									platform = "hsqldb";
 | 
				
			||||||
| 
						 | 
					@ -72,8 +59,7 @@ public class BatchDatabaseInitializer implements EnvironmentAware {
 | 
				
			||||||
				platform = "oracle10g";
 | 
									platform = "oracle10g";
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
 | 
								ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
 | 
				
			||||||
			String schemaLocation = this.environment.getProperty("schema",
 | 
								String schemaLocation = this.properties.getSchema();
 | 
				
			||||||
					DEFAULT_SCHEMA_LOCATION);
 | 
					 | 
				
			||||||
			schemaLocation = schemaLocation.replace("@@platform@@", platform);
 | 
								schemaLocation = schemaLocation.replace("@@platform@@", platform);
 | 
				
			||||||
			populator.addScript(this.resourceLoader.getResource(schemaLocation));
 | 
								populator.addScript(this.resourceLoader.getResource(schemaLocation));
 | 
				
			||||||
			populator.setContinueOnError(true);
 | 
								populator.setContinueOnError(true);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,77 @@
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright 2012-2014 the original author or authors.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||||
 | 
					 * you may not use this file except in compliance with the License.
 | 
				
			||||||
 | 
					 * You may obtain a copy of the License at
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 *      http://www.apache.org/licenses/LICENSE-2.0
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Unless required by applicable law or agreed to in writing, software
 | 
				
			||||||
 | 
					 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
				
			||||||
 | 
					 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||||
 | 
					 * See the License for the specific language governing permissions and
 | 
				
			||||||
 | 
					 * limitations under the License.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package org.springframework.boot.autoconfigure.batch;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.springframework.boot.context.properties.ConfigurationProperties;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Configuration properties for Spring Batch.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author Stephane Nicoll
 | 
				
			||||||
 | 
					 * @since 1.2.0
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					@ConfigurationProperties("spring.batch")
 | 
				
			||||||
 | 
					public class BatchProperties {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
 | 
				
			||||||
 | 
								+ "batch/core/schema-@@platform@@.sql";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private String schema = DEFAULT_SCHEMA_LOCATION;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private final Initializer initializer = new Initializer();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private final Job job = new Job();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public String getSchema() {
 | 
				
			||||||
 | 
							return schema;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public void setSchema(String schema) {
 | 
				
			||||||
 | 
							this.schema = schema;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public Initializer getInitializer() {
 | 
				
			||||||
 | 
							return initializer;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public Job getJob() {
 | 
				
			||||||
 | 
							return job;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public static class Initializer {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							private boolean enabled = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							public boolean isEnabled() {
 | 
				
			||||||
 | 
								return enabled;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							public void setEnabled(boolean enabled) {
 | 
				
			||||||
 | 
								this.enabled = enabled;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public static class Job {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							private String names = "";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							public String getNames() {
 | 
				
			||||||
 | 
								return names;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue