Minor refactoring

- Extract/Rearrange methods
- Inline variables
- Improve Javadoc formatting
This commit is contained in:
Mahmoud Ben Hassine
2022-05-15 01:54:12 +02:00
parent 670770578a
commit 72d9177cf1
4 changed files with 59 additions and 52 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.batch.core.configuration.annotation;
import java.util.Collection;
import java.util.Map;
import javax.sql.DataSource;
@@ -39,8 +40,8 @@ import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.Assert;
/**
* Base {@code Configuration} class providing common structure for enabling and using Spring Batch. Customization is
* available by implementing the {@link BatchConfigurer} interface. {@link BatchConfigurer}.
* Base {@code Configuration} class providing common structure for enabling and using Spring Batch.
* Customization is available by implementing the {@link BatchConfigurer} interface.
*
* @author Dave Syer
* @author Michael Minella
@@ -57,8 +58,6 @@ public abstract class AbstractBatchConfiguration implements ImportAware, Initial
private BatchConfigurer configurer;
private JobRegistry jobRegistry = new MapJobRegistry();
private JobBuilderFactory jobBuilderFactory;
private StepBuilderFactory stepBuilderFactory;
@@ -120,7 +119,7 @@ public abstract class AbstractBatchConfiguration implements ImportAware, Initial
*/
@Bean
public JobRegistry jobRegistry() throws Exception {
return this.jobRegistry;
return new MapJobRegistry();
}
/**
@@ -133,10 +132,11 @@ public abstract class AbstractBatchConfiguration implements ImportAware, Initial
@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
AnnotationAttributes enabled = AnnotationAttributes.fromMap(importMetadata.getAnnotationAttributes(
EnableBatchProcessing.class.getName(), false));
Assert.notNull(enabled,
"@EnableBatchProcessing is not present on importing class " + importMetadata.getClassName());
Map<String, Object> annotationAttributes =
importMetadata.getAnnotationAttributes(EnableBatchProcessing.class.getName(), false);
AnnotationAttributes enabled = AnnotationAttributes.fromMap(annotationAttributes);
String message = "@EnableBatchProcessing is not present on importing class " + importMetadata.getClassName();
Assert.notNull(enabled, message);
}
@Override
@@ -156,23 +156,11 @@ public abstract class AbstractBatchConfiguration implements ImportAware, Initial
return this.configurer;
}
if (configurers == null || configurers.isEmpty()) {
DataSource dataSource;
try {
dataSource = this.context.getBean(DataSource.class);
} catch (NoUniqueBeanDefinitionException exception) {
throw new IllegalStateException(
"Multiple data sources are defined in the application context and no primary candidate was found. " +
"To use the default BatchConfigurer, one of the data sources should be annotated with '@Primary'.",
exception);
} catch (NoSuchBeanDefinitionException exception) {
throw new IllegalStateException(
"To use the default BatchConfigurer, the application context must contain at least one data source.",
exception);
}
DataSource dataSource = getDataSource();
DefaultBatchConfigurer configurer = new DefaultBatchConfigurer(dataSource);
configurer.initialize();
this.configurer = configurer;
return configurer;
return this.configurer;
}
if (configurers.size() > 1) {
throw new IllegalStateException(
@@ -183,4 +171,21 @@ public abstract class AbstractBatchConfiguration implements ImportAware, Initial
return this.configurer;
}
private DataSource getDataSource() {
DataSource dataSource;
try {
dataSource = this.context.getBean(DataSource.class);
} catch (NoUniqueBeanDefinitionException exception) {
throw new IllegalStateException(
"Multiple data sources are defined in the application context and no primary candidate was found. " +
"To use the default BatchConfigurer, one of the data sources should be annotated with '@Primary'.",
exception);
} catch (NoSuchBeanDefinitionException exception) {
throw new IllegalStateException(
"To use the default BatchConfigurer, the application context must contain at least one data source.",
exception);
}
return dataSource;
}
}

View File

@@ -42,23 +42,6 @@ public class DefaultBatchConfigurer implements BatchConfigurer {
private JobLauncher jobLauncher;
private JobExplorer jobExplorer;
/**
* Sets the dataSource.
*
* @param dataSource The data source to use. Must not be {@code null}.
*/
public void setDataSource(DataSource dataSource) {
Assert.notNull(dataSource, "DataSource must not be null");
this.dataSource = dataSource;
}
/**
* @return The {@link DataSource} used by the {@link DefaultBatchConfigurer}.
*/
public DataSource getDataSource() {
return this.dataSource;
}
/**
* Create a new {@link DefaultBatchConfigurer} with the passed datasource. This constructor
* will configure a default {@link DataSourceTransactionManager}.
@@ -81,24 +64,42 @@ public class DefaultBatchConfigurer implements BatchConfigurer {
this.transactionManager = transactionManager;
}
@Override
public JobRepository getJobRepository() {
return jobRepository;
/**
* Sets the dataSource.
*
* @param dataSource The data source to use. Must not be {@code null}.
*/
public void setDataSource(DataSource dataSource) {
Assert.notNull(dataSource, "DataSource must not be null");
this.dataSource = dataSource;
}
/**
* @return The {@link DataSource} used by the {@link DefaultBatchConfigurer}.
*/
public DataSource getDataSource() {
return this.dataSource;
}
@Override
public PlatformTransactionManager getTransactionManager() {
return transactionManager;
public JobRepository getJobRepository() {
return this.jobRepository;
}
@Override
public JobLauncher getJobLauncher() {
return jobLauncher;
return this.jobLauncher;
}
@Override
public JobExplorer getJobExplorer() {
return jobExplorer;
return this.jobExplorer;
}
@Override
public PlatformTransactionManager getTransactionManager() {
return this.transactionManager;
}
/**

View File

@@ -22,6 +22,7 @@ import org.springframework.batch.core.repository.JobRepository;
* Convenient factory for a {@link JobBuilder} which sets the {@link JobRepository} automatically.
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
public class JobBuilderFactory {
@@ -43,8 +44,7 @@ public class JobBuilderFactory {
* @return a job builder
*/
public JobBuilder get(String name) {
JobBuilder builder = new JobBuilder(name).repository(jobRepository);
return builder;
return new JobBuilder(name).repository(this.jobRepository);
}
}

View File

@@ -24,6 +24,7 @@ import org.springframework.transaction.PlatformTransactionManager;
* {@link PlatformTransactionManager} automatically.
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
public class StepBuilderFactory {
@@ -51,9 +52,9 @@ public class StepBuilderFactory {
* @return a step builder
*/
public StepBuilder get(String name) {
StepBuilder builder = new StepBuilder(name).repository(jobRepository).transactionManager(
transactionManager);
return builder;
return new StepBuilder(name)
.repository(this.jobRepository)
.transactionManager(this.transactionManager);
}
}