/** * Create a new ClassPathXmlApplicationContext with the given parent, * loading the definitions from the given XML files and automatically * refreshing the context. * @param paths array of relative (or absolute) paths within the class path * @param clazz the class to load resources with (basis for the given paths) * @param parent the parent context * @throws BeansException if context creation failed * @see org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class) * @see org.springframework.context.support.GenericApplicationContext * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader */ public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz, @Nullable ApplicationContext parent) throws BeansException { super(parent); Assert.notNull(paths, "Path array must not be null"); Assert.notNull(clazz, "Class argument must not be null"); this.configResources = new Resource[paths.length]; for (int i = 0; i < paths.length; i++) { this.configResources[i] = new ClassPathResource(paths[i], clazz); } refresh(); }
/** * Create a new ClassPathXmlApplicationContext with the given parent, * loading the definitions from the given XML files. * @param configLocations array of resource locations * @param refresh whether to automatically refresh the context, * loading all bean definitions and creating all singletons. * Alternatively, call refresh manually after further configuring the context. * @param parent the parent context * @throws BeansException if context creation failed * @see #refresh() */ public ClassPathXmlApplicationContext( String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException { super(parent); setConfigLocations(configLocations); if (refresh) { refresh(); } }
/** * Create a new ClassPathXmlApplicationContext with the given parent, * loading the definitions from the given XML files and automatically * refreshing the context. * @param paths array of relative (or absolute) paths within the class path * @param clazz the class to load resources with (basis for the given paths) * @param parent the parent context * @throws BeansException if context creation failed * @see org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class) * @see org.springframework.context.support.GenericApplicationContext * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader */ public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz, @Nullable ApplicationContext parent) throws BeansException { super(parent); Assert.notNull(paths, "Path array must not be null"); Assert.notNull(clazz, "Class argument must not be null"); this.configResources = new Resource[paths.length]; for (int i = 0; i < paths.length; i++) { this.configResources[i] = new ClassPathResource(paths[i], clazz); } refresh(); }
/** * Create a new ClassPathXmlApplicationContext with the given parent, * loading the definitions from the given XML files. * @param configLocations array of resource locations * @param refresh whether to automatically refresh the context, * loading all bean definitions and creating all singletons. * Alternatively, call refresh manually after further configuring the context. * @param parent the parent context * @throws BeansException if context creation failed * @see #refresh() */ public ClassPathXmlApplicationContext( String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException { super(parent); setConfigLocations(configLocations); if (refresh) { refresh(); } }
@Before public void setUp() throws Exception { File f = new File("target/foo.properties"); if (f.exists()) f.delete(); ctx = new ClassPathXmlApplicationContext( "GeoServerPropertyConfigurerTest-applicationContext.xml", getClass()); ctx.refresh(); }
@Override public void refresh() throws BeansException { try { super.refresh(); } catch (BeanCreationException ex) { DefaultListableBeanFactory factory = (DefaultListableBeanFactory) getBeanFactory(); assertEquals(0, factory.getSingletonCount()); throw ex; } } };
@Before public void setUp() throws Exception { ctx = new ClassPathXmlApplicationContext( "GeoServerDataDirectoryTest-applicationContext.xml", getClass()); ctx.refresh(); dataDir = new GeoServerDataDirectory(Files.createTempDirectory("data").toFile()); dataDir.root().deleteOnExit(); }
@Test public void testAllowBeanOverride() { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); applicationContext.setConfigLocation("org/springframework/batch/core/configuration/xml/BeanDefinitionOverrideTests-context.xml"); applicationContext.refresh(); }
@Test public void testUserSpecified() throws Exception { Properties p = new Properties(); p.put("prop1", "foobar"); p.put("prop2", "barfoo"); FileOutputStream out = new FileOutputStream("target/foo.properties"); p.store(out, ""); out.flush(); out.close(); ctx.refresh(); Foo f = (Foo) ctx.getBean("myBean"); assertEquals("foobar", f.getBar()); assertEquals("barfoo", f.getBaz()); }
@Test public void testAllowBeanOverrideFalse() { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); applicationContext.setAllowBeanDefinitionOverriding(false); applicationContext.setConfigLocation("org/springframework/batch/core/configuration/xml/BeanDefinitionOverrideTests-context.xml"); applicationContext.refresh(); } }
@Test public void testAliasWithPlaceholder() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( FQ_CONTEXT_B, FQ_ALIASED_CONTEXT_C, FQ_CONTEXT_A); assertTrue(ctx.containsBean("service")); assertTrue(ctx.containsBean("logicOne")); assertTrue(ctx.containsBean("logicTwo")); ctx.refresh(); }
@Test public void registerDataValueProcessorOnlyIfNotRegistered() throws Exception { try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext()) { context.setAllowBeanDefinitionOverriding(false); context.setConfigLocation(this.xml("RegisterDataValueProcessorOnyIfNotRegistered")); context.refresh(); } }
@Test public void testContextWithInvalidValueType() throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] {INVALID_VALUE_TYPE_CONTEXT}, false); try { context.refresh(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { assertTrue(ex.contains(TypeMismatchException.class)); assertTrue(ex.toString().contains("someMessageSource")); assertTrue(ex.toString().contains("useCodeAsDefaultMessage")); checkExceptionFromInvalidValueType(ex); checkExceptionFromInvalidValueType(new ExceptionInInitializerError(ex)); assertFalse(context.isActive()); } }
@Test public void testMultipleConfigLocations() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( FQ_CONTEXT_B, FQ_CONTEXT_C, FQ_CONTEXT_A); assertTrue(ctx.containsBean("service")); assertTrue(ctx.containsBean("logicOne")); assertTrue(ctx.containsBean("logicTwo")); // re-refresh (after construction refresh) Service service = (Service) ctx.getBean("service"); ctx.refresh(); assertTrue(service.isProperlyDestroyed()); // regular close call service = (Service) ctx.getBean("service"); ctx.close(); assertTrue(service.isProperlyDestroyed()); // re-activating and re-closing the context (SPR-13425) ctx.refresh(); service = (Service) ctx.getBean("service"); ctx.close(); assertTrue(service.isProperlyDestroyed()); }
@Test public void testRemoteChunkingMasterIdAttrAssert() throws Exception { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); applicationContext.setValidating(false); applicationContext.setConfigLocation("/org/springframework/batch/integration/config/xml/RemoteChunkingMasterParserMissingIdAttrTests.xml"); try { applicationContext.refresh(); fail(); } catch (BeanDefinitionStoreException e) { assertTrue("Nested exception must be of type IllegalArgumentException", e.getCause() instanceof IllegalArgumentException); IllegalArgumentException iae = (IllegalArgumentException) e.getCause(); assertTrue("Expected: " + "The id attribute must be specified" + " but got: " + iae.getMessage(), "The id attribute must be specified".equals(iae.getMessage())); } }
@Test public void testRemoteChunkingSlaveOutputChannelAttrAssert() throws Exception { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); applicationContext.setValidating(false); applicationContext.setConfigLocation("/org/springframework/batch/integration/config/xml/RemoteChunkingSlaveParserMissingOutputChannelAttrTests.xml"); try { applicationContext.refresh(); fail(); } catch (BeanDefinitionStoreException e) { assertTrue("Nested exception must be of type IllegalArgumentException", e.getCause() instanceof IllegalArgumentException); IllegalArgumentException iae = (IllegalArgumentException) e.getCause(); assertTrue("Expected: " + "The output-channel attribute must be specified" + " but got: " + iae.getMessage(), "The output-channel attribute must be specified".equals(iae.getMessage())); } }
@Test public void testRemoteChunkingSlaveIdAttrAssert() throws Exception { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); applicationContext.setValidating(false); applicationContext.setConfigLocation("/org/springframework/batch/integration/config/xml/RemoteChunkingSlaveParserMissingIdAttrTests.xml"); try { applicationContext.refresh(); fail(); } catch (BeanDefinitionStoreException e) { assertTrue("Nested exception must be of type IllegalArgumentException", e.getCause() instanceof IllegalArgumentException); IllegalArgumentException iae = (IllegalArgumentException) e.getCause(); assertTrue("Expected: " + "The id attribute must be specified" + " but got: " + iae.getMessage(), "The id attribute must be specified".equals(iae.getMessage())); } }
@Test public void testRemoteChunkingSlaveInputChannelAttrAssert() throws Exception { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); applicationContext.setValidating(false); applicationContext.setConfigLocation("/org/springframework/batch/integration/config/xml/RemoteChunkingSlaveParserMissingInputChannelAttrTests.xml"); try { applicationContext.refresh(); fail(); } catch (BeanDefinitionStoreException e) { assertTrue("Nested exception must be of type IllegalArgumentException", e.getCause() instanceof IllegalArgumentException); IllegalArgumentException iae = (IllegalArgumentException) e.getCause(); assertTrue("Expected: " + "The input-channel attribute must be specified" + " but got: " + iae.getMessage(), "The input-channel attribute must be specified".equals(iae.getMessage())); } }
@Test public void testRemoteChunkingMasterReplyChannelAttrAssert() throws Exception { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); applicationContext.setValidating(false); applicationContext.setConfigLocation("/org/springframework/batch/integration/config/xml/RemoteChunkingMasterParserMissingReplyChannelAttrTests.xml"); try { applicationContext.refresh(); fail(); } catch (BeanDefinitionStoreException e) { assertTrue("Nested exception must be of type IllegalArgumentException", e.getCause() instanceof IllegalArgumentException); IllegalArgumentException iae = (IllegalArgumentException) e.getCause(); assertTrue("Expected: " + "The reply-channel attribute must be specified" + " but got: " + iae.getMessage(), "The reply-channel attribute must be specified".equals(iae.getMessage())); } }
public void handlerMappingXmlConfig() throws Exception { ClassPathXmlApplicationContext wac = new ClassPathXmlApplicationContext("map.xml", getClass()); wac.refresh();