Results 1 to 10 of 12

Thread: @ContextConfiguration

Hybrid View

  1. #1
    Join Date
    Aug 2009
    Posts
    2

    Default @ContextConfiguration

    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:

    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();
    	}
    }
    does not seem to work. Instead, I'm continually being advised to start my program like this:

    Code:
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/com/emc/config/spring.cfg.xml");
    System.out.println(((HelloService)ctx.getBean("HelloService")).sayHello("Tom"));
    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?

    Thank you in advance for any reply.

    Best regards
    Kessi

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    You don't and you will never be able to do so. Not without changing the way classes are loaded (you will need something that knows what to do with the @ContextConfiguration annotation). You would need AOP for that, next to that you wouldn't want the context to be initialized all the time.

    You will need to create a ApplicationContext and you need to configure AspectJ to do loadtime weaving (add a java agent to the classpath) to make the @Configurable work. That way your new is working but you will first need an ApplicationContext which the Aspect can use for autowiring.

    P.S. Please next time don't post multiple times!
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    423

    Default

    In addition to what Martin pointed out, @ContextConfiguration is part of the the Spring TestContext Framework. As such it is packaged in the "org.springframework.test" module and should never be used in production code.

    - Sam

  4. #4
    Join Date
    Nov 2007
    Location
    Netherlands
    Posts
    36

    Default

    I do agree with the original poster that using @ContextConfiguration could be an easier way to fire up an application. As has been told there is of course no way to start Spring without calling Spring in your main method.

    But it would be almost trivial for the people who programmed the JUnit test runner to implement something simple like a "SpringStarter" to make it work, so we can start the program by doing something like:

    Code:
    	public static void main(String[] args) {
    		SpringStarter.start();
    	}

  5. #5
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    423

    Default

    Quote Originally Posted by SlowStrider View Post
    But it would be almost trivial for the people who programmed the JUnit test runner to implement something simple like a "SpringStarter" to make it work, so we can start the program by doing something like:
    Is the following not trivial enough for you?

    Code:
    public static void main(String[] args) {
    	ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
    	Service service = (Service) ctx.getBean("service");
    }
    FYI: I am the person "who programmed the JUnit test runner".

    Regards,

    Sam

    p.s. you may be interested in some of the new Java-based container configuration support (i.e., annotation-driven configuration options) that have been introduced in Spring 3.0 (e.g., @Configuration, @Bean, @Lazy, @Import, etc.). These features have been incorporated into the core Spring Framework from the Spring JavaConfig project.

  6. #6
    Join Date
    Nov 2007
    Location
    Netherlands
    Posts
    36

    Default

    Well now you mention it, it doesn't matter much in terms of lines of code , 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.

    My example would also be a bit longer: I forgot to pass in the Class to my hypothetical "SpringStarter"; it would be handy if it knows where to find the @ContextConfiguration.

  7. #7
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    423

    Default

    Quote Originally Posted by SlowStrider View Post
    My example would also be a bit longer: I forgot to pass in the Class to my hypothetical "SpringStarter"; it would be handy if it knows where to find the @ContextConfiguration.
    Like I alluded to before, it really sounds like you might be interested in Spring JavaConfig. Consider the following example taken from the JavaConfig Reference Manual:

    Code:
    JavaConfigApplicationContext context = new JavaConfigApplicationContext(AppConfig.class);
    Service service = context.getBean(Service.class);
    - Sam

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
  •