Many Spring Boot developers like their apps to use auto-configuration, component scan and
be able to define extra configuration on their "application class". A single
@SpringBootApplication
annotation can be used to enable those tree features, that is:
@EnableAutoConfiguration
: enable Spring Boot’s
auto-configuration mechanism@ComponentScan
: enable @Component
scan on the package where the application is
located (see the best practices)@Configuration
: allow to register extra beans in the context or import additional
configuration classesThe @SpringBootApplication
annotation is equivalent to using @Configuration
,
@EnableAutoConfiguration
and @ComponentScan
with their default attributes:
package com.example.myproject; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Note | |
---|---|
|
Note | |
---|---|
None of these features are mandatory and you may chose to replace this single annotation by any of the features that it enables. For instance, you may not want to use component scan in your application: package com.example.myproject; import org.springframework.boot.SpringApplication; import org.springframework.context.annotation.ComponentScan import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @EnableAutoConfiguration @Import({ MyConfig.class, MyAnotherConfig.class }) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } In this example, |