Announcement Announcement Module
Collapse
No announcement yet.
How do I call a context when using annotations? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment

    Working...
    X