From cc6be985ad614777d31ea177827fa4d4b607eeab Mon Sep 17 00:00:00 2001 From: Thomas Bosch Date: Mon, 12 May 2014 13:29:11 +0200 Subject: [PATCH] Allow multiple configs (xmls, java-files, packages) to be loaded in ApplicationContextFactory --- .../AbstractApplicationContextFactory.java | 18 +++++----- .../GenericApplicationContextFactory.java | 36 +++++++++---------- ...GenericApplicationContextFactoryTests.java | 34 ++++++++++++++++++ .../support/abstract-context.xml | 4 +-- 4 files changed, 63 insertions(+), 29 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/AbstractApplicationContextFactory.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/AbstractApplicationContextFactory.java index f89c25d6b..86ed7749a 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/AbstractApplicationContextFactory.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/AbstractApplicationContextFactory.java @@ -16,11 +16,6 @@ package org.springframework.batch.core.configuration.support; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; @@ -39,6 +34,11 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.util.Assert; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + /** * {@link ApplicationContextFactory} implementation that takes a parent context and a path to the context to create. * When createApplicationContext method is called, the child {@link ApplicationContext} will be returned. The child @@ -61,12 +61,12 @@ public abstract class AbstractApplicationContextFactory implements ApplicationCo private Collection> beanPostProcessorExcludeClasses; /** - * Create a factory instance with the resource specified. The resource is a Spring configuration file or java - * package containing configuration files. + * Create a factory instance with the resource specified. The resources are Spring configuration files or java + * packages containing configuration files. */ - public AbstractApplicationContextFactory(Object... resources) { + public AbstractApplicationContextFactory(Object... resource) { - this.resources = resources; + this.resources = resource; beanFactoryPostProcessorClasses = new ArrayList>(); beanFactoryPostProcessorClasses.add(PropertyPlaceholderConfigurer.class); beanFactoryPostProcessorClasses.add(PropertySourcesPlaceholderConfigurer.class); diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/GenericApplicationContextFactory.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/GenericApplicationContextFactory.java index 013038767..f9db72d15 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/GenericApplicationContextFactory.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/GenericApplicationContextFactory.java @@ -46,7 +46,7 @@ public class GenericApplicationContextFactory extends AbstractApplicationContext * Create an application context factory for the resource specified. The resource can be an actual {@link Resource}, * in which case it will be interpreted as an XML file, or it can be a @Configuration class, or a package name. * - * @param resources a resource (XML configuration file, @Configuration class or java package to scan) + * @param resources some resources (XML configuration files, @Configuration classes or java packages to scan) */ public GenericApplicationContextFactory(Object... resources) { super(resources); @@ -59,13 +59,13 @@ public class GenericApplicationContextFactory extends AbstractApplicationContext protected ConfigurableApplicationContext createApplicationContext(ConfigurableApplicationContext parent, Object... resources) { if (allObjectsOfType(resources, Resource.class)) { - return new ResourceXmlApplicationContext(parent, resources); + return new ResourceXmlApplicationContext(parent, resources); } if (allObjectsOfType(resources, Class.class)) { - return new ResourceAnnotationApplicationContext(parent, resources); + return new ResourceAnnotationApplicationContext(parent, resources); } if (allObjectsOfType(resources, String.class)) { - return new ResourceAnnotationApplicationContext(parent, resources); + return new ResourceAnnotationApplicationContext(parent, resources); } List> types = new ArrayList>(); for (Object resource : resources) { @@ -91,7 +91,7 @@ public class GenericApplicationContextFactory extends AbstractApplicationContext private final ConfigurableApplicationContext parent; public ApplicationContextHelper(ConfigurableApplicationContext parent, GenericApplicationContext context, - Object... configs) { + Object... config) { this.parent = parent; if (parent != null) { Assert.isTrue(parent.getBeanFactory() instanceof DefaultListableBeanFactory, @@ -102,8 +102,8 @@ public class GenericApplicationContextFactory extends AbstractApplicationContext parentBeanFactory = null; } context.setParent(parent); - context.setId(generateId(configs)); - loadConfiguration(configs); + context.setId(generateId(config)); + loadConfiguration(config); prepareContext(parent, context); } @@ -136,21 +136,21 @@ public class GenericApplicationContextFactory extends AbstractApplicationContext @Override protected String generateId(Object... configs) { Resource[] resources = Arrays.copyOfRange(configs, 0, configs.length, Resource[].class); - try { - List uris = new ArrayList(); - for (Resource resource : resources) { - uris.add(resource.getURI().toString()); - } - return StringUtils.collectionToCommaDelimitedString(uris); - } - catch (IOException e) { - return Arrays.toString(resources); - } + try { + List uris = new ArrayList(); + for (Resource resource : resources) { + uris.add(resource.getURI().toString()); + } + return StringUtils.collectionToCommaDelimitedString(uris); + } + catch (IOException e) { + return Arrays.toString(resources); + } } @Override protected void loadConfiguration(Object... configs) { Resource[] resources = Arrays.copyOfRange(configs, 0, configs.length, Resource[].class); - load(resources); + load(resources); } }; refresh(); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/GenericApplicationContextFactoryTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/GenericApplicationContextFactoryTests.java index 398ea4858..e1cf59e16 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/GenericApplicationContextFactoryTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/GenericApplicationContextFactoryTests.java @@ -136,6 +136,40 @@ public class GenericApplicationContextFactoryTests { assertEquals(other, factory); assertEquals(other.hashCode(), factory.hashCode()); } + + @Test + public void testEqualsMultileConfigs() throws Exception { + Resource resource1 = new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), + "abstract-context.xml")); + Resource resource2 = new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), + "child-context-with-abstract-job.xml")); + GenericApplicationContextFactory factory = new GenericApplicationContextFactory(resource1, resource2); + GenericApplicationContextFactory other = new GenericApplicationContextFactory(resource1, resource2); + assertEquals(other, factory); + assertEquals(other.hashCode(), factory.hashCode()); + } + + @Test + public void testParentConfigurationInheritedMultipleConfigs() { + Resource resource1 = new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), + "abstract-context.xml")); + Resource resource2 = new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), + "child-context-with-abstract-job.xml")); + GenericApplicationContextFactory factory = new GenericApplicationContextFactory(resource1, resource2); + ConfigurableApplicationContext context = factory.createApplicationContext(); + assertEquals("concrete-job", context.getBeanNamesForType(Job.class)[0]); + assertEquals("bar", context.getBean("concrete-job", Job.class).getName()); + assertEquals(4, context.getBean("foo", Foo.class).values[1], 0.01); + assertNotNull(context.getBean("concrete-job", JobSupport.class).getStep("step31")); + assertNotNull(context.getBean("concrete-job", JobSupport.class).getStep("step32")); + boolean autowiredFound = false; + for (BeanPostProcessor postProcessor : ((AbstractBeanFactory) context.getBeanFactory()).getBeanPostProcessors()) { + if (postProcessor instanceof AutowiredAnnotationBeanPostProcessor) { + autowiredFound = true; + } + } + assertTrue(autowiredFound); + } @Test public void testEqualsMultipleConfigs() throws Exception { diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/support/abstract-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/support/abstract-context.xml index 598146c05..e64b43eed 100644 --- a/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/support/abstract-context.xml +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/support/abstract-context.xml @@ -21,7 +21,7 @@ - + @@ -34,4 +34,4 @@ - + \ No newline at end of file