/** * Create an {@link ApplicationContextFactory} from each resource provided * in {@link #setResources(Resource[])}. * * @return an array of {@link ApplicationContextFactory} * @see org.springframework.beans.factory.FactoryBean#getObject() */ @Override public ApplicationContextFactory[] getObject() throws Exception { if (resources == null) { return new ApplicationContextFactory[0]; } List<ApplicationContextFactory> applicationContextFactories = new ArrayList<>(); for (Resource resource : resources) { GenericApplicationContextFactory factory = new GenericApplicationContextFactory(resource); factory.setCopyConfiguration(copyConfiguration); if (beanFactoryPostProcessorClasses != null) { factory.setBeanFactoryPostProcessorClasses(beanFactoryPostProcessorClasses); } if (beanPostProcessorExcludeClasses != null) { factory.setBeanPostProcessorExcludeClasses(beanPostProcessorExcludeClasses); } factory.setApplicationContext(applicationContext); applicationContextFactories.add(factory); } return applicationContextFactories.toArray(new ApplicationContextFactory[applicationContextFactories.size()]); }
/** * @see AbstractApplicationContextFactory#createApplicationContext(ConfigurableApplicationContext, Object...) */ @Override protected ConfigurableApplicationContext createApplicationContext(ConfigurableApplicationContext parent, Object... resources) { ConfigurableApplicationContext context; if (allObjectsOfType(resources, Resource.class)) { 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<Class<?>> 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; }
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { if (parentBeanFactory != null) { GenericApplicationContextFactory.this.prepareBeanFactory(parentBeanFactory, beanFactory); for (Class<? extends BeanFactoryPostProcessor> cls : getBeanFactoryPostProcessorClasses()) { for (String name : parent.getBeanNamesForType(cls)) { beanFactory.registerSingleton(name, (parent.getBean(name))); } } } }
private void register(String[] paths) throws DuplicateJobException, IOException { maybeCreateJobLoader(); for (int i = 0; i < paths.length; i++) { Resource[] resources = parentContext.getResources(paths[i]); for (int j = 0; j < resources.length; j++) { Resource path = resources[j]; logger.info("Registering Job definitions from " + Arrays.toString(resources)); GenericApplicationContextFactory factory = new GenericApplicationContextFactory(path); factory.setApplicationContext(parentContext); jobLoader.load(factory); } } }
@Bean public ApplicationContextFactory jobContextFactory() { return new GenericApplicationContextFactory(TestConfiguration.class); }
@Test public void testParentChildLifecycleEvents() throws InterruptedException { AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(ParentContext.class); GenericApplicationContextFactory child = new GenericApplicationContextFactory(ChildContextConfiguration.class); child.setApplicationContext(parent); ApplicationContext context = child.createApplicationContext(); ChildBean bean = context.getBean(ChildBean.class); assertEquals(1, bean.counter1); assertEquals(1, bean.counter2); }
@SuppressWarnings("resource") @Test public void testBeanFactoryConfigurationNotCopied() { GenericApplicationContextFactory factory = new GenericApplicationContextFactory(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml"))); factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath( getClass(), "parent-context.xml"))); factory.setCopyConfiguration(false); ConfigurableApplicationContext context = factory.createApplicationContext(); assertEquals("test-job", context.getBeanNamesForType(Job.class)[0]); assertEquals("bar", context.getBean("test-job", Job.class).getName()); // The CustomEditorConfigurer is a BeanFactoryPostProcessor so the // editor gets copied anyway! assertEquals(4, context.getBean("foo", Foo.class).values[1], 0.01); }
@SuppressWarnings("resource") @Test public void testBeanFactoryPostProcessorsNotCopied() { GenericApplicationContextFactory factory = new GenericApplicationContextFactory( new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml"))); factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath( getClass(), "parent-context.xml"))); @SuppressWarnings("unchecked") Class<? extends BeanFactoryPostProcessor>[] classes = (Class<? extends BeanFactoryPostProcessor>[]) new Class<?>[0]; factory.setBeanFactoryPostProcessorClasses(classes); ConfigurableApplicationContext context = factory.createApplicationContext(); assertEquals("test-job", context.getBeanNamesForType(Job.class)[0]); assertEquals("${foo}", context.getBean("test-job", Job.class).getName()); assertEquals(4, context.getBean("foo", Foo.class).values[1], 0.01); }
@Test(expected = IllegalArgumentException.class) public void testDifferentResourceTypes() throws Exception { Resource resource1 = new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "abstract-context.xml")); GenericApplicationContextFactory factory = new GenericApplicationContextFactory(resource1, Configuration1.class); factory.createApplicationContext(); }
@Test public void testEquals() throws Exception { Resource resource = new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml")); GenericApplicationContextFactory factory = new GenericApplicationContextFactory(resource); GenericApplicationContextFactory other = new GenericApplicationContextFactory(resource); assertEquals(other, factory); assertEquals(other.hashCode(), factory.hashCode()); }
public ApplicationContextHelper(ConfigurableApplicationContext parent, GenericApplicationContext context, Object... config) { this.parent = parent; if (parent != null) { Assert.isTrue(parent.getBeanFactory() instanceof DefaultListableBeanFactory, "The parent application context must have a bean factory of type DefaultListableBeanFactory"); parentBeanFactory = (DefaultListableBeanFactory) parent.getBeanFactory(); } else { parentBeanFactory = null; } context.setParent(parent); context.setId(generateId(config)); loadConfiguration(config); prepareContext(parent, context); }
@Bean public ApplicationContextFactory vanillaContextFactory() { return new GenericApplicationContextFactory(VanillaConfiguration.class); }
private void setUpApplicationContextFactories(Resource[] jobPaths, ApplicationContext parent) { Collection<ApplicationContextFactory> applicationContextFactories = new ArrayList<>(); for (Resource resource : jobPaths) { GenericApplicationContextFactory factory = new GenericApplicationContextFactory(resource); factory.setApplicationContext(parent); applicationContextFactories.add(factory); } registrar.setApplicationContextFactories(applicationContextFactories .toArray(new ApplicationContextFactory[jobPaths.length])); }
@SuppressWarnings("resource") @Test public void testBeanFactoryPostProcessorOrderRespected() { GenericApplicationContextFactory factory = new GenericApplicationContextFactory( new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "placeholder-context.xml"))); factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath( getClass(), "parent-context.xml"))); ConfigurableApplicationContext context = factory.createApplicationContext(); assertEquals("test-job", context.getBeanNamesForType(Job.class)[0]); assertEquals("spam", context.getBean("test-job", Job.class).getName()); }
@Test public void testGetJobName() { GenericApplicationContextFactory factory = new GenericApplicationContextFactory( new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "trivial-context.xml"))); assertEquals("test-job", factory.createApplicationContext().getBeanNamesForType(Job.class)[0]); }
@Test public void testEqualsMultipleConfigs() 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()); }
public ApplicationContextHelper(ConfigurableApplicationContext parent, GenericApplicationContext context, Object... config) { this.parent = parent; if (parent != null) { Assert.isTrue(parent.getBeanFactory() instanceof DefaultListableBeanFactory, "The parent application context must have a bean factory of type DefaultListableBeanFactory"); parentBeanFactory = (DefaultListableBeanFactory) parent.getBeanFactory(); } else { parentBeanFactory = null; } context.setParent(parent); context.setId(generateId(config)); loadConfiguration(config); prepareContext(parent, context); }
/** * Create an {@link ApplicationContextFactory} from each resource provided * in {@link #setResources(Resource[])}. * * @return an array of {@link ApplicationContextFactory} * @see org.springframework.beans.factory.FactoryBean#getObject() */ @Override public ApplicationContextFactory[] getObject() throws Exception { if (resources == null) { return new ApplicationContextFactory[0]; } List<ApplicationContextFactory> applicationContextFactories = new ArrayList<>(); for (Resource resource : resources) { GenericApplicationContextFactory factory = new GenericApplicationContextFactory(resource); factory.setCopyConfiguration(copyConfiguration); if (beanFactoryPostProcessorClasses != null) { factory.setBeanFactoryPostProcessorClasses(beanFactoryPostProcessorClasses); } if (beanPostProcessorExcludeClasses != null) { factory.setBeanPostProcessorExcludeClasses(beanPostProcessorExcludeClasses); } factory.setApplicationContext(applicationContext); applicationContextFactories.add(factory); } return applicationContextFactories.toArray(new ApplicationContextFactory[applicationContextFactories.size()]); }
@PostConstruct public void initialize() { registrar.addApplicationContextFactory(new GenericApplicationContextFactory(TestConfiguration.class)); registrar.addApplicationContextFactory(new GenericApplicationContextFactory(VanillaConfiguration.class)); }
/** * Create an application context from the resource provided. Extension point * for subclasses if they need to customize the context in any way. The * default uses a {@link GenericApplicationContextFactory}. * * @param parent the parent application context (or null if there is none) * @param resource the location of the XML configuration * * @return an application context containing jobs */ protected ApplicationContextFactory createApplicationContextFactory(ApplicationContext parent, Resource resource) { GenericApplicationContextFactory applicationContextFactory = new GenericApplicationContextFactory(resource); if (parent != null) { applicationContextFactory.setApplicationContext(parent); } return applicationContextFactory; }