Results 1 to 2 of 2

Thread: How do I call a context when using annotations?

  1. #1
    Join Date
    Dec 2008
    Posts
    20

    Default How do I call a context when using annotations?

    Hi, good evening.

    As everyone may have seen, when you open the helloworld proyect from the spring-integration-by-example;
    in the example they donīt use annotations so when the call the spring context they put:

    Code:
    		AbstractApplicationContext context = new ClassPathXmlApplicationContext(
    				"/META-INF/spring/integration/helloWorldDemo.xml", HelloWorldApp.class);
    		MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class);
    		PollableChannel outputChannel = context.getBean("outputChannel", PollableChannel.class);
    		inputChannel.send(new GenericMessage<String>("World"));
    		logger.info("==> HelloWorldDemo: " + outputChannel.receive(0).getPayload());
    But, what if Iīm using annotations to load the context, like this:

    Code:
    @ContextConfiguration(locations = { "classpath:envio-test-config.xml" })
    public class EnvioTests extends AbstractTestNGSpringContextTests {
        @Test
        public void myMethod() { }
    }
    and in "myMethod" I want to do something like:

    Code:
    		PollableChannel outputChannel = context.getBean("outputChannel", PollableChannel.class);
    		inputChannel.send(new GenericMessage<String>("World"));
    		logger.info("==> HelloWorldDemo: " + outputChannel.receive(0).getPayload());
    what would be itīs equivalent when using annotations??

    Thanks in advance.

  2. #2
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,148

    Default

    You can either

    Code:
    @ContextConfiguration(locations = { "classpath:envio-test-config.xml" })
    @RunWith(SpringJUnit4ClassRunner.class)
    public class EnvioTests extends AbstractTestNGSpringContextTests {
    
        @Autowired
        private MessageChannel inputChannel;
    
        @Autowired
        private PollableChannel outputChannel;
    
        @Test
        public void myMethod() { 
        ...
        }
    }
    or
    Code:
    @Autowired
    private ApplicationContext context
    and use context.getBean() as before.

    There are lots of examples (of either option) in the framework test cases.
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •