Conditionally add custom scopes when using XML configuration

When using a combination of Java configuration and the XML namespace
configuration, prior to this commit, we would add the batch custom
scopes twice which lead to issues.  This commit gives priority to the
custom scopes provided by the java configuration.  If you've used
@EnableBatchProcessing and have configured batch components using the
batch namespace, the custom scopes that come with the java config will
take precidence.

This commit addresses Jira BATCH-2266
This commit is contained in:
Michael Minella
2014-09-22 13:45:43 -05:00
parent fc2431332b
commit a2f2df0b17
4 changed files with 71 additions and 16 deletions

View File

@@ -16,29 +16,35 @@
package org.springframework.batch.core.configuration.annotation;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.Callable;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.scope.context.StepSynchronizationManager;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.concurrent.Callable;
import static org.junit.Assert.assertEquals;
/**
* @author Dave Syer
* @author Michael Minella
*
*/
public class StepScopeConfigurationTests {
@@ -85,6 +91,16 @@ public class StepScopeConfigurationTests {
assertEquals("STEP", value.call());
}
@Test
public void testStepScopeXmlImportUsingNamespace() throws Exception {
init(StepScopeConfigurationXmlImportUsingNamespace.class);
SimpleHolder value = (SimpleHolder) context.getBean("xmlValue");
assertEquals("STEP", value.call());
value = (SimpleHolder) context.getBean("javaValue");
assertEquals("STEP", value.call());
}
@Test
public void testStepScopeWithProxyTargetClassInjected() throws Exception {
init(StepScopeConfigurationInjectingProxy.class);
@@ -199,6 +215,20 @@ public class StepScopeConfigurationTests {
}
@Configuration
@ImportResource("org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTestsXmlImportUsingNamespace-context.xml")
@EnableBatchProcessing
public static class StepScopeConfigurationXmlImportUsingNamespace {
@Bean
@StepScope
protected SimpleHolder javaValue(@Value("#{stepExecution.stepName}")
final String value) {
return new SimpleHolder(value);
}
}
@Configuration
@EnableBatchProcessing
public static class StepScopeConfigurationInjectingProxy {
@@ -256,4 +286,11 @@ public class StepScopeConfigurationTests {
}
public static class TaskletSupport implements Tasklet {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
return RepeatStatus.FINISHED;
}
}
}