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 f9db72d15..74e37425b 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 @@ -16,11 +16,6 @@ package org.springframework.batch.core.configuration.support; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory; @@ -33,6 +28,11 @@ import org.springframework.core.io.Resource; import org.springframework.util.Assert; import org.springframework.util.StringUtils; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +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 @@ -45,6 +45,8 @@ 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. + * All types must be the same (mixing XML with a java package for example is not allowed and will result in an + * {@link java.lang.IllegalArgumentException}). * * @param resources some resources (XML configuration files, @Configuration classes or java packages to scan) */ @@ -58,21 +60,24 @@ public class GenericApplicationContextFactory extends AbstractApplicationContext @Override protected ConfigurableApplicationContext createApplicationContext(ConfigurableApplicationContext parent, Object... resources) { + ConfigurableApplicationContext context; + if (allObjectsOfType(resources, Resource.class)) { - return new ResourceXmlApplicationContext(parent, resources); + context = new ResourceXmlApplicationContext(parent, resources); + } else if (allObjectsOfType(resources, Class.class)) { + context = new ResourceAnnotationApplicationContext(parent, resources); + } else if (allObjectsOfType(resources, String.class)) { + context = new ResourceAnnotationApplicationContext(parent, resources); + } else { + List> types = new ArrayList>(); + for (Object resource : resources) { + types.add(resource.getClass()); + } + throw new IllegalArgumentException("No application context could be created for resource types: " + + Arrays.toString(types.toArray())); } - if (allObjectsOfType(resources, Class.class)) { - return new ResourceAnnotationApplicationContext(parent, resources); - } - if (allObjectsOfType(resources, String.class)) { - return new ResourceAnnotationApplicationContext(parent, resources); - } - List> types = new ArrayList>(); - for (Object resource : resources) { - types.add(resource.getClass()); - } - throw new IllegalArgumentException("No application context could be created for resource types: " - + Arrays.toString(types.toArray())); + + return context; } private boolean allObjectsOfType(Object[] objects, Class type) { 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 e1cf59e16..1793c1e7c 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 @@ -15,10 +15,6 @@ */ package org.springframework.batch.core.configuration.support; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import org.junit.Test; import org.springframework.batch.core.Job; import org.springframework.batch.core.job.JobSupport; @@ -27,11 +23,17 @@ import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.support.AbstractBeanFactory; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.util.ClassUtils; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + /** * @author Dave Syer * @@ -171,38 +173,34 @@ public class GenericApplicationContextFactoryTests { assertTrue(autowiredFound); } - @Test - public void testEqualsMultipleConfigs() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void testDifferentResourceTypes() 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()); + "abstract-context.xml")); + GenericApplicationContextFactory factory = new GenericApplicationContextFactory(resource1, Configuration1.class); + factory.createApplicationContext(); } @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); + public void testPackageScanning() throws Exception { + GenericApplicationContextFactory factory = new GenericApplicationContextFactory("org.springframework.batch.core.configuration.support"); 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); + + assertEquals(context.getBean("bean1"), "bean1"); + assertEquals(context.getBean("bean2"), "bean2"); + assertEquals(context.getBean("bean3"), "bean3"); + assertEquals(context.getBean("bean4"), "bean4"); + } + + @Test + public void testMultipleConfigurationClasses() throws Exception { + GenericApplicationContextFactory factory = new GenericApplicationContextFactory(Configuration1.class, Configuration2.class); + ConfigurableApplicationContext context = factory.createApplicationContext(); + + assertEquals(context.getBean("bean1"), "bean1"); + assertEquals(context.getBean("bean2"), "bean2"); + assertEquals(context.getBean("bean3"), "bean3"); + assertEquals(context.getBean("bean4"), "bean4"); } public static class Foo { @@ -212,4 +210,30 @@ public class GenericApplicationContextFactoryTests { this.values = values; } } + + @Configuration + public static class Configuration1 { + @Bean + public String bean1() { + return "bean1"; + } + + @Bean + public String bean2() { + return "bean2"; + } + } + + @Configuration + public static class Configuration2 { + @Bean + public String bean3() { + return "bean3"; + } + + @Bean + public String bean4() { + return "bean4"; + } + } }