@After public void close() { if (context != null) { context.close(); } }
private static void executeSample1() { final Scanner scanner = new Scanner(System.in); final GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.load("classpath:META-INF/spring/integration/spring-integration-sample1-context.xml"); context.registerShutdownHook(); context.refresh(); final StringConversionService service = context.getBean(StringConversionService.class); final String message = "\n=========================================================" + "\n " + "\n Please press 'q + Enter' to quit the application. " + "\n " + "\n=========================================================" + "\n\n Please enter a string and press <enter>: "; System.out.print(message); while (!scanner.hasNext("q")) { String input = scanner.nextLine(); System.out.println("Converting String to Uppcase using Stored Procedure..."); String inputUpperCase = service.convertToUpperCase(input); System.out.println("Retrieving Numeric value via Sql Function..."); Integer number = service.getNumber(); System.out.println(String.format("Converted '%s' - End Result: '%s_%s'.", input, inputUpperCase, number)); System.out.print("To try again, please enter a string and press <enter>:"); } scanner.close(); context.close(); System.out.println("Back to main menu."); }
@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(); }
@Test public void componentScanRespectsProfileAnnotation() { String xmlLocation = "org/springframework/context/annotation/componentScanRespectsProfileAnnotationTests.xml"; { // should exclude the profile-annotated bean if active profiles remains unset GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.load(xmlLocation); context.refresh(); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(false)); context.close(); } { // should include the profile-annotated bean with active profiles set GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.getEnvironment().setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME); context.load(xmlLocation); context.refresh(); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true)); context.close(); } { // ensure the same works for AbstractRefreshableApplicationContext impls too ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] { xmlLocation }, false); context.getEnvironment().setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME); context.refresh(); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true)); context.close(); } }
@Test public void testGatewayDemo() throws InterruptedException { System.setProperty("spring.profiles.active", "testCase"); final GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(configFilesGatewayDemo); final MessageChannel stdinToJmsOutChannel = applicationContext.getBean("stdinToJmsOutChannel", MessageChannel.class); stdinToJmsOutChannel.send(MessageBuilder.withPayload("jms test").build()); final QueueChannel queueChannel = applicationContext.getBean("queueChannel", QueueChannel.class); @SuppressWarnings("unchecked") Message<String> reply = (Message<String>) queueChannel.receive(20000); Assert.assertNotNull(reply); String out = reply.getPayload(); Assert.assertEquals("JMS response: JMS TEST", out); applicationContext.close(); }
@Test public void testChannelAdapterDemo() throws InterruptedException { System.setProperty("spring.profiles.active", "testCase"); final GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(configFilesChannelAdapterDemo); final MessageChannel stdinToJmsOutChannel = applicationContext.getBean("stdinToJmsOutChannel", MessageChannel.class); stdinToJmsOutChannel.send(MessageBuilder.withPayload("jms test").build()); final QueueChannel queueChannel = applicationContext.getBean("queueChannel", QueueChannel.class); @SuppressWarnings("unchecked") Message<String> reply = (Message<String>) queueChannel.receive(20000); Assert.assertNotNull(reply); String out = reply.getPayload(); Assert.assertEquals("jms test", out); applicationContext.close(); }
@After public void close() { if (context != null) { context.close(); } }
@Test public void testCustomIdGenerationWithParentChildIndependentCreation() throws Exception { ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context-withGenerator.xml", this.getClass()); GenericXmlApplicationContext child = new GenericXmlApplicationContext(); child.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context.xml"); child.setParent(parent); child.refresh(); IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class); MessageChannel inputChannel = child.getBean("input", MessageChannel.class); inputChannel.send(new GenericMessage<Integer>(0)); verify(idGenerator, atLeastOnce()).generateId(); child.close(); parent.close(); this.assertDestroy(); }
@Test public void testCustomIdGenerationWithParentChildIndependentCreationChildrenRegistrarsOneAtTheTime() throws Exception { ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", this.getClass()); GenericXmlApplicationContext childA = new GenericXmlApplicationContext(); childA.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context-withGenerator.xml"); childA.setParent(parent); childA.refresh(); childA.close(); GenericXmlApplicationContext childB = new GenericXmlApplicationContext(); childB.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context-withGenerator.xml"); childB.setParent(parent); childB.refresh(); parent.close(); childB.close(); this.assertDestroy(); }
new GenericXmlApplicationContext(resource).close(); fail("Expected Failure to load ApplicationContext");
new GenericXmlApplicationContext(resource).close(); fail("Expected Failure to load ApplicationContext");
new GenericXmlApplicationContext(resource).close(); fail("Expected Failure to load ApplicationContext");
public void shutDown() { if (packageContexts != null) { synchronized (pkgContextLock) { for (MdwCloudAppContext appContext : packageContexts.values()) shutDown(appContext); } } if (springAppContext != null) { springAppContext.close(); } }
public static void main(String[] args) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:config-without-id.xml"); String[] beanNames = ctx.getBeanDefinitionNames(); for (String bn : beanNames) System.out.println(bn); ctx.close(); } }
private static void useXml() { System.out.println("===== XML Meta ====="); String configLocation = "classpath:config-for-scope.xml"; GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(configLocation); WorkScheduler scheduler = ctx.getBean("workScheduler", WorkScheduler.class); scheduler.makeAndRunWork(); ctx.close(); }
public static void main(String[] args) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:config-all.xml"); AuthenticationService authSvc = ctx.getBean("authenticationService", AuthenticationService.class); authSvc.authenticate("bkchoi", "1234"); Monitor monitor = ctx.getBean(Monitor.class); System.out.println(monitor); ctx.close(); }