@@ -417,14 +417,16 @@ and use a reversed domain name (for example, `com.example.project`).
417417[[using-boot-locating-the-main-class]]
418418=== Locating the main application class
419419We generally recommend that you locate your main application class in a root package
420- above other classes. The `@EnableAutoConfiguration` annotation is often placed on your
421- main class, and it implicitly defines a base "`search package`" for certain items. For
422- example, if you are writing a JPA application, the package of the
423- `@EnableAutoConfiguration` annotated class will be used to search for `@Entity` items.
420+ above other classes. The <<using-boot-using-springbootapplication-annotation,
421+ `@SpringBootApplication` annotation>> is often placed on your main class, and it
422+ implicitly defines a base "`search package`" for certain items. For example, if you are
423+ writing a JPA application, the package of the `@SpringBootApplication` annotated class
424+ will be used to search for `@Entity` items. Using a root package also allows component
425+ scan to apply only on your project.
424426
425- Using a root package also allows the `@ComponentScan` annotation to be used without
426- needing to specify a `basePackage` attribute. You can also use the
427- `@SpringBootApplication` annotation if your main class is in the root package .
427+ TIP: If you don't want to use `@SpringBootApplication`, the `@EnableAutoConfiguration`
428+ and `@ComponentScan` annotations that it imports defines that behaviour so you can also
429+ use that instead .
428430
429431Here is a typical layout:
430432
@@ -447,20 +449,16 @@ Here is a typical layout:
447449----
448450
449451The `Application.java` file would declare the `main` method, along with the basic
450- `@Configuration `.
452+ `@SpringBootApplication `.
451453
452454[source,java,indent=0]
453455----
454456 package com.example.myproject;
455457
456458 import org.springframework.boot.SpringApplication;
457- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
458- import org.springframework.context.annotation.ComponentScan;
459- import org.springframework.context.annotation.Configuration;
459+ import org.springframework.boot.autoconfigure.SpringBootApplication
460460
461- @Configuration
462- @EnableAutoConfiguration
463- @ComponentScan
461+ @SpringBootApplication
464462 public class Application {
465463
466464 public static void main(String[] args) {
@@ -512,8 +510,9 @@ connection beans, then we will auto-configure an in-memory database.
512510You need to opt-in to auto-configuration by adding the `@EnableAutoConfiguration` or
513511`@SpringBootApplication` annotations to one of your `@Configuration` classes.
514512
515- TIP: You should only ever add one `@EnableAutoConfiguration` annotation. We generally
516- recommend that you add it to your primary `@Configuration` class.
513+ TIP: You should only ever add one `@SpringBootApplication` or `@EnableAutoConfiguration`
514+ annotation. We generally recommend that you add one or the other to your primary
515+ `@Configuration` class only.
517516
518517
519518
@@ -614,10 +613,16 @@ as `final`, indicating that it cannot be subsequently changed.
614613
615614[[using-boot-using-springbootapplication-annotation]]
616615== Using the @SpringBootApplication annotation
617- Many Spring Boot developers always have their main class annotated with `@Configuration`,
618- `@EnableAutoConfiguration` and `@ComponentScan`. Since these annotations are so frequently
619- used together (especially if you follow the <<using-boot-structuring-your-code, best practices>>
620- above), Spring Boot provides a convenient `@SpringBootApplication` alternative.
616+ Many Spring Boot developers like their apps to use auto-configuration, component scan and
617+ be able to define extra configuration on their "application class". A single
618+ `@SpringBootApplication` annotation can be used to enable those tree features, that is:
619+
620+ * `@EnableAutoConfiguration`: enable <<using-boot-auto-configuration,Spring Boot's
621+ auto-configuration mechanism>>
622+ * `@ComponentScan`: enable `@Component` scan on the package where the application is
623+ located (see <<using-boot-structuring-your-code,the best practices>>)
624+ * `@Configuration`: allow to register extra beans in the context or import additional
625+ configuration classes
621626
622627The `@SpringBootApplication` annotation is equivalent to using `@Configuration`,
623628`@EnableAutoConfiguration` and `@ComponentScan` with their default attributes:
@@ -644,6 +649,39 @@ NOTE: `@SpringBootApplication` also provides aliases to customize the attributes
644649`@EnableAutoConfiguration` and `@ComponentScan`.
645650
646651
652+ [NOTE]
653+ ====
654+ None of these features are mandatory and you may chose to replace this single annotation
655+ by any of the features that it enables. For instance, you may not want to use component
656+ scan in your application:
657+
658+ [source,java,indent=0]
659+ ----
660+ package com.example.myproject;
661+
662+ import org.springframework.boot.SpringApplication;
663+ import org.springframework.context.annotation.ComponentScan
664+ import org.springframework.context.annotation.Configuration;
665+ import org.springframework.context.annotation.Import;
666+
667+ @Configuration
668+ @EnableAutoConfiguration
669+ @Import({ MyConfig.class, MyAnotherConfig.class })
670+ public class Application {
671+
672+ public static void main(String[] args) {
673+ SpringApplication.run(Application.class, args);
674+ }
675+
676+ }
677+ ----
678+
679+ In this example, `Application` is just like any other Spring Boot application except that
680+ `@Component`-annotated classes are not detected automatically and the user-defined beans
681+ are imported explicitly (see `@Import`).
682+ ====
683+
684+
647685
648686[[using-boot-running-your-application]]
649687== Running your application
0 commit comments