Example usage for org.springframework.context.support ClassPathXmlApplicationContext start

List of usage examples for org.springframework.context.support ClassPathXmlApplicationContext start

Introduction

In this page you can find the example usage for org.springframework.context.support ClassPathXmlApplicationContext start.

Prototype

@Override
    public void start() 

Source Link

Usage

From source file:com.hm.SSI.dubbo.DubboConsumer.java

@Test
public void testMysqlSelectAllUser() {

    String config = "classpath:/dubbo-consumer.xml";
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
    context.start();
    final MiUserManager muserManagerImpl = (MiUserManager) context.getBean("muserManagerImpl");

    try {/*from  w  ww  .ja  v a2s . c  o m*/
        List<User> userList = muserManagerImpl.selectAllUser();
        for (User user : userList) {
            System.out.println("Id:" + user.getId() + "   name:" + user.getName());
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.lottery.common.message.ProducerTest.java

@Test
public void testSend() {
    final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(CONFIG);
    ctx.start();

    final MessageChannel channel = ctx.getBean("common-message.producer", MessageChannel.class);

    channel.send(MessageBuilder.withPayload("from messageChannel" + System.currentTimeMillis())
            .setHeader("messageKey", "key").setHeader("topic", "test").build());

    MessageProducer messageProducer = ctx.getBean(MessageProducer.class);
    messageProducer.send("test", "from messageProducer" + System.currentTimeMillis());
    try {/*from   w w  w.j  a v  a2 s  .  c  o  m*/
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    ctx.close();
}

From source file:net.zcarioca.jmx.servlet.JRestMXServlet.java

/**
 * {@inheritDoc}//w w  w  . j a  v  a 2 s.  co m
 */
@Override
public void init() throws ServletException {
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(appContext);

    applicationContext.registerShutdownHook();
    applicationContext.start();

    this.applicationContext = applicationContext;
    this.requestHandler = this.applicationContext.getBean(RestRequestHandler.class);
}

From source file:boot.dubbo.normal.client.DemoConsumer.java

@Test
public void test() {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
            new String[] { "classpath:/META-INF/spring/applicationContext-dubbo.xml" });
    ctx.start();

    System.out.println(ctx.getStartupDate());
    UserService userService = ctx.getBean("userService", UserService.class);
    while (true) {
        List<User> users = userService.findAll();
        User u = new User();
        Random r = new Random();
        u.setName("gsadg" + r.nextInt());
        String v = userService.create(u);
        System.out.println("" + v + "size:" + users.size());
        try {/*from www  . j av  a 2s  .  co m*/
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // try {
    // System.in.read();
    // } catch (IOException e) {
    // e.printStackTrace();
    // }
}

From source file:com.alibaba.dubbo.examples.annotation.AnnotationTest.java

@Test
public void testAnnotation() {
    ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(
            AnnotationTest.class.getPackage().getName().replace('.', '/') + "/annotation-provider.xml");
    providerContext.start();
    try {/*from  w w w .j  av a  2 s . c  om*/
        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();
    }
}

From source file:com.baidu.cc.spring.ConfigChangeNotiferAnnotationParserTest.java

/**
 * test annotation parser./*  w w  w  .jav  a2 s.  c  om*/
 */
@Ignore
@Test
public void testAnnotationParserInRealRuntime() {

    ClassPathXmlApplicationContext appContext;
    appContext = new ClassPathXmlApplicationContext(
            "classpath:/com/baidu/cc/annotation/applicationContext-no-mock.xml");

    appContext.start();

    try {
        Thread.sleep(3000L);
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
}

From source file:com.baidu.cc.spring.ConfigChangeNotiferAnnotationParserTest.java

/**
 * test annotation parser. by mock class.
 *///ww  w  . j  ava  2 s . c  o m
@Test
public void testAnnotationParserByMock() {

    ClassPathXmlApplicationContext appContext;
    appContext = new ClassPathXmlApplicationContext(
            "classpath:/com/baidu/cc/annotation/applicationContext.xml");

    appContext.start();

    //get service bean
    ConfigServerServiceMock configServerService;
    configServerService = (ConfigServerServiceMock) appContext.getBean("configServerServiceMocker");

    Assert.assertNotNull(configServerService);
    try {
        Thread.sleep(500L);
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }

    Map<String, String> ret = new HashMap<String, String>();
    ret.put(ConfigServerService.TAG_KEY, ConfigServerServiceMock.TEST_TAG_NEW_VALUE);

    ret.put("key1", "value1");
    ret.put("key2", "value22");
    ret.put("key3", "value3");
    ret.put("xml", "2");

    configServerService.setProps(ret);
    configServerService.setTagValue(ConfigServerServiceMock.TEST_TAG_NEW_VALUE);

    Assert.assertEquals(ConfigServerServiceMock.TEST_TAG_NEW_VALUE, configServerService.getTagValue());

    try {
        Thread.sleep(3000L);
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }

    // appContext.stop();

}

From source file:com.hm.SSI.dubbo.AnnotationTest.java

@Test
public void testAnnotation() {
    ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(
            AnnotationTest.class.getPackage().getName().replace('.', '/') + "/annotation-provider.xml");
    System.out.println(/*from w  w w  .  j  ava2 s  .  c o m*/
            AnnotationTest.class.getPackage().getName().replace('.', '/') + "/annotation-provider.xml");
    providerContext.start();
    try {
        ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext(
                AnnotationTest.class.getPackage().getName().replace('.', '/') + "/annotation-consumer.xml");
        System.out.println(
                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();
    }
}

From source file:com.alibaba.dubbo.examples.validation.ValidationTest.java

@Test
public void testValidation() {
    ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(
            ValidationTest.class.getPackage().getName().replace('.', '/') + "/validation-provider.xml");
    providerContext.start();
    try {//from  w w  w  .j a  v  a  2s  .c  o m
        ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext(
                ValidationTest.class.getPackage().getName().replace('.', '/') + "/validation-consumer.xml");
        consumerContext.start();
        try {
            ValidationService validationService = (ValidationService) consumerContext
                    .getBean("validationService");

            // Save OK
            ValidationParameter parameter = new ValidationParameter();
            parameter.setName("liangfei");
            parameter.setEmail("liangfei@liang.fei");
            parameter.setAge(50);
            parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
            parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
            validationService.save(parameter);

            try {
                parameter = new ValidationParameter();
                parameter.setName("l");
                parameter.setEmail("liangfei@liang.fei");
                parameter.setAge(50);
                parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
                parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
                validationService.save(parameter);
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
            }

            // Save Error
            try {
                parameter = new ValidationParameter();
                validationService.save(parameter);
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
            }

            // Delete OK
            validationService.delete(2, "abc");

            // Delete Error
            try {
                validationService.delete(2, "a");
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }

            // Delete Error
            try {
                validationService.delete(0, "abc");
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            try {
                validationService.delete(2, null);
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            try {
                validationService.delete(0, null);
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(2, violations.size());
            }
        } finally {
            consumerContext.stop();
            consumerContext.close();
        }
    } finally {
        providerContext.stop();
        providerContext.close();
    }
}

From source file:validation.ValidationTest.java

@Test
public void testValidation() {
    ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(
            ValidationTest.class.getPackage().getName().replace('.', '/') + "/validation-provider.xml");
    providerContext.start();
    try {//from   ww  w.j  av a  2s.co m
        ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext(
                ValidationTest.class.getPackage().getName().replace('.', '/') + "/validation-consumer.xml");
        consumerContext.start();
        try {
            ValidationService validationService = (ValidationService) consumerContext
                    .getBean("validationService");

            // Save OK
            ValidationParameter parameter = new ValidationParameter();
            parameter.setName("liangfei");
            parameter.setEmail("liangfei@liang.fei");
            parameter.setAge(50);
            parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
            parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
            validationService.save(parameter);

            try {
                parameter = new ValidationParameter();
                parameter.setName("l");
                parameter.setEmail(null);
                parameter.setAge(50);
                parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
                parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
                validationService.save(parameter);
                // Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                System.out.println(violations);
                // Assert.assertNotNull(violations);
            }

            // Save Error
            try {
                parameter = new ValidationParameter();
                validationService.save(parameter);
                // Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                //  Assert.assertNotNull(violations);
                System.out.println(violations);
            }

            // Delete OK
            validationService.delete(2, "abc");

            // Delete Error
            try {
                validationService.delete(2, "a");
                //    Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                System.out.println(violations);

            }

            // Delete Error
            try {
                validationService.delete(0, "abc");
                //    Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                System.out.println(violations);
            }
            try {
                validationService.delete(2, null);
                //    Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                System.out.println(violations);
            }
            try {
                validationService.delete(0, null);
                //    Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                System.out.println(violations);

            }
            try {
                validationService.delete(0, "0000");
                //    Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                System.out.println(violations);

            }
        } finally {
            consumerContext.stop();
            consumerContext.close();
        }
    } finally {
        providerContext.stop();
        providerContext.close();
    }
}