From 72d9177cf1484d548cfa7956e22455f2b1d32263 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Sun, 15 May 2022 01:54:12 +0200 Subject: [PATCH] Minor refactoring - Extract/Rearrange methods - Inline variables - Improve Javadoc formatting --- .../AbstractBatchConfiguration.java | 51 ++++++++++--------- .../annotation/DefaultBatchConfigurer.java | 49 +++++++++--------- .../annotation/JobBuilderFactory.java | 4 +- .../annotation/StepBuilderFactory.java | 7 +-- 4 files changed, 59 insertions(+), 52 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/AbstractBatchConfiguration.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/AbstractBatchConfiguration.java index 7c6918789..4d720b4e6 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/AbstractBatchConfiguration.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/AbstractBatchConfiguration.java @@ -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 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; + } + } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/DefaultBatchConfigurer.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/DefaultBatchConfigurer.java index 24f88e60c..328450ccf 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/DefaultBatchConfigurer.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/DefaultBatchConfigurer.java @@ -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; } /** diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/JobBuilderFactory.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/JobBuilderFactory.java index ff8ed1191..a5d91a704 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/JobBuilderFactory.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/JobBuilderFactory.java @@ -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); } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/StepBuilderFactory.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/StepBuilderFactory.java index bbcb51ecf..72fa4ecee 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/StepBuilderFactory.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/StepBuilderFactory.java @@ -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); } }