diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ClassPathXmlApplicationContextFactory.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ClassPathXmlApplicationContextFactory.java index dd3d4cc07..ad369a0de 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ClassPathXmlApplicationContextFactory.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ClassPathXmlApplicationContextFactory.java @@ -238,6 +238,11 @@ public class ClassPathXmlApplicationContextFactory implements ApplicationContext super.customizeBeanFactory(beanFactory); if (parentBeanFactory != null) { ClassPathXmlApplicationContextFactory.this.prepareBeanFactory(parentBeanFactory, beanFactory); + for (Class cls : beanFactoryPostProcessorClasses) { + for (String name : parent.getBeanNamesForType(cls)) { + beanFactory.registerSingleton(name, ((BeanFactoryPostProcessor) parent.getBean(name))); + } + } } } @@ -245,10 +250,10 @@ public class ClassPathXmlApplicationContextFactory implements ApplicationContext protected Resource[] getConfigResources() { return new Resource[] { resource }; } - + @Override public String toString() { - return "ResourceXmlApplicationContext:"+getId(); + return "ResourceXmlApplicationContext:" + getId(); } } @@ -256,10 +261,7 @@ public class ClassPathXmlApplicationContextFactory implements ApplicationContext /** * Extension point for special subclasses that want to do more complex * things with the context prior to refresh. The default implementation - * copies bean factory post processors according to the values requested. - * The bean factory for the context will be available if needed through - * {@link ConfigurableApplicationContext#getBeanFactory() - * context.getBeanFactory()}. + * does nothing. * * @param parent the parent for the new application context * @param context the new application context before it is refreshed, but @@ -268,11 +270,6 @@ public class ClassPathXmlApplicationContextFactory implements ApplicationContext * @see ClassPathXmlApplicationContextFactory#setBeanFactoryPostProcessorClasses(Class[]) */ protected void prepareContext(ConfigurableApplicationContext parent, ConfigurableApplicationContext context) { - for (Class cls : beanFactoryPostProcessorClasses) { - for (String name : parent.getBeanNamesForType(cls)) { - context.addBeanFactoryPostProcessor((BeanFactoryPostProcessor) parent.getBean(name)); - } - } } /** diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/ClassPathXmlApplicationContextFactoryTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/ClassPathXmlApplicationContextFactoryTests.java index 7e545d97b..8d22a6326 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/ClassPathXmlApplicationContextFactoryTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/ClassPathXmlApplicationContextFactoryTests.java @@ -64,6 +64,17 @@ public class ClassPathXmlApplicationContextFactoryTests { assertEquals(4, ((Foo) context.getBean("foo", Foo.class)).values[1], 0.01); } + @Test + public void testBeanFactoryPostProcessorOrderRespected() { + factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath( + getClass(), "parent-context.xml"))); + factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), + "placeholder-context.xml"))); + ConfigurableApplicationContext context = factory.createApplicationContext(); + assertEquals("test-job", context.getBeanNamesForType(Job.class)[0]); + assertEquals("spam", ((Job) context.getBean("test-job", Job.class)).getName()); + } + @Test public void testBeanFactoryPostProcessorsNotCopied() { factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath( diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/support/parent-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/support/parent-context.xml index c88fec662..83a1035e3 100644 --- a/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/support/parent-context.xml +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/support/parent-context.xml @@ -8,6 +8,7 @@ + diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/support/placeholder-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/support/placeholder-context.xml new file mode 100644 index 000000000..13c3dfe96 --- /dev/null +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/support/placeholder-context.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + +