Hi everyone
Probably a simple question, so here we go: when writing unit tests, I can use @ContextConfiguration to set my spring context and have the load time weaver autowire my @Configurable classes' fields.
However, running the main program like this:
does not seem to work. Instead, I'm continually being advised to start my program like this:Code:@ContextConfiguration(locations = {"classpath:/com/emc/config/spring.cfg.xml"}) @Configurable public class Hello { @Autowired private HelloService service; public void sayHello() { service.sayHello("Tom"); } public static void main(String[] args) { Hello hello = new Hello(); hello.sayHello(); } }
Personally, however, I find the other way using @ContextConfiguration much more elegant. How can such an annotation based context load be achieved in this situation?Code:ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/com/emc/config/spring.cfg.xml"); System.out.println(((HelloService)ctx.getBean("HelloService")).sayHello("Tom"));
Thank you in advance for any reply.
Best regards
Kessi


Reply With Quote

, I don't know what I was thinking... Although it would provide a slightly more consistent way of starting either a unit test or a main program.
