0

I am writing an app based on Spring Boot. I am trying to understand the difference and implications between doing either of the following:

AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppContext.class); 

or

SpringApplication.run(MySpringConfiguration.class, args); 

Particularly I am interested in following implications:

  1. Autowiring. When I manually get context the concrete objects inside my class don't get autowired even though I can see them in the context, when debugging.
  2. Testing. Which way is easier to test with?

1 Answer 1

1

Autowiring and testing should be nearly identical in both cases (the SpringApplication has a companion test annotation @SpringApplicationContext but that's the only main difference). The difference is in the features provided by SpringApplication on top of the vanilla application context (embedded servlet container, logging initialization, external config file parsing, etc.).

Sign up to request clarification or add additional context in comments.

2 Comments

Just to double check - stuff will not be "autowired" into a class in which I just do "new AnnotationConfigApplicationContext(..)". That's what I am seeing at least.
Autowiring should work by default if you use @Configuration in any form. Maybe you didn't do that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.