Java Code Examples for org.springframework.context.support.ClassPathXmlApplicationContext#close()

The following examples show how to use org.springframework.context.support.ClassPathXmlApplicationContext#close() . 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: SpringBeansTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testClientUsingDifferentBus() throws Exception {
    ClassPathXmlApplicationContext ctx =
        new ClassPathXmlApplicationContext(new String[] {"/org/apache/cxf/jaxws/spring/clients.xml"});
    Greeter greeter = (Greeter) ctx.getBean("differentBusGreeter");
    assertNotNull(greeter);
    Client client = ClientProxy.getClient(greeter);
    assertEquals("snarf", client.getBus().getProperty("foo"));

    Greeter greeter1 = (Greeter) ctx.getBean("client1");
    assertNotNull(greeter1);
    Client client1 = ClientProxy.getClient(greeter1);
    assertEquals("barf", client1.getBus().getProperty("foo"));
    Greeter greeter2 = (Greeter) ctx.getBean("wsdlLocation");
    assertNotNull(greeter2);
    Client client2 = ClientProxy.getClient(greeter2);
    assertSame(client1.getBus(), client2.getBus());
    ctx.close();
}
 
Example 2
Source File: ConfigTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnnotation() {
    SimpleRegistryService registryService = new SimpleRegistryService();
    Exporter<RegistryService> exporter = SimpleRegistryExporter.export(4548, registryService);
    try {
        ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/annotation-provider.xml");
        providerContext.start();
        try {
            ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/annotation-consumer.xml");
            consumerContext.start();
            try {
                AnnotationAction annotationAction = (AnnotationAction) consumerContext.getBean("annotationAction");
                String hello = annotationAction.doSayName("hello");
                assertEquals("annotation:hello", hello);
            } finally {
                consumerContext.stop();
                consumerContext.close();
            }
        } finally {
            providerContext.stop();
            providerContext.close();
        }
    } finally {
        exporter.unexport();
    }
}
 
Example 3
Source File: QuartzSupportTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void schedulerAccessorBean() throws Exception {
	Assume.group(TestGroup.PERFORMANCE);
	ClassPathXmlApplicationContext ctx = context("schedulerAccessorBean.xml");
	Thread.sleep(3000);
	try {
		QuartzTestBean exportService = (QuartzTestBean) ctx.getBean("exportService");
		QuartzTestBean importService = (QuartzTestBean) ctx.getBean("importService");

		assertEquals("doImport called exportService", 0, exportService.getImportCount());
		assertEquals("doExport not called on exportService", 2, exportService.getExportCount());
		assertEquals("doImport not called on importService", 2, importService.getImportCount());
		assertEquals("doExport called on importService", 0, importService.getExportCount());
	}
	finally {
		ctx.close();
	}
}
 
Example 4
Source File: ConfigTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnnotation() {
    SimpleRegistryService registryService = new SimpleRegistryService();
    Exporter<RegistryService> exporter = SimpleRegistryExporter.export(4548, registryService);
    try {
        ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/annotation-provider.xml");
        providerContext.start();
        try {
            ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/annotation-consumer.xml");
            consumerContext.start();
            try {
                AnnotationAction annotationAction = (AnnotationAction) consumerContext.getBean("annotationAction");
                String hello = annotationAction.doSayName("hello");
                assertEquals("annotation:hello", hello);
            } finally {
                consumerContext.stop();
                consumerContext.close();
            }
        } finally {
            providerContext.stop();
            providerContext.close();
        }
    } finally {
        exporter.unexport();
    }
}
 
Example 5
Source File: ConfigTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelayFixedTime() throws Exception {
    SimpleRegistryService registryService = new SimpleRegistryService();
    Exporter<RegistryService> exporter = SimpleRegistryExporter.export(4548, registryService);
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-fixed-time.xml");
    ctx.start();
    try {
        List<URL> urls = registryService.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNull(urls);
        int i = 0;
        while ((i ++) < 60 && urls == null) {
            urls = registryService.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
            Thread.sleep(10);
        }
        assertNotNull(urls);
        assertEquals(1, urls.size());
        assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20883/com.alibaba.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString());
    } finally {
        ctx.stop();
        ctx.close();
        exporter.unexport();
    }
}
 
