diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/scope/StepScope.java b/spring-batch-core/src/main/java/org/springframework/batch/core/scope/StepScope.java index c8380c039..2ed217130 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/scope/StepScope.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/scope/StepScope.java @@ -152,16 +152,27 @@ public class StepScope implements Scope, BeanFactoryPostProcessor, Ordered { * @throws BeansException if there is a problem. */ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + beanFactory.registerScope(name, this); + Assert.state(beanFactory instanceof BeanDefinitionRegistry, "BeanFactory was not a BeanDefinitionRegistry, so StepScope cannot be used."); - Scopifier scopifier = new Scopifier((BeanDefinitionRegistry) beanFactory, name, proxyTargetClass); + BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; + + Scopifier scopifier = new Scopifier(registry, name, proxyTargetClass); + for (String beanName : beanFactory.getBeanDefinitionNames()) { BeanDefinition definition = beanFactory.getBeanDefinition(beanName); // Replace this or any of its inner beans with scoped proxy if it // has this scope scopifier.visitBeanDefinition(definition); + if (name.equals(definition.getScope())) { + BeanDefinitionHolder proxyHolder = ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder( + definition, beanName), registry, proxyTargetClass); + registry.registerBeanDefinition(beanName, proxyHolder.getBeanDefinition()); + } } + } /** diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/scope/StepScopeIntegrationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/scope/StepScopeIntegrationTests.java index e88382ef9..d4f302d0d 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/scope/StepScopeIntegrationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/scope/StepScopeIntegrationTests.java @@ -35,6 +35,10 @@ public class StepScopeIntegrationTests { @Qualifier("enhanced") private Step enhanced; + @Autowired + @Qualifier("double") + private Step doubleEnhanced; + @Before @After public void start() { @@ -64,7 +68,25 @@ public class StepScopeIntegrationTests { ExecutionContext executionContext = new ExecutionContext(); executionContext.put("name", "spam"); stepExecution.setExecutionContext(executionContext); - enhanced.execute(stepExecution); + proxied.execute(stepExecution); + assertTrue(TestStep.getContext().attributeNames().length>0); + String collaborator = (String) TestStep.getContext().getAttribute("collaborator"); + assertNotNull(collaborator); + assertEquals("bar", collaborator); + } + + @Test + public void testScopedProxyForReference() throws Exception { + enhanced.execute(new StepExecution("foo",new JobExecution(11L))); + assertTrue(TestStep.getContext().attributeNames().length>0); + String collaborator = (String) TestStep.getContext().getAttribute("collaborator"); + assertNotNull(collaborator); + assertEquals("bar", collaborator); + } + + @Test + public void testScopedProxyForSecondReference() throws Exception { + doubleEnhanced.execute(new StepExecution("foo",new JobExecution(11L))); assertTrue(TestStep.getContext().attributeNames().length>0); String collaborator = (String) TestStep.getContext().getAttribute("collaborator"); assertNotNull(collaborator); diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/scope/StepScopeIntegrationTests-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/scope/StepScopeIntegrationTests-context.xml index 17351db4c..95608805a 100644 --- a/spring-batch-core/src/test/resources/org/springframework/batch/core/scope/StepScopeIntegrationTests-context.xml +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/scope/StepScopeIntegrationTests-context.xml @@ -6,6 +6,7 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> + @@ -13,6 +14,7 @@ + + - - - - - + - - - + + + + + + + + +