Skip to content

Commit bf06854

Browse files
committed
Merge branch '1.5.x'
2 parents 30e3cf4 + 1805cc5 commit bf06854

File tree

1 file changed

+57
-21
lines changed

1 file changed

+57
-21
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/using-spring-boot.adoc

+57-21
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,16 @@ reversed domain name (for example, `com.example.project`).
344344
[[using-boot-locating-the-main-class]]
345345
=== Locating the Main Application Class
346346
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.
351353

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.
355357

356358
The following listing shows a typical layout:
357359

@@ -376,20 +378,16 @@ The following listing shows a typical layout:
376378
----
377379

378380
The `Application.java` file would declare the `main` method, along with the basic
379-
`@Configuration`, as follows:
381+
`@SpringBootApplication`, as follows:
380382

381383
[source,java,indent=0]
382384
----
383385
package com.example.myapplication;
384386
385387
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
389389
390-
@Configuration
391-
@EnableAutoConfiguration
392-
@ComponentScan
390+
@SpringBootApplication
393391
public class Application {
394392
395393
public static void main(String[] args) {
@@ -441,8 +439,9 @@ then Spring Boot auto-configures an in-memory database.
441439
You need to opt-in to auto-configuration by adding the `@EnableAutoConfiguration` or
442440
`@SpringBootApplication` annotations to one of your `@Configuration` classes.
443441

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.
446445

447446

448447

@@ -546,11 +545,16 @@ TIP: Notice how using constructor injection lets the `riskAssessor` field be mar
546545

547546
[[using-boot-using-springbootapplication-annotation]]
548547
== 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
554558

555559
The `@SpringBootApplication` annotation is equivalent to using `@Configuration`,
556560
`@EnableAutoConfiguration`, and `@ComponentScan` with their default attributes, as shown
@@ -577,6 +581,38 @@ in the following example:
577581
NOTE: `@SpringBootApplication` also provides aliases to customize the attributes of
578582
`@EnableAutoConfiguration` and `@ComponentScan`.
579583

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+
580616

581617

582618
[[using-boot-running-your-application]]

0 commit comments

Comments
 (0)