diff --git a/core/src/main/java/org/springframework/batch/core/configuration/JobConfiguration.java b/core/src/main/java/org/springframework/batch/core/configuration/JobConfiguration.java index 5541f1b2c..c0aaf152e 100644 --- a/core/src/main/java/org/springframework/batch/core/configuration/JobConfiguration.java +++ b/core/src/main/java/org/springframework/batch/core/configuration/JobConfiguration.java @@ -59,12 +59,27 @@ public class JobConfiguration implements BeanNameAware { } /** - * The callback from {@link BeanNameAware} comes after the setters, so it - * will always overwrite the name with the bean id. + * Set the name property if it is not already set. Because of the order of + * the callbacks in a Spring container the name property will be set first + * if it is present. Care is needed with bean definition inheritance - if a + * parent bean has a name, then its children need an explicit name as well, + * otherwise they will not be unique. * * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String) */ public void setBeanName(String name) { + if (this.name==null) { + this.name = name; + } + } + + /** + * Set the name property. Always overrides the default value if this object + * is a Spring bean. + * + * @see #setBeanName(java.lang.String) + */ + public void setName(String name) { this.name = name; } diff --git a/core/src/main/java/org/springframework/batch/core/configuration/StepConfigurationSupport.java b/core/src/main/java/org/springframework/batch/core/configuration/StepConfigurationSupport.java index 56bc7b50e..089d1c2b6 100644 --- a/core/src/main/java/org/springframework/batch/core/configuration/StepConfigurationSupport.java +++ b/core/src/main/java/org/springframework/batch/core/configuration/StepConfigurationSupport.java @@ -59,12 +59,27 @@ public class StepConfigurationSupport implements StepConfiguration, } /** - * Set the name property. Always overrides the default value if this - * object is a Spring bean. + * Set the name property if it is not already set. Because of the order of + * the callbacks in a Spring container the name property will be set first + * if it is present. Care is needed with bean definition inheritance - if a + * parent bean has a name, then its children need an explicit name as well, + * otherwise they will not be unique. * * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String) */ public void setBeanName(String name) { + if (this.name==null) { + this.name = name; + } + } + + /** + * Set the name property. Always overrides the default value if this object + * is a Spring bean. + * + * @see #setBeanName(java.lang.String) + */ + public void setName(String name) { this.name = name; } diff --git a/core/src/test/java/org/springframework/batch/core/configuration/JobConfigurationTests.java b/core/src/test/java/org/springframework/batch/core/configuration/JobConfigurationTests.java index 1007f6918..778b62cf0 100644 --- a/core/src/test/java/org/springframework/batch/core/configuration/JobConfigurationTests.java +++ b/core/src/test/java/org/springframework/batch/core/configuration/JobConfigurationTests.java @@ -42,7 +42,7 @@ public class JobConfigurationTests extends TestCase { */ public void testSetBeanName() { configuration.setBeanName("foo"); - assertEquals("foo", configuration.getName()); + assertEquals("job", configuration.getName()); } /** diff --git a/core/src/test/java/org/springframework/batch/core/configuration/SpringBeanJobConfigurationTests.java b/core/src/test/java/org/springframework/batch/core/configuration/SpringBeanJobConfigurationTests.java index 566c30540..689e9a088 100644 --- a/core/src/test/java/org/springframework/batch/core/configuration/SpringBeanJobConfigurationTests.java +++ b/core/src/test/java/org/springframework/batch/core/configuration/SpringBeanJobConfigurationTests.java @@ -47,8 +47,8 @@ public class SpringBeanJobConfigurationTests extends TestCase { JobConfiguration configuration = (JobConfiguration) context .getBean("bean"); assertNotNull(configuration.getName()); - assertEquals("bean", configuration.getName()); - configuration.setBeanName("foo"); + assertEquals("foo", configuration.getName()); + configuration.setBeanName("bar"); assertEquals("foo", configuration.getName()); } @@ -62,8 +62,10 @@ public class SpringBeanJobConfigurationTests extends TestCase { JobConfiguration configuration = (JobConfiguration) context .getBean("bean"); assertNotNull(configuration.getName()); - assertEquals("bean", configuration.getName()); + assertEquals("bar", configuration.getName()); configuration.setBeanName("foo"); + assertEquals("bar", configuration.getName()); + configuration.setName("foo"); assertEquals("foo", configuration.getName()); } } diff --git a/execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepConfigurationTests.java b/execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepConfigurationTests.java index c2f4ad5d0..e1f867316 100644 --- a/execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepConfigurationTests.java +++ b/execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepConfigurationTests.java @@ -61,23 +61,6 @@ public class SimpleStepConfigurationTests extends TestCase { assertEquals(20, configuration.getCommitInterval()); } - /** - * Test method for {@link org.springframework.batch.execution.step.AbstractStepConfiguration#setBeanName(java.lang.String)}. - */ - public void testSetBeanName() { - configuration.setBeanName("bar"); - assertEquals("bar", configuration.getName()); - } - - /** - * Test method for {@link org.springframework.batch.execution.step.AbstractStepConfiguration#setBeanName(java.lang.String)}. - */ - public void testSetBeanNameOverrideNull() { - configuration = new SimpleStepConfiguration(); - configuration.setBeanName("bar"); - assertEquals("bar", configuration.getName()); - } - /** * Test method for {@link org.springframework.batch.execution.step.AbstractStepConfiguration#getExceptionHandler()}. */