org.springframework.context.support.GenericXmlApplicationContext Java Examples

The following examples show how to use org.springframework.context.support.GenericXmlApplicationContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AlarmJobTest.java    From pinpoint with Apache License 2.0 8 votes vote down vote up
public static void main(String[] args) throws Exception{
     GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext("/applicationContext-test.xml");
     JobLauncherTestUtils testLauncher = applicationContext.getBean(JobLauncherTestUtils.class);
     
     JobExecution jobExecution = testLauncher.launchJob(getParameters());
     BatchStatus status = jobExecution.getStatus();
     assertEquals(BatchStatus.COMPLETED, status);
     
     applicationContext.close();
}
 
Example #2
Source File: AsyncAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 7 votes vote down vote up
@Test
public void configuredThroughNamespace() {
	GenericXmlApplicationContext context = new GenericXmlApplicationContext();
	context.load(new ClassPathResource("taskNamespaceTests.xml", getClass()));
	context.refresh();
	ITestBean testBean = context.getBean("target", ITestBean.class);
	testBean.test();
	testBean.await(3000);
	Thread asyncThread = testBean.getThread();
	assertTrue(asyncThread.getName().startsWith("testExecutor"));

	TestableAsyncUncaughtExceptionHandler exceptionHandler =
			context.getBean("exceptionHandler", TestableAsyncUncaughtExceptionHandler.class);
	assertFalse("handler should not have been called yet", exceptionHandler.isCalled());

	testBean.failWithVoid();
	exceptionHandler.await(3000);
	Method m = ReflectionUtils.findMethod(TestBean.class, "failWithVoid");
	exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class);
	context.close();
}
 
Example #3
Source File: DestroyMethodInferenceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void xml() {
	ConfigurableApplicationContext ctx = new GenericXmlApplicationContext(
			getClass(), "DestroyMethodInferenceTests-context.xml");
	WithLocalCloseMethod x1 = ctx.getBean("x1", WithLocalCloseMethod.class);
	WithLocalCloseMethod x2 = ctx.getBean("x2", WithLocalCloseMethod.class);
	WithLocalCloseMethod x3 = ctx.getBean("x3", WithLocalCloseMethod.class);
	WithNoCloseMethod x4 = ctx.getBean("x4", WithNoCloseMethod.class);
	WithInheritedCloseMethod x8 = ctx.getBean("x8", WithInheritedCloseMethod.class);

	assertThat(x1.closed, is(false));
	assertThat(x2.closed, is(false));
	assertThat(x3.closed, is(false));
	assertThat(x4.closed, is(false));
	ctx.close();
	assertThat(x1.closed, is(false));
	assertThat(x2.closed, is(true));
	assertThat(x3.closed, is(true));
	assertThat(x4.closed, is(false));
	assertThat(x8.closed, is(false));
}
 
Example #4
Source File: AsyncAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void configuredThroughNamespace() {
	GenericXmlApplicationContext context = new GenericXmlApplicationContext();
	context.load(new ClassPathResource("taskNamespaceTests.xml", getClass()));
	context.refresh();
	ITestBean testBean = context.getBean("target", ITestBean.class);
	testBean.test();
	testBean.await(3000);
	Thread asyncThread = testBean.getThread();
	assertTrue(asyncThread.getName().startsWith("testExecutor"));

	TestableAsyncUncaughtExceptionHandler exceptionHandler =
			context.getBean("exceptionHandler", TestableAsyncUncaughtExceptionHandler.class);
	assertFalse("handler should not have been called yet", exceptionHandler.isCalled());

	testBean.failWithVoid();
	exceptionHandler.await(3000);
	Method m = ReflectionUtils.findMethod(TestBean.class, "failWithVoid");
	exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class);
	context.close();
}
 
