Java Code Examples for org.springframework.context.support.GenericXmlApplicationContext#getBean()

The following examples show how to use org.springframework.context.support.GenericXmlApplicationContext#getBean() . 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: 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 4
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 5
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 6
Source File: GroovyAspectIntegrationTests.java    From spring4-understanding with Apache License 2.0 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 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 8
Source File: AsyncAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 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 9
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 10
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 11
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 12
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 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: GroovyAspectIntegrationTests.java    From spring-analysis-note 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 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: EnableLoadTimeWeavingTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void control() {
	GenericXmlApplicationContext ctx =
		new GenericXmlApplicationContext(getClass(), "EnableLoadTimeWeavingTests-context.xml");
	ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
}
 
Example 18
Source File: EnableLoadTimeWeavingTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void control() {
	GenericXmlApplicationContext ctx =
		new GenericXmlApplicationContext(getClass(), "EnableLoadTimeWeavingTests-context.xml");
	ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
}
 
Example 19
Source File: EnableLoadTimeWeavingTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void control() {
	GenericXmlApplicationContext ctx =
		new GenericXmlApplicationContext(getClass(), "EnableLoadTimeWeavingTests-context.xml");
	ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
}