@@ -344,14 +344,16 @@ reversed domain name (for example, `com.example.project`).
344
344
[[using-boot-locating-the-main-class]]
345
345
=== Locating the Main Application Class
346
346
We generally recommend that you locate your main application class in a root package
347
- above other classes. The `@EnableAutoConfiguration` annotation is often placed on your
348
- main class, and it implicitly defines a base "`search package`" for certain items. For
349
- example, if you are writing a JPA application, the package of the
350
- `@EnableAutoConfiguration` annotated class is used to search for `@Entity` items.
347
+ above other classes. The <<using-boot-using-springbootapplication-annotation,
348
+ `@SpringBootApplication` annotation>> is often placed on your main class, and it
349
+ implicitly defines a base "`search package`" for certain items. For example, if you are
350
+ writing a JPA application, the package of the `@SpringBootApplication` annotated class
351
+ is used to search for `@Entity` items. Using a root package also allows component
352
+ scan to apply only on your project.
351
353
352
- Using a root package also lets the `@ComponentScan` annotation be used without needing to
353
- specify a `basePackage` attribute. You can also use the `@SpringBootApplication`
354
- annotation if your main class is in the root package .
354
+ TIP: If you don't want to use `@SpringBootApplication`, the `@EnableAutoConfiguration`
355
+ and `@ComponentScan` annotations that it imports defines that behaviour so you can also
356
+ use that instead .
355
357
356
358
The following listing shows a typical layout:
357
359
@@ -376,20 +378,16 @@ The following listing shows a typical layout:
376
378
----
377
379
378
380
The `Application.java` file would declare the `main` method, along with the basic
379
- `@Configuration `, as follows:
381
+ `@SpringBootApplication `, as follows:
380
382
381
383
[source,java,indent=0]
382
384
----
383
385
package com.example.myapplication;
384
386
385
387
import org.springframework.boot.SpringApplication;
386
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
387
- import org.springframework.context.annotation.ComponentScan;
388
- import org.springframework.context.annotation.Configuration;
388
+ import org.springframework.boot.autoconfigure.SpringBootApplication
389
389
390
- @Configuration
391
- @EnableAutoConfiguration
392
- @ComponentScan
390
+ @SpringBootApplication
393
391
public class Application {
394
392
395
393
public static void main(String[] args) {
@@ -441,8 +439,9 @@ then Spring Boot auto-configures an in-memory database.
441
439
You need to opt-in to auto-configuration by adding the `@EnableAutoConfiguration` or
442
440
`@SpringBootApplication` annotations to one of your `@Configuration` classes.
443
441
444
- TIP: You should only ever add one `@EnableAutoConfiguration` annotation. We generally
445
- recommend that you add it to your primary `@Configuration` class.
442
+ TIP: You should only ever add one `@SpringBootApplication` or `@EnableAutoConfiguration`
443
+ annotation. We generally recommend that you add one or the other to your primary
444
+ `@Configuration` class only.
446
445
447
446
448
447
@@ -546,11 +545,16 @@ TIP: Notice how using constructor injection lets the `riskAssessor` field be mar
546
545
547
546
[[using-boot-using-springbootapplication-annotation]]
548
547
== Using the @SpringBootApplication Annotation
549
- Many Spring Boot developers always have their main class annotated with `@Configuration`,
550
- `@EnableAutoConfiguration`, and `@ComponentScan`. Since these annotations are so
551
- frequently used together (especially if you follow the
552
- <<using-boot-structuring-your-code, best practices>> above), Spring Boot provides a
553
- convenient `@SpringBootApplication` alternative.
548
+ Many Spring Boot developers like their apps to use auto-configuration, component scan and
549
+ be able to define extra configuration on their "application class". A single
550
+ `@SpringBootApplication` annotation can be used to enable those tree features, that is:
551
+
552
+ * `@EnableAutoConfiguration`: enable <<using-boot-auto-configuration,Spring Boot's
553
+ auto-configuration mechanism>>
554
+ * `@ComponentScan`: enable `@Component` scan on the package where the application is
555
+ located (see <<using-boot-structuring-your-code,the best practices>>)
556
+ * `@Configuration`: allow to register extra beans in the context or import additional
557
+ configuration classes
554
558
555
559
The `@SpringBootApplication` annotation is equivalent to using `@Configuration`,
556
560
`@EnableAutoConfiguration`, and `@ComponentScan` with their default attributes, as shown
@@ -577,6 +581,38 @@ in the following example:
577
581
NOTE: `@SpringBootApplication` also provides aliases to customize the attributes of
578
582
`@EnableAutoConfiguration` and `@ComponentScan`.
579
583
584
+ [NOTE]
585
+ ====
586
+ None of these features are mandatory and you may chose to replace this single annotation
587
+ by any of the features that it enables. For instance, you may not want to use component
588
+ scan in your application:
589
+
590
+ [source,java,indent=0]
591
+ ----
592
+ package com.example.myapplication;
593
+
594
+ import org.springframework.boot.SpringApplication;
595
+ import org.springframework.context.annotation.ComponentScan
596
+ import org.springframework.context.annotation.Configuration;
597
+ import org.springframework.context.annotation.Import;
598
+
599
+ @Configuration
600
+ @EnableAutoConfiguration
601
+ @Import({ MyConfig.class, MyAnotherConfig.class })
602
+ public class Application {
603
+
604
+ public static void main(String[] args) {
605
+ SpringApplication.run(Application.class, args);
606
+ }
607
+
608
+ }
609
+ ----
610
+
611
+ In this example, `Application` is just like any other Spring Boot application except that
612
+ `@Component`-annotated classes are not detected automatically and the user-defined beans
613
+ are imported explicitly (see `@Import`).
614
+ ====
615
+
580
616
581
617
582
618
[[using-boot-running-your-application]]
0 commit comments