Example #5
Source File: GroovyAspectIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testJavaBean() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-java-context.xml");
	TestService bean = context.getBean("javaBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (RuntimeException ex) {
		assertEquals("TestServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountThrows());

}
 
Example #6
Source File: GroovyAspectIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testGroovyBeanInterface() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-interface-context.xml");
	TestService bean = context.getBean("groovyBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (RuntimeException ex) {
		assertEquals("GroovyServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountThrows());
}
 
Example #7
Source File: GroovyAspectIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testGroovyBeanDynamic() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-dynamic-context.xml");
	TestService bean = context.getBean("groovyBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (RuntimeException ex) {
		assertEquals("GroovyServiceImpl", ex.getMessage());
	}
	// No proxy here because the pointcut only applies to the concrete class, not the interface
	assertEquals(0, logAdvice.getCountThrows());
	assertEquals(0, logAdvice.getCountBefore());
}
 
Example #8
Source File: GroovyAspectIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testGroovyBeanProxyTargetClass() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-proxy-target-class-context.xml");
	TestService bean = context.getBean("groovyBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (TestException ex) {
		assertEquals("GroovyServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountBefore());
	assertEquals(1, logAdvice.getCountThrows());
}
 
Example #9
Source File: GroovyAspectIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testJavaBean() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-java-context.xml");
	TestService bean = context.getBean("javaBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (RuntimeException ex) {
		assertEquals("TestServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountThrows());

}
 
Example #10
Source File: GroovyAspectIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testGroovyBeanDynamic() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-dynamic-context.xml");
	TestService bean = context.getBean("groovyBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (RuntimeException ex) {
		assertEquals("GroovyServiceImpl", ex.getMessage());
	}
	// No proxy here because the pointcut only applies to the concrete class, not the interface
	assertEquals(0, logAdvice.getCountThrows());
	assertEquals(0, logAdvice.getCountBefore());
}
 
Example #11
Source File: GroovyAspectIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testGroovyBeanProxyTargetClass() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-proxy-target-class-context.xml");
	TestService bean = context.getBean("groovyBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (TestException ex) {
		assertEquals("GroovyServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountBefore());
	assertEquals(1, logAdvice.getCountThrows());
}
 
Example #12
Source File: DestroyMethodInferenceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void xml() {
	ConfigurableApplicationContext ctx = new GenericXmlApplicationContext(
			getClass(), "DestroyMethodInferenceTests-context.xml");
	WithLocalCloseMethod x1 = ctx.getBean("x1", WithLocalCloseMethod.class);
	WithLocalCloseMethod x2 = ctx.getBean("x2", WithLocalCloseMethod.class);
	WithLocalCloseMethod x3 = ctx.getBean("x3", WithLocalCloseMethod.class);
	WithNoCloseMethod x4 = ctx.getBean("x4", WithNoCloseMethod.class);
	WithInheritedCloseMethod x8 = ctx.getBean("x8", WithInheritedCloseMethod.class);

	assertThat(x1.closed, is(false));
	assertThat(x2.closed, is(false));
	assertThat(x3.closed, is(false));
	assertThat(x4.closed, is(false));
	ctx.close();
	assertThat(x1.closed, is(false));
	assertThat(x2.closed, is(true));
	assertThat(x3.closed, is(true));
	assertThat(x4.closed, is(false));
	assertThat(x8.closed, is(false));
}
 
Example #13
Source File: GroovyAspectIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testGroovyBeanProxyTargetClass() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-proxy-target-class-context.xml");
	TestService bean = context.getBean("groovyBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (TestException ex) {
		assertEquals("GroovyServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountBefore());
	assertEquals(1, logAdvice.getCountThrows());
}
 
Example #14
Source File: SpringConfigurationHelper.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public static ProcessEngine buildProcessEngine(URL resource) {
  log.fine("==== BUILDING SPRING APPLICATION CONTEXT AND PROCESS ENGINE =========================================");
  
  ApplicationContext applicationContext = new GenericXmlApplicationContext(new UrlResource(resource));
  Map<String, ProcessEngine> beansOfType = applicationContext.getBeansOfType(ProcessEngine.class);
  if ( (beansOfType==null)
       || (beansOfType.isEmpty())
     ) {
    throw new ProcessEngineException("no "+ProcessEngine.class.getName()+" defined in the application context "+resource.toString());
  }
  
  ProcessEngine processEngine = beansOfType.values().iterator().next();

  log.fine("==== SPRING PROCESS ENGINE CREATED ==================================================================");
  return processEngine;
}
 
Example #15
Source File: GroovyAspectIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testGroovyBeanInterface() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-interface-context.xml");
	TestService bean = context.getBean("groovyBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (RuntimeException ex) {
		assertEquals("GroovyServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountThrows());
}
 
Example #16
Source File: GroovyAspectIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testJavaBean() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-java-context.xml");
	TestService bean = context.getBean("javaBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (RuntimeException ex) {
		assertEquals("TestServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountThrows());

}
 
Example #17
Source File: LazyScheduledTasksBeanDefinitionParserTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test(timeout = 5000)
@SuppressWarnings("resource")
public void checkTarget() {
	Task task =
		new GenericXmlApplicationContext(
				LazyScheduledTasksBeanDefinitionParserTests.class,
				"lazyScheduledTasksContext.xml")
			.getBean(Task.class);

	while (!task.executed) {
		try {
			Thread.sleep(10);
		}
		catch (Exception ex) { /* Do Nothing */ }
	}
}
 
Example #18
Source File: App.java    From Easy-Cassandra-samples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException, ParseException {
	@SuppressWarnings("resource")
	ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");
	Musics musicas = ctx.getBean(Musics.class);
	MusicService service = ctx.getBean(MusicService.class);
	for (Music musica: musicas.getMusics()) {
		service.save(musica);	
	}
	LuceneUtil.INSTANCE.backupToHD();
	System.out.println(service.findMusicByLyric("lepo lepo"));
	System.out.println(service.findMusicByLyric("aMoR"));
	
	System.out.println(service.findMusicByAuthor("Luan Santana"));
	System.out.println(service.findMusicByAuthor("Santana"));
	System.out.println(service.findMusicByAuthor("Psirico"));
}
 
Example #19
Source File: CacheAdviceParserTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void keyAndKeyGeneratorCannotBeSetTogether() {
	try {
		new GenericXmlApplicationContext("/org/springframework/cache/config/cache-advice-invalid.xml");
		fail("Should have failed to load context, one advise define both a key and a key generator");
	} catch (BeanDefinitionStoreException e) { // TODO better exception handling
	}
}
 
Example #20
Source File: ContextNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void propertyPlaceholderEnvironmentProperties() throws Exception {
	MockEnvironment env = new MockEnvironment().withProperty("foo", "spam");
	GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext();
	applicationContext.setEnvironment(env);
	applicationContext.load(new ClassPathResource("contextNamespaceHandlerTests-simple.xml", getClass()));
	applicationContext.refresh();
	Map<String, PlaceholderConfigurerSupport> beans = applicationContext
			.getBeansOfType(PlaceholderConfigurerSupport.class);
	assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
	assertEquals("spam", applicationContext.getBean("string"));
	assertEquals("none", applicationContext.getBean("fallback"));
}
 
Example #21
Source File: EnvironmentSystemIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void genericXmlApplicationContext() {
	GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
	assertHasStandardEnvironment(ctx);
	ctx.setEnvironment(prodEnv);
	ctx.load(XML_PATH);
	ctx.refresh();

	assertHasEnvironment(ctx, prodEnv);
	assertEnvironmentBeanRegistered(ctx);
	assertEnvironmentAwareInvoked(ctx, prodEnv);
	assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false));
	assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true));
}
 
Example #22
Source File: AnnotationNamespaceDrivenTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void bothSetOnlyResolverIsUsed() {
	ConfigurableApplicationContext context = new GenericXmlApplicationContext(
			"/org/springframework/cache/config/annotationDrivenCacheNamespace-manager-resolver.xml");

	CacheInterceptor ci = context.getBean(CacheInterceptor.class);
	assertSame(context.getBean("cacheResolver"), ci.getCacheResolver());
	context.close();
}
 
Example #23
Source File: SpringContextBuilder.java    From ob1k with Apache License 2.0 5 votes vote down vote up
public AbstractApplicationContext createSubContext(final AbstractApplicationContext parent, final String path, final boolean allowBeanOverride) {
  final GenericXmlApplicationContext subContext = new GenericXmlApplicationContext();
  subContext.setParent(parent);
  subContext.setAllowBeanDefinitionOverriding(allowBeanOverride);
  subContext.load(path);
  subContext.refresh();
  return subContext;
}
 
Example #24
Source File: LazyScheduledTasksBeanDefinitionParserTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 5000)
public void checkTarget() {
	Task task =
		new GenericXmlApplicationContext(
				LazyScheduledTasksBeanDefinitionParserTests.class,
				"lazyScheduledTasksContext.xml")
			.getBean(Task.class);

	while (!task.executed) {
		try {
			Thread.sleep(10);
		} catch (Exception e) { /* Do Nothing */ }
	}
}
 
Example #25
Source File: AnnotationNamespaceDrivenTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void cacheResolver() {
	ConfigurableApplicationContext context = new GenericXmlApplicationContext(
			"/org/springframework/cache/config/annotationDrivenCacheNamespace-resolver.xml");

	CacheInterceptor ci = context.getBean(CacheInterceptor.class);
	assertSame(context.getBean("cacheResolver"), ci.getCacheResolver());
	context.close();
}
 
Example #26
Source File: SpringConfigurationHelper.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
private static ProcessEngine buildProcessEngine(URL resource) {
  log.debug("==== BUILDING SPRING APPLICATION CONTEXT AND PROCESS ENGINE =========================================");

  ApplicationContext applicationContext = new GenericXmlApplicationContext(new UrlResource(resource));
  Map<String, ProcessEngine> beansOfType = applicationContext.getBeansOfType(ProcessEngine.class);
  if ((beansOfType == null) || (beansOfType.isEmpty())) {
    throw new ActivitiException("no " + ProcessEngine.class.getName() + " defined in the application context " + resource.toString());
  }

  ProcessEngine processEngine = beansOfType.values().iterator().next();

  log.debug("==== SPRING PROCESS ENGINE CREATED ==================================================================");
  return processEngine;
}
 
Example #27
Source File: SpringContextBuilder.java    From ob1k with Apache License 2.0 5 votes vote down vote up
private AbstractApplicationContext createMainContext(final boolean allowBeanOverride) {
  final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
  if (activeProfiles != null && !activeProfiles.isEmpty()) {
    context.getEnvironment().setActiveProfiles(activeProfiles);
  }
  context.setAllowBeanDefinitionOverriding(allowBeanOverride);
  context.load(mainParams.path);
  context.refresh();
  return context;
}
 
Example #28
Source File: AnnotationNamespaceDrivenTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void bothSetOnlyResolverIsUsed() {
	ConfigurableApplicationContext context = new GenericXmlApplicationContext(
			"/org/springframework/cache/config/annotationDrivenCacheNamespace-manager-resolver.xml");

	CacheInterceptor ci = context.getBean(CacheInterceptor.class);
	assertSame(context.getBean("cacheResolver"), ci.getCacheResolver());
	context.close();
}
 
Example #29
Source File: AnnotationNamespaceDrivenTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void cacheResolver() {
	ConfigurableApplicationContext context = new GenericXmlApplicationContext(
			"/org/springframework/cache/config/annotationDrivenCacheNamespace-resolver.xml");

	CacheInterceptor ci = context.getBean(CacheInterceptor.class);
	assertSame(context.getBean("cacheResolver"), ci.getCacheResolver());
	context.close();
}
 
Example #30
Source File: SpringContentConfigurationHelper.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
public static ContentEngine buildContentEngine(URL resource) {
    LOGGER.debug("==== BUILDING SPRING APPLICATION CONTEXT AND CONTENT ENGINE =========================================");

    ApplicationContext applicationContext = new GenericXmlApplicationContext(new UrlResource(resource));
    Map<String, ContentEngine> beansOfType = applicationContext.getBeansOfType(ContentEngine.class);
    if ((beansOfType == null) || beansOfType.isEmpty()) {
        throw new FlowableException("no " + ContentEngine.class.getName() + " defined in the application context " + resource.toString());
    }

    ContentEngine contentEngine = beansOfType.values().iterator().next();

    LOGGER.debug("==== SPRING CONTENT ENGINE CREATED ==================================================================");
    return contentEngine;
}