diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/CoreNamespaceUtils.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/CoreNamespaceUtils.java new file mode 100644 index 000000000..eb65e4225 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/CoreNamespaceUtils.java @@ -0,0 +1,49 @@ +/* + * Copyright 2006-2009 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.batch.core.configuration.xml; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.AbstractBeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.xml.ParserContext; + +/** + * utility methods used in parsing of the batch core namespace + * + * @author Thomas Risberg + */ +public class CoreNamespaceUtils { + + protected static void checkForStepScope(ParserContext parserContext) { + final String stepScopeClassName = "org.springframework.batch.core.scope.StepScope"; + boolean foundStepScope = false; + String[] beanNames = parserContext.getRegistry().getBeanDefinitionNames(); + for (String beanName : beanNames) { + BeanDefinition bd = parserContext.getRegistry().getBeanDefinition(beanName); + if (stepScopeClassName.equals(bd.getBeanClassName())) { + foundStepScope = true; + break; + } + } + if (!foundStepScope) { + BeanDefinitionBuilder stepScopeBuilder = + BeanDefinitionBuilder.genericBeanDefinition(stepScopeClassName); + AbstractBeanDefinition abd = stepScopeBuilder.getBeanDefinition(); + parserContext.getRegistry().registerBeanDefinition(stepScopeClassName, abd); + } + } + +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/JobParser.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/JobParser.java index e726af659..7dbeea718 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/JobParser.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/JobParser.java @@ -50,6 +50,8 @@ public class JobParser extends AbstractSingleBeanDefinitionParser { */ @Override protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + + CoreNamespaceUtils.checkForStepScope(parserContext); String jobName = element.getAttribute("id"); builder.addConstructorArgValue(jobName); @@ -86,5 +88,5 @@ public class JobParser extends AbstractSingleBeanDefinitionParser { } } - + } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/TopLevelStepParser.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/TopLevelStepParser.java index 38180792b..e4f6f2fd4 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/TopLevelStepParser.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/TopLevelStepParser.java @@ -31,6 +31,7 @@ public class TopLevelStepParser extends AbstractBeanDefinitionParser { @Override protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { + CoreNamespaceUtils.checkForStepScope(parserContext); StandaloneStepParser stepParser = new StandaloneStepParser(); return stepParser.parse(element, parserContext); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/AutoRegisteringStepScopeTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/AutoRegisteringStepScopeTests.java new file mode 100644 index 000000000..facdd69bc --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/AutoRegisteringStepScopeTests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2006-2009 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.batch.core.configuration.xml; + +import static org.junit.Assert.assertTrue; + +import java.util.Map; + +import org.junit.Test; +import org.springframework.batch.core.scope.StepScope; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + + +/** + * @author Thomas Risberg + */ +public class AutoRegisteringStepScopeTests { + + @SuppressWarnings("unchecked") + @Test + public void testJobElement() throws Exception { + ConfigurableApplicationContext ctx = + new ClassPathXmlApplicationContext("org/springframework/batch/core/configuration/xml/AutoRegisteringStepScopeForJobElementTests-context.xml"); + Map beans = ctx.getBeansOfType(StepScope.class); + assertTrue("StepScope not defined properly", beans.size() == 1); + } + + @SuppressWarnings("unchecked") + @Test + public void testStepElement() throws Exception { + ConfigurableApplicationContext ctx = + new ClassPathXmlApplicationContext("org/springframework/batch/core/configuration/xml/AutoRegisteringStepScopeForStepElementTests-context.xml"); + Map beans = ctx.getBeansOfType(StepScope.class); + assertTrue("StepScope not defined properly", beans.size() == 1); + } + +} diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/AutoRegisteringStepScopeForJobElementTests-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/AutoRegisteringStepScopeForJobElementTests-context.xml new file mode 100644 index 000000000..c0096e7de --- /dev/null +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/AutoRegisteringStepScopeForJobElementTests-context.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/AutoRegisteringStepScopeForStepElementTests-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/AutoRegisteringStepScopeForStepElementTests-context.xml new file mode 100644 index 000000000..2390ccce0 --- /dev/null +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/AutoRegisteringStepScopeForStepElementTests-context.xml @@ -0,0 +1,13 @@ + + + + + + + + + + \ No newline at end of file