Example 6
Source File: AnnotationTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnnotation() {
    ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(AnnotationTest.class.getPackage().getName().replace('.', '/') + "/annotation-provider.xml");
    providerContext.start();
    try {
        ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext(AnnotationTest.class.getPackage().getName().replace('.', '/') + "/annotation-consumer.xml");
        consumerContext.start();
        try {
            AnnotationAction annotationAction = (AnnotationAction) consumerContext.getBean("annotationAction");
            String hello = annotationAction.doSayHello("world");
            assertEquals("annotation: hello, world", hello);
        } finally {
            consumerContext.stop();
            consumerContext.close();
        }
    } finally {
        providerContext.stop();
        providerContext.close();
    }
}
 
Example 7
Source File: JAXRSHttpsBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore("Works in the studio only if local jaxrs.xsd is updated to have jaxrs:client")
public void testGetBook123WebClientFromSpringWildcardOldJaxrsClient() throws Exception {
    ClassPathXmlApplicationContext ctx =
        new ClassPathXmlApplicationContext(new String[] {CLIENT_CONFIG_FILE_OLD});
    Object bean = ctx.getBean("bookService.proxyFactory");
    assertNotNull(bean);
    JAXRSClientFactoryBean cfb = (JAXRSClientFactoryBean) bean;

    WebClient wc = (WebClient)cfb.create();
    assertEquals("https://localhost:" + PORT, wc.getBaseURI().toString());

    wc.accept("application/xml");
    wc.path("bookstore/securebooks/123");
    TheBook b = wc.get(TheBook.class);

    assertEquals(b.getId(), 123);
    b = wc.get(TheBook.class);
    assertEquals(b.getId(), 123);
    ctx.close();
}
 
Example 8
Source File: RuntimeExecBeansTest.java    From alfresco-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testExecOfNeverEndingProcess()
{
    File dir = new File(DIR);
    dir.mkdir();
    assertTrue("Directory not created", dir.exists());
    
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(APP_CONTEXT_XML);
    try
    {
        RuntimeExec failureExec = (RuntimeExec) ctx.getBean("commandNeverEnding");
        assertNotNull(failureExec);
        // Execute it
        failureExec.execute();
        // The command is never-ending, so this should be out immediately
    }
    finally
    {
        ctx.close();
    }
}
 
Example 9
Source File: ConsumerServer.java    From azeroth with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {

        final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "test-kafka-consumer.xml");

        logger.info("Kafka Consumer started....");
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

            @Override
            public void run() {
                logger.info("Kafka Consumer Stoped....");
                context.close();
            }
        }));

        Scanner scan = new Scanner(System.in);
        String cmd = scan.next();
        if ("q".equalsIgnoreCase(cmd)) {
            context.close();
            scan.close();
        }

    }
 
Example 10
Source File: ConfigTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testInitReference() throws Exception {
    ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider.xml");
    providerContext.start();
    try {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/init-reference.xml");
        ctx.start();
        try {
            DemoService demoService = (DemoService) ctx.getBean("demoService");
            assertEquals("say:world", demoService.sayName("world"));
        } finally {
            ctx.stop();
            ctx.close();
        }
    } finally {
        providerContext.stop();
        providerContext.close();
    }
}
 
Example 11
Source File: SpringBeansTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testChildContext() throws Exception {
    //Test for CXF-2283 - if a Child context is closed,
    //we shouldn't be shutting down
    ClassPathXmlApplicationContext ctx =
        new ClassPathXmlApplicationContext(new String[] {"/org/apache/cxf/jaxws/spring/servers.xml"});

    final Bus b = (Bus)ctx.getBean("cxf");
    BusLifeCycleManager lifeCycleManager = b.getExtension(BusLifeCycleManager.class);
    BusLifeCycleListener listener = new BusLifeCycleListener() {
        public void initComplete() {
        }

        public void postShutdown() {
            b.setProperty("post.was.called", Boolean.TRUE);
        }

        public void preShutdown() {
            b.setProperty("pre.was.called", Boolean.TRUE);
        }
    };
    lifeCycleManager.registerLifeCycleListener(listener);
    ClassPathXmlApplicationContext ctx2 =
            new ClassPathXmlApplicationContext(new String[] {"/org/apache/cxf/jaxws/spring/child.xml"},
                                               ctx);

    ctx2.close();

    assertNull(b.getProperty("post.was.called"));
    assertNull(b.getProperty("pre.was.called"));
}
 
Example 12
Source File: ConfigTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Test
public void testSpringExtensionInject() {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/spring-extension-inject.xml");
    ctx.start();
    try {
        MockFilter filter = (MockFilter) ExtensionLoader.getExtensionLoader(Filter.class).getExtension("mymock");
        assertNotNull(filter.getMockDao());
        assertNotNull(filter.getProtocol());
        assertNotNull(filter.getLoadBalance());
    } finally {
        ctx.stop();
        ctx.close();
    }
}
 
Example 13
Source File: ComponentScanParserScopedProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testTargetClassScopedProxy() throws Exception {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
			"org/springframework/context/annotation/scopedProxyTargetClassTests.xml");
	context.getBeanFactory().registerScope("myScope", new SimpleMapScope());
	ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean");
	// should be a class-based proxy
	assertTrue(AopUtils.isCglibProxy(bean));
	// test serializability
	assertEquals("bar", bean.foo(1));
	ScopedProxyTestBean deserialized = (ScopedProxyTestBean) SerializationTestUtils.serializeAndDeserialize(bean);
	assertNotNull(deserialized);
	assertEquals("bar", deserialized.foo(1));
	context.close();
}
 
Example 14
Source File: Test.java    From Project with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(
            "applicationContext.xml");
    SpringLifeCycle springLifeCycle = (SpringLifeCycle) context.getBean("springLifeCycle");
    springLifeCycle.sayHello();

    // 销毁 Spring 容器
    ClassPathXmlApplicationContext classContext = (ClassPathXmlApplicationContext) context;
    classContext.close();
}
 
Example 15
Source File: JdbcNamespaceIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void createAndDestroy() throws Exception {
	ClassPathXmlApplicationContext context = context("jdbc-destroy-config.xml");
	try {
		DataSource dataSource = context.getBean(DataSource.class);
		JdbcTemplate template = new JdbcTemplate(dataSource);
		assertNumRowsInTestTable(template, 1);
		context.getBean(DataSourceInitializer.class).destroy();
		expected.expect(BadSqlGrammarException.class); // Table has been dropped
		assertNumRowsInTestTable(template, 1);
	}
	finally {
		context.close();
	}
}
 
Example 16
Source File: CustomerServiceSpringServer.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    ClassPathXmlApplicationContext ctx
        = new ClassPathXmlApplicationContext("server-applicationContext.xml");
    Thread.sleep(5 * 60 * 1000);
    ctx.close();
    System.out.println("Server exiting");
}
 
Example 17
Source File: ConfigTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiProtocolDefault() {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-protocol-default.xml");
    ctx.start();
    try {
        DemoService demoService = refer("rmi://127.0.0.1:10991");
        String hello = demoService.sayName("hello");
        assertEquals("say:hello", hello);
    } finally {
        ctx.stop();
        ctx.close();
    }
}
 
Example 18
Source File: JmsProtocolSpring.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {


XBeanBrokerFactory factory = new XBeanBrokerFactory();
BrokerService broker = factory.createBroker(new URI(JmsProtocolTest.class.getPackage().getName().replace('.', '/') +"/activemq.xml"));
broker.start();

      ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext( 
      		JmsProtocolSpring.class.getPackage().getName().replace('.', '/') + "/dubbo-demo-provider.xml");
      providerContext.start();
      try {
          ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext( 
          		JmsProtocolSpring.class.getPackage().getName().replace('.', '/') + "/dubbo-demo-consumer.xml");
          consumerContext.start();
          try {
          	DemoService demoService = (DemoService) consumerContext.getBean("demoService");
              String hello = demoService.test("world",0);
              assertEquals("world:1", hello);
          } finally {
              consumerContext.stop();
              consumerContext.close();
          }
      } finally {
          providerContext.stop();
          providerContext.close();
      }
      
      broker.stop();
  }
 
Example 19
Source File: SpringBusFactoryTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadBusWithApplicationContext() throws BusException {
    ClassPathXmlApplicationContext ctx =
        new ClassPathXmlApplicationContext(new String[] {"/org/apache/cxf/systest/bus/basic.xml"});
    Bus bus = ctx.getBean("cxf", Bus.class);
    ctx.refresh();
    bus = ctx.getBean("cxf", Bus.class);
    checkBindingExtensions(bus);
    checkHTTPTransportFactories(bus);
    checkOtherCoreExtensions(bus);
    ctx.close();
}
 
Example 20
Source File: MainTest.java    From Project with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:performance.xml");
	Performance performance = (Performance) context.getBean("performance");
	performance.perform();
	context.close();
}