Example usage for org.springframework.context ApplicationContext getBean

List of usage examples for org.springframework.context ApplicationContext getBean

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext getBean.

Prototype

<T> T getBean(String name, Class<T> requiredType) throws BeansException;

Source Link

Document

Return an instance, which may be shared or independent, of the specified bean.

Usage

From source file:com.apress.prospringintegration.jdbc.JdbcGateway.java

public static void main(String[] args) throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("/spring/jdbc/jdbc-gateway-context.xml");

    MessageChannel input = context.getBean("input", MessageChannel.class);
    PollableChannel output = context.getBean("output", PollableChannel.class);

    Map<String, Object> rowMessage = new HashMap<String, Object>();

    rowMessage.put("id", 3);
    rowMessage.put("firstname", "Mr");
    rowMessage.put("lastname", "Bill");
    rowMessage.put("status", 0);

    Message<Map<String, Object>> message = MessageBuilder.withPayload(rowMessage).build();
    input.send(message);/*from   ww w. ja  va2s .c  o  m*/

    Message<?> reply = output.receive();

    System.out.println("Reply message: " + reply);

    Map<String, Object> rowMap = (Map<String, Object>) reply.getPayload();

    for (String column : rowMap.keySet()) {
        System.out.println("column: " + column + " value: " + rowMap.get(column));
    }
}

From source file:com.cimpress.puzzle.Main.java

public static void main(final String... args) {

    logger.debug("tt");

    ApplicationContext cntx = new ClassPathXmlApplicationContext("classpath:spring-integration-context.xml");

    AsyncLogger asyncLogger = cntx.getBean("asyncLogger", AsyncLogger.class);

    asyncLogger.log("Container loaded", String.class, Main.class, logger);
    PuzzleAPI puzzleAPI = cntx.getBean("puzzleAPI", PuzzleAPI.class);

    DateTime dt = new DateTime();
    Puzzle puzzle = puzzleAPI.generatePuzzle();

    // new SquareFinder().solvePuzzle(Environment.getPuzzleObject());
    List<Square> squareList = new SquareFinder().solvePuzzle(puzzle);
    System.out.println("Total squares :" + squareList.size());
    puzzleAPI.postSolution(squareList, puzzle.getId());
    DateTime d2 = new DateTime();
    Duration elapsedTime = new Duration(dt, d2);
    System.out.println("\nTotal Time Taken :" + elapsedTime.getStandardSeconds());
}

From source file:myjpa.guest.model.Test.java

public static void main(String[] args) {
    ApplicationContext ctx = new FileSystemXmlApplicationContext("web/WEB-INF/applicationContext.xml",
            "web/WEB-INF/dispatcher-servlet.xml");

    Guests guests = ctx.getBean("guests", Guests.class);

    //        int guestId = guests.insert(new Guest("Hello World", "Wee"));
    //        System.out.println("Guest Id:" + guestId);

    for (Guest g : guests.findAll(2, 3)) {
        System.out.println(g);/*from w w w  .ja  v a  2s  .c  o  m*/
    }
}

From source file:org.thys.michels.email2sfdc.email.GmailInboundPop3AdapterTestApp.java

public static void main(String[] args) throws Exception {
    ApplicationContext ac = new ClassPathXmlApplicationContext(
            "/META-INF/spring/integration/gmail-pop3-config.xml");
    DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class);
    inputChannel.subscribe(new MessageHandler() {
        public void handleMessage(Message<?> message) throws MessagingException {
            logger.info("Message: " + message.getPayload() + " " + message.getHeaders());
        }// w ww  . j av a  2 s. c o  m
    });
}

From source file:com.gopivotal.training.LoadRegions.java

/**
 * @param args/*  w ww.j ava  2  s  .c  o  m*/
 */
public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/gemfire/cache-config.xml");
    ClientCache cache = context.getBean("clientCache", ClientCache.class);
    if (cache != null) {
        new LoadRegions(cache).populateGemFire();
    } else {
        System.out.println("Failed to initialize Client Cache");
    }
}

From source file:com.springsource.samples.resttemplate.Driver.java

public static void main(String[] args) {
    if (args.length < 2) {
        System.out.println("Usage: java " + Driver.class.getName() + " [Flickr API key] [search term]");
        System.exit(-1);/*from   ww w  .  j  a v a2  s .c  om*/
    }
    String apiKey = args[0];
    String searchTerm = args[1];

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml",
            Driver.class);
    FlickrClient client = (FlickrClient) applicationContext.getBean("flickrClient", FlickrClient.class);
    client.doIt(apiKey, searchTerm);
}

From source file:org.springone2gx_2011.integration.aggregator.AggregatorDemo.java

/**
 * @param args//from   w w w . j a v  a 2 s. c o  m
 */
public static void main(String[] args) throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("aggregator-config.xml",
            AggregatorDemo.class);
    MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class);
    int iterations = 13;
    for (int i = 0; i < iterations; i++) {
        Message<?> message = MessageBuilder.withPayload(i).setCorrelationId(1).setSequenceNumber(i)
                .setSequenceSize(iterations).build();
        System.out.println("Sending: " + message);
        inputChannel.send(message);

        Thread.sleep(1000);
    }
}

From source file:com.plugtree.integration.external.jms.JMSProducer.java

public static void main(final String[] args) throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("camel-client.xml");
    CamelContext camel = context.getBean("camel-client", CamelContext.class);

    // get the endpoint from the camel context
    Endpoint endpoint = camel.getEndpoint("jms:queue:numbers");

    // create the exchange used for the communication
    // we use the in out pattern for a synchronized exchange where we expect a response
    Exchange exchange = endpoint.createExchange(ExchangePattern.InOut);
    // set the input on the in body
    // must you correct type to match the expected type of an Integer object
    Map<String, String> props = new HashMap<String, String>();
    props.put("key1", "value1");
    props.put("key2", "value2");
    props.put("key3", "value3");

    exchange.getIn().setBody(props);/*  w  ww  .  j  a  v a 2 s  .  c om*/

    // to send the exchange we need an producer to do it for us
    Producer producer = endpoint.createProducer();
    // start the producer so it can operate
    producer.start();

    // let the producer process the exchange where it does all the work in this oneline of code
    producer.process(exchange);

    // get the response from the out body and cast it to an integer
    String response = exchange.getOut().getBody(String.class);
    System.out.println("... the result is: " + response);
    System.exit(0);

}

From source file:to.sparks.mtgox.example.WebsocketExamples.java

public static void main(String[] args) throws Exception {

    ApplicationContext context = new ClassPathXmlApplicationContext(
            "to/sparks/mtgox/example/WebsocketExamples.xml");
    WebsocketExamples me = context.getBean("websocketExamples", WebsocketExamples.class);
}

From source file:siia.monitoring.controlbus.GroovyControlBusDemo.java

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("groovy-context.xml",
            GroovyControlBusDemo.class);
    NumberHolder numberHolder = context.getBean("numberHolder", NumberHolder.class);
    MessageChannel controlChannel = context.getBean("controlChannel", MessageChannel.class);
    System.out.println("number before increment: " + numberHolder.getNumber());
    Message<String> message = MessageBuilder.withPayload("numberHolder.increment()").build();
    controlChannel.send(message);// w ww . j a va  2 s . co  m
    System.out.println("number after increment:  " + numberHolder.getNumber());
    FilePoller filePoller = context.getBean("filePoller", FilePoller.class);
    System.out.println("file poller isRunning before start: " + filePoller.isRunning());
    controlChannel.send(MessageBuilder.withPayload("filePoller.start()").build());
    System.out.println("file poller isRunning after start:  " + filePoller.isRunning());
    controlChannel.send(MessageBuilder.withPayload("filePoller.stop()").build());
    System.out.println("file poller isRunning after stop:   " + filePoller.isRunning());
    ThreadPoolTaskExecutor executor = context.getBean("myExecutor", ThreadPoolTaskExecutor.class);
    System.out.println("max pool size before update: " + executor.getMaxPoolSize());
    controlChannel.send(MessageBuilder.withPayload("myExecutor.setMaxPoolSize(25)").build());
    System.out.println("max pool size after update:  " + executor.getMaxPoolSize());
}