using.md 69.2 KB
Newer Older
茶陵後's avatar
茶陵後 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
# Developing with Spring Boot

This section goes into more detail about how you should use Spring Boot.
It covers topics such as build systems, auto-configuration, and how to run your applications.
We also cover some Spring Boot best practices.
Although there is nothing particularly special about Spring Boot (it is just another library that you can consume), there are a few recommendations that, when followed, make your development process a little easier.

If you are starting out with Spring Boot, you should probably read the *[Getting Started](getting-started.html#getting-started)* guide before diving into this section.

## 1. Build Systems

It is strongly recommended that you choose a build system that supports [*dependency management*](#using.build-systems.dependency-management) and that can consume artifacts published to the “Maven Central” repository.
We would recommend that you choose Maven or Gradle.
It is possible to get Spring Boot to work with other build systems (Ant, for example), but they are not particularly well supported.

### 1.1. Dependency Management

Each release of Spring Boot provides a curated list of dependencies that it supports.
In practice, you do not need to provide a version for any of these dependencies in your build configuration, as Spring Boot manages that for you.
When you upgrade Spring Boot itself, these dependencies are upgraded as well in a consistent way.

|   |You can still specify a version and override Spring Boot’s recommendations if you need to do so.|
|---|------------------------------------------------------------------------------------------------|

The curated list contains all the Spring modules that you can use with Spring Boot as well as a refined list of third party libraries.
The list is available as a standard Bills of Materials (`spring-boot-dependencies`) that can be used with both [Maven](#using.build-systems.maven) and [Gradle](#using.build-systems.gradle).

|   |Each release of Spring Boot is associated with a base version of the Spring Framework.<br/>We **highly** recommend that you not specify its version.|
|---|----------------------------------------------------------------------------------------------------------------------------------------------------|

### 1.2. Maven

To learn about using Spring Boot with Maven, see the documentation for Spring Boot’s Maven plugin:

* Reference ([HTML](https://docs.spring.io/spring-boot/docs/2.6.4/maven-plugin/reference/htmlsingle/) and [PDF](https://docs.spring.io/spring-boot/docs/2.6.4/maven-plugin/reference/pdf/spring-boot-maven-plugin-reference.pdf))

* [API](https://docs.spring.io/spring-boot/docs/2.6.4/maven-plugin/api/)

### 1.3. Gradle

To learn about using Spring Boot with Gradle, see the documentation for Spring Boot’s Gradle plugin:

* Reference ([HTML](https://docs.spring.io/spring-boot/docs/2.6.4/gradle-plugin/reference/htmlsingle/) and [PDF](https://docs.spring.io/spring-boot/docs/2.6.4/gradle-plugin/reference/pdf/spring-boot-gradle-plugin-reference.pdf))

* [API](https://docs.spring.io/spring-boot/docs/2.6.4/gradle-plugin/api/)

### 1.4. Ant

It is possible to build a Spring Boot project using Apache Ant+Ivy.
The `spring-boot-antlib` “AntLib” module is also available to help Ant create executable jars.

To declare dependencies, a typical `ivy.xml` file looks something like the following example:

```
<ivy-module version="2.0">
    <info organisation="org.springframework.boot" module="spring-boot-sample-ant" />
    <configurations>
        <conf name="compile" description="everything needed to compile this module" />
        <conf name="runtime" extends="compile" description="everything needed to run this module" />
    </configurations>
    <dependencies>
        <dependency org="org.springframework.boot" name="spring-boot-starter"
            rev="${spring-boot.version}" conf="compile" />
    </dependencies>
</ivy-module>
```

A typical `build.xml` looks like the following example:

```
<project
    xmlns:ivy="antlib:org.apache.ivy.ant"
    xmlns:spring-boot="antlib:org.springframework.boot.ant"
    name="myapp" default="build">

    <property name="spring-boot.version" value="2.6.4" />

    <target name="resolve" description="--> retrieve dependencies with ivy">
        <ivy:retrieve pattern="lib/[conf]/[artifact]-[type]-[revision].[ext]" />
    </target>

    <target name="classpaths" depends="resolve">
        <path id="compile.classpath">
            <fileset dir="lib/compile" includes="*.jar" />
        </path>
    </target>

    <target name="init" depends="classpaths">
        <mkdir dir="build/classes" />
    </target>

    <target name="compile" depends="init" description="compile">
        <javac srcdir="src/main/java" destdir="build/classes" classpathref="compile.classpath" />
    </target>

    <target name="build" depends="compile">
        <spring-boot:exejar destfile="build/myapp.jar" classes="build/classes">
            <spring-boot:lib>
                <fileset dir="lib/runtime" />
            </spring-boot:lib>
        </spring-boot:exejar>
    </target>
</project>
```

|   |If you do not want to use the `spring-boot-antlib` module, see the *[howto.html](howto.html#howto.build.build-an-executable-archive-with-ant-without-using-spring-boot-antlib)* “How-to” .|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

### 1.5. Starters

Starters are a set of convenient dependency descriptors that you can include in your application.
You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors.
For example, if you want to get started using Spring and JPA for database access, include the `spring-boot-starter-data-jpa` dependency in your project.

The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.

What is in a name

All **official** starters follow a similar naming pattern; `spring-boot-starter-*`, where `*` is a particular type of application.
This naming structure is intended to help when you need to find a starter.
The Maven integration in many IDEs lets you search dependencies by name.
For example, with the appropriate Eclipse or Spring Tools plugin installed, you can press `ctrl-space` in the POM editor and type “spring-boot-starter” for a complete list.

As explained in the “[Creating Your Own Starter](features.html#features.developing-auto-configuration.custom-starter)” section, third party starters should not start with `spring-boot`, as it is reserved for official Spring Boot artifacts.
Rather, a third-party starter typically starts with the name of the project.
For example, a third-party starter project called `thirdpartyproject` would typically be named `thirdpartyproject-spring-boot-starter`.

The following application starters are provided by Spring Boot under the `org.springframework.boot` group:

|                      Name                       |                                                                                             Description                                                                                              |
|-------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|            []()`spring-boot-starter`            |                                                                 Core starter, including auto-configuration support, logging and YAML                                                                 |
|       []()`spring-boot-starter-activemq`        |                                                                           Starter for JMS messaging using Apache ActiveMQ                                                                            |
|         []()`spring-boot-starter-amqp`          |                                                                             Starter for using Spring AMQP and Rabbit MQ                                                                              |
|          []()`spring-boot-starter-aop`          |                                                                 Starter for aspect-oriented programming with Spring AOP and AspectJ                                                                  |
|        []()`spring-boot-starter-artemis`        |                                                                            Starter for JMS messaging using Apache Artemis                                                                            |
|         []()`spring-boot-starter-batch`         |                                                                                    Starter for using Spring Batch                                                                                    |
|         []()`spring-boot-starter-cache`         |                                                                         Starter for using Spring Framework’s caching support                                                                         |
|    []()`spring-boot-starter-data-cassandra`     |                                                              Starter for using Cassandra distributed database and Spring Data Cassandra                                                              |
|[]()`spring-boot-starter-data-cassandra-reactive`|                                                         Starter for using Cassandra distributed database and Spring Data Cassandra Reactive                                                          |
|    []()`spring-boot-starter-data-couchbase`     |                                                           Starter for using Couchbase document-oriented database and Spring Data Couchbase                                                           |
|[]()`spring-boot-starter-data-couchbase-reactive`|                                                      Starter for using Couchbase document-oriented database and Spring Data Couchbase Reactive                                                       |
|  []()`spring-boot-starter-data-elasticsearch`   |                                                      Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch                                                       |
|       []()`spring-boot-starter-data-jdbc`       |                                                                                  Starter for using Spring Data JDBC                                                                                  |
|       []()`spring-boot-starter-data-jpa`        |                                                                           Starter for using Spring Data JPA with Hibernate                                                                           |
|       []()`spring-boot-starter-data-ldap`       |                                                                                  Starter for using Spring Data LDAP                                                                                  |
|     []()`spring-boot-starter-data-mongodb`      |                                                             Starter for using MongoDB document-oriented database and Spring Data MongoDB                                                             |
| []()`spring-boot-starter-data-mongodb-reactive` |                                                        Starter for using MongoDB document-oriented database and Spring Data MongoDB Reactive                                                         |
|      []()`spring-boot-starter-data-neo4j`       |                                                                     Starter for using Neo4j graph database and Spring Data Neo4j                                                                     |
|      []()`spring-boot-starter-data-r2dbc`       |                                                                                 Starter for using Spring Data R2DBC                                                                                  |
|      []()`spring-boot-starter-data-redis`       |                                                      Starter for using Redis key-value data store with Spring Data Redis and the Lettuce client                                                      |
|  []()`spring-boot-starter-data-redis-reactive`  |                                                 Starter for using Redis key-value data store with Spring Data Redis reactive and the Lettuce client                                                  |
|       []()`spring-boot-starter-data-rest`       |                                                            Starter for exposing Spring Data repositories over REST using Spring Data REST                                                            |
|      []()`spring-boot-starter-freemarker`       |                                                                   Starter for building MVC web applications using FreeMarker views                                                                   |
|   []()`spring-boot-starter-groovy-templates`    |                                                                Starter for building MVC web applications using Groovy Templates views                                                                |
|        []()`spring-boot-starter-hateoas`        |                                                   Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS                                                   |
|      []()`spring-boot-starter-integration`      |                                                                                 Starter for using Spring Integration                                                                                 |
|         []()`spring-boot-starter-jdbc`          |                                                                       Starter for using JDBC with the HikariCP connection pool                                                                       |
|        []()`spring-boot-starter-jersey`         |                            Starter for building RESTful web applications using JAX-RS and Jersey. An alternative to [`spring-boot-starter-web`](#spring-boot-starter-web)                            |
|         []()`spring-boot-starter-jooq`          |Starter for using jOOQ to access SQL databases with JDBC. An alternative to [`spring-boot-starter-data-jpa`](#spring-boot-starter-data-jpa) or [`spring-boot-starter-jdbc`](#spring-boot-starter-jdbc)|
|         []()`spring-boot-starter-json`          |                                                                                 Starter for reading and writing json                                                                                 |
|     []()`spring-boot-starter-jta-atomikos`      |                                                                             Starter for JTA transactions using Atomikos                                                                              |
|         []()`spring-boot-starter-mail`          |                                                               Starter for using Java Mail and Spring Framework’s email sending support                                                               |
|       []()`spring-boot-starter-mustache`        |                                                                      Starter for building web applications using Mustache views                                                                      |
|     []()`spring-boot-starter-oauth2-client`     |                                                              Starter for using Spring Security’s OAuth2/OpenID Connect client features                                                               |
|[]()`spring-boot-starter-oauth2-resource-server` |                                                                 Starter for using Spring Security’s OAuth2 resource server features                                                                  |
|        []()`spring-boot-starter-quartz`         |                                                                                Starter for using the Quartz scheduler                                                                                |
|        []()`spring-boot-starter-rsocket`        |                                                                           Starter for building RSocket clients and servers                                                                           |
|       []()`spring-boot-starter-security`        |                                                                                  Starter for using Spring Security                                                                                   |
|         []()`spring-boot-starter-test`          |                                              Starter for testing Spring Boot applications with libraries including JUnit Jupiter, Hamcrest and Mockito                                               |
|       []()`spring-boot-starter-thymeleaf`       |                                                                   Starter for building MVC web applications using Thymeleaf views                                                                    |
|      []()`spring-boot-starter-validation`       |                                                                   Starter for using Java Bean Validation with Hibernate Validator                                                                    |
|          []()`spring-boot-starter-web`          |                                      Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container                                       |
|     []()`spring-boot-starter-web-services`      |                                                                                Starter for using Spring Web Services                                                                                 |
|        []()`spring-boot-starter-webflux`        |                                                       Starter for building WebFlux applications using Spring Framework’s Reactive Web support                                                        |
|       []()`spring-boot-starter-websocket`       |                                                        Starter for building WebSocket applications using Spring Framework’s WebSocket support                                                        |

In addition to the application starters, the following starters can be used to add *[production ready](actuator.html#actuator)* features:

|               Name               |                                                           Description                                                           |
|----------------------------------|---------------------------------------------------------------------------------------------------------------------------------|
|[]()`spring-boot-starter-actuator`|Starter for using Spring Boot’s Actuator which provides production ready features to help you monitor and manage your application|

Finally, Spring Boot also includes the following starters that can be used if you want to exclude or swap specific technical facets:

|                 Name                  |                                                                        Description                                                                        |
|---------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|
|    []()`spring-boot-starter-jetty`    |         Starter for using Jetty as the embedded servlet container. An alternative to [`spring-boot-starter-tomcat`](#spring-boot-starter-tomcat)          |
|   []()`spring-boot-starter-log4j2`    |                   Starter for using Log4j2 for logging. An alternative to [`spring-boot-starter-logging`](#spring-boot-starter-logging)                   |
|   []()`spring-boot-starter-logging`   |                                                Starter for logging using Logback. Default logging starter                                                 |
|[]()`spring-boot-starter-reactor-netty`|                                           Starter for using Reactor Netty as the embedded reactive HTTP server.                                           |
|   []()`spring-boot-starter-tomcat`    |Starter for using Tomcat as the embedded servlet container. Default servlet container starter used by [`spring-boot-starter-web`](#spring-boot-starter-web)|
|  []()`spring-boot-starter-undertow`   |        Starter for using Undertow as the embedded servlet container. An alternative to [`spring-boot-starter-tomcat`](#spring-boot-starter-tomcat)        |

To learn how to swap technical facets, please see the how-to documentation for [swapping web server](howto.html#howto.webserver.use-another) and [logging system](howto.html#howto.logging.log4j).

|   |For a list of additional community contributed starters, see the [README file](https://github.com/spring-projects/spring-boot/tree/main/spring-boot-project/spring-boot-starters/README.adoc) in the `spring-boot-starters` module on GitHub.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

## 2. Structuring Your Code

Spring Boot does not require any specific code layout to work.
However, there are some best practices that help.

### 2.1. Using the “default” Package

When a class does not include a `package` declaration, it is considered to be in the “default package”.
The use of the “default package” is generally discouraged and should be avoided.
It can cause particular problems for Spring Boot applications that use the `@ComponentScan`, `@ConfigurationPropertiesScan`, `@EntityScan`, or `@SpringBootApplication` annotations, since every class from every jar is read.

|   |We recommend that you follow Java’s recommended package naming conventions and use a reversed domain name (for example, `com.example.project`).|
|---|-----------------------------------------------------------------------------------------------------------------------------------------------|

### 2.2. Locating the Main Application Class

We generally recommend that you locate your main application class in a root package above other classes.
The [`@SpringBootApplication` annotation](#using.using-the-springbootapplication-annotation) is often placed on your main class, and it implicitly defines a base “search package” for certain items.
For example, if you are writing a JPA application, the package of the `@SpringBootApplication` annotated class is used to search for `@Entity` items.
Using a root package also allows component scan to apply only on your project.

|   |If you do not want to use `@SpringBootApplication`, the `@EnableAutoConfiguration` and `@ComponentScan` annotations that it imports defines that behavior so you can also use those instead.|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

The following listing shows a typical layout:

```
com
 +- example
     +- myapplication
         +- MyApplication.java
         |
         +- customer
         |   +- Customer.java
         |   +- CustomerController.java
         |   +- CustomerService.java
         |   +- CustomerRepository.java
         |
         +- order
             +- Order.java
             +- OrderController.java
             +- OrderService.java
             +- OrderRepository.java
```

The `MyApplication.java` file would declare the `main` method, along with the basic `@SpringBootApplication`, as follows:

```
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

```

## 3. Configuration Classes

Spring Boot favors Java-based configuration.
Although it is possible to use `SpringApplication` with XML sources, we generally recommend that your primary source be a single `@Configuration` class.
Usually the class that defines the `main` method is a good candidate as the primary `@Configuration`.

|   |Many Spring configuration examples have been published on the Internet that use XML configuration.<br/>If possible, always try to use the equivalent Java-based configuration.<br/>Searching for `Enable*` annotations can be a good starting point.|
|---|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

### 3.1. Importing Additional Configuration Classes ###

You need not put all your `@Configuration` into a single class.
The `@Import` annotation can be used to import additional configuration classes.
Alternatively, you can use `@ComponentScan` to automatically pick up all Spring components, including `@Configuration` classes.

### 3.2. Importing XML Configuration

If you absolutely must use XML based configuration, we recommend that you still start with a `@Configuration` class.
You can then use an `@ImportResource` annotation to load XML configuration files.

## 4. Auto-configuration

Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added.
For example, if `HSQLDB` is on your classpath, and you have not manually configured any database connection beans, then Spring Boot auto-configures an in-memory database.

You need to opt-in to auto-configuration by adding the `@EnableAutoConfiguration` or `@SpringBootApplication` annotations to one of your `@Configuration` classes.

|   |You should only ever add one `@SpringBootApplication` or `@EnableAutoConfiguration` annotation.<br/>We generally recommend that you add one or the other to your primary `@Configuration` class only.|
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

### 4.1. Gradually Replacing Auto-configuration

Auto-configuration is non-invasive.
At any point, you can start to define your own configuration to replace specific parts of the auto-configuration.
For example, if you add your own `DataSource` bean, the default embedded database support backs away.

If you need to find out what auto-configuration is currently being applied, and why, start your application with the `--debug` switch.
Doing so enables debug logs for a selection of core loggers and logs a conditions report to the console.

### 4.2. Disabling Specific Auto-configuration Classes

If you find that specific auto-configuration classes that you do not want are being applied, you can use the exclude attribute of `@SpringBootApplication` to disable them, as shown in the following example:

```
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class MyApplication {

}

```

If the class is not on the classpath, you can use the `excludeName` attribute of the annotation and specify the fully qualified name instead.
If you prefer to use `@EnableAutoConfiguration` rather than `@SpringBootApplication`, `exclude` and `excludeName` are also available.
Finally, you can also control the list of auto-configuration classes to exclude by using the `spring.autoconfigure.exclude` property.

|   |You can define exclusions both at the annotation level and by using the property.|
|---|---------------------------------------------------------------------------------|

|   |Even though auto-configuration classes are `public`, the only aspect of the class that is considered public API is the name of the class which can be used for disabling the auto-configuration.<br/>The actual contents of those classes, such as nested configuration classes or bean methods are for internal use only and we do not recommend using those directly.|
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

## 5. Spring Beans and Dependency Injection

You are free to use any of the standard Spring Framework techniques to define your beans and their injected dependencies.
We generally recommend using constructor injection to wire up dependencies and `@ComponentScan` to find beans.

If you structure your code as suggested above (locating your application class in a top package), you can add `@ComponentScan` without any arguments or use the `@SpringBootApplication` annotation which implicitly includes it.
All of your application components (`@Component`, `@Service`, `@Repository`, `@Controller`, and others) are automatically registered as Spring Beans.

The following example shows a `@Service` Bean that uses constructor injection to obtain a required `RiskAssessor` bean:

```
import org.springframework.stereotype.Service;

@Service
public class MyAccountService implements AccountService {

    private final RiskAssessor riskAssessor;

    public MyAccountService(RiskAssessor riskAssessor) {
        this.riskAssessor = riskAssessor;
    }

    // ...

}

```

If a bean has more than one constructor, you will need to mark the one you want Spring to use with `@Autowired`:

```
import java.io.PrintStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyAccountService implements AccountService {

    private final RiskAssessor riskAssessor;

    private final PrintStream out;

    @Autowired
    public MyAccountService(RiskAssessor riskAssessor) {
        this.riskAssessor = riskAssessor;
        this.out = System.out;
    }

    public MyAccountService(RiskAssessor riskAssessor, PrintStream out) {
        this.riskAssessor = riskAssessor;
        this.out = out;
    }

    // ...

}

```

|   |Notice how using constructor injection lets the `riskAssessor` field be marked as `final`, indicating that it cannot be subsequently changed.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------|

## 6. Using the @SpringBootApplication Annotation

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 three features, that is:

* `@EnableAutoConfiguration`: enable [Spring Boot’s auto-configuration mechanism](#using.auto-configuration)

* `@ComponentScan`: enable `@Component` scan on the package where the application is located (see [the best practices](#using.structuring-your-code))

* `@SpringBootConfiguration`: enable registration of extra beans in the context or the import of additional configuration classes.
  An alternative to Spring’s standard `@Configuration` that aids [configuration detection](features.html#features.testing.spring-boot-applications.detecting-configuration) in your integration tests.

```
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// Same as @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan
@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

```

|   |`@SpringBootApplication` also provides aliases to customize the attributes of `@EnableAutoConfiguration` and `@ComponentScan`.|
|---|------------------------------------------------------------------------------------------------------------------------------|

|   |None of these features are mandatory and you may choose to replace this single annotation by any of the features that it enables.<br/>For instance, you may not want to use component scan or configuration properties scan in your application:<br/><br/>```<br/>import org.springframework.boot.SpringApplication;<br/>import org.springframework.boot.SpringBootConfiguration;<br/>import org.springframework.boot.autoconfigure.EnableAutoConfiguration;<br/>import org.springframework.context.annotation.Import;<br/><br/>@SpringBootConfiguration(proxyBeanMethods = false)<br/>@EnableAutoConfiguration<br/>@Import({ SomeConfiguration.class, AnotherConfiguration.class })<br/>public class MyApplication {<br/><br/>    public static void main(String[] args) {<br/>        SpringApplication.run(MyApplication.class, args);<br/>    }<br/><br/>}<br/><br/>```<br/><br/>In this example, `MyApplication` is just like any other Spring Boot application except that `@Component`-annotated classes and `@ConfigurationProperties`-annotated classes are not detected automatically and the user-defined beans are imported explicitly (see `@Import`).|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

## 7. Running Your Application

One of the biggest advantages of packaging your application as a jar and using an embedded HTTP server is that you can run your application as you would any other.
The sample applies to debugging Spring Boot applications.
You do not need any special IDE plugins or extensions.

|   |This section only covers jar-based packaging.<br/>If you choose to package your application as a war file, see your server and IDE documentation.|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------|

### 7.1. Running from an IDE

You can run a Spring Boot application from your IDE as a Java application.
However, you first need to import your project.
Import steps vary depending on your IDE and build system.
Most IDEs can import Maven projects directly.
For example, Eclipse users can select `Import…​``Existing Maven Projects` from the `File` menu.

If you cannot directly import your project into your IDE, you may be able to generate IDE metadata by using a build plugin.
Maven includes plugins for [Eclipse](https://maven.apache.org/plugins/maven-eclipse-plugin/) and [IDEA](https://maven.apache.org/plugins/maven-idea-plugin/).
Gradle offers plugins for [various IDEs](https://docs.gradle.org/current/userguide/userguide.html).

|   |If you accidentally run a web application twice, you see a “Port already in use” error.<br/>Spring Tools users can use the `Relaunch` button rather than the `Run` button to ensure that any existing instance is closed.|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

### 7.2. Running as a Packaged Application

If you use the Spring Boot Maven or Gradle plugins to create an executable jar, you can run your application using `java -jar`, as shown in the following example:

```
$ java -jar target/myapplication-0.0.1-SNAPSHOT.jar
```

It is also possible to run a packaged application with remote debugging support enabled.
Doing so lets you attach a debugger to your packaged application, as shown in the following example:

```
$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
       -jar target/myapplication-0.0.1-SNAPSHOT.jar
```

### 7.3. Using the Maven Plugin

The Spring Boot Maven plugin includes a `run` goal that can be used to quickly compile and run your application.
Applications run in an exploded form, as they do in your IDE.
The following example shows a typical Maven command to run a Spring Boot application:

```
$ mvn spring-boot:run
```

You might also want to use the `MAVEN_OPTS` operating system environment variable, as shown in the following example:

```
$ export MAVEN_OPTS=-Xmx1024m
```

### 7.4. Using the Gradle Plugin

The Spring Boot Gradle plugin also includes a `bootRun` task that can be used to run your application in an exploded form.
The `bootRun` task is added whenever you apply the `org.springframework.boot` and `java` plugins and is shown in the following example:

```
$ gradle bootRun
```

You might also want to use the `JAVA_OPTS` operating system environment variable, as shown in the following example:

```
$ export JAVA_OPTS=-Xmx1024m
```

### 7.5. Hot Swapping

Since Spring Boot applications are plain Java applications, JVM hot-swapping should work out of the box.
JVM hot swapping is somewhat limited with the bytecode that it can replace.
For a more complete solution, [JRebel](https://www.jrebel.com/products/jrebel) can be used.

The `spring-boot-devtools` module also includes support for quick application restarts.
See the [Hot swapping “How-to”](howto.html#howto.hotswapping) for details.

## 8. Developer Tools

Spring Boot includes an additional set of tools that can make the application development experience a little more pleasant.
The `spring-boot-devtools` module can be included in any project to provide additional development-time features.
To include devtools support, add the module dependency to your build, as shown in the following listings for Maven and Gradle:

Maven

```
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>
```

Gradle

```
dependencies {
    developmentOnly("org.springframework.boot:spring-boot-devtools")
}
```

|   |Devtools might cause classloading issues, in particular in multi-module projects.[Diagnosing Classloading Issues](#using.devtools.diagnosing-classloading-issues) explains how to diagnose and solve them.|
|---|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

|   |Developer tools are automatically disabled when running a fully packaged application.<br/>If your application is launched from `java -jar` or if it is started from a special classloader, then it is considered a “production application”.<br/>You can control this behavior by using the `spring.devtools.restart.enabled` system property.<br/>To enable devtools, irrespective of the classloader used to launch your application, set the `-Dspring.devtools.restart.enabled=true` system property.<br/>This must not be done in a production environment where running devtools is a security risk.<br/>To disable devtools, exclude the dependency or set the `-Dspring.devtools.restart.enabled=false` system property.|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

|   |Flagging the dependency as optional in Maven or using the `developmentOnly` configuration in Gradle (as shown above) prevents devtools from being transitively applied to other modules that use your project.|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

|   |Repackaged archives do not contain devtools by default.<br/>If you want to use a [certain remote devtools feature](#using.devtools.remote-applications), you need to include it.<br/>When using the Maven plugin, set the `excludeDevtools` property to `false`.<br/>When using the Gradle plugin, [configure the task’s classpath to include the `developmentOnly` configuration](https://docs.spring.io/spring-boot/docs/2.6.4/gradle-plugin/reference/htmlsingle/#packaging-executable-configuring-including-development-only-dependencies).|
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

### 8.1. Diagnosing Classloading Issues

As described in the [Restart vs Reload](#using.devtools.restart.restart-vs-reload) section, restart functionality is implemented by using two classloaders.
For most applications, this approach works well.
However, it can sometimes cause classloading issues, in particular in multi-module projects.

To diagnose whether the classloading issues are indeed caused by devtools and its two classloaders, [try disabling restart](#using.devtools.restart.disable).
If this solves your problems, [customize the restart classloader](#using.devtools.restart.customizing-the-classload) to include your entire project.

### 8.2. Property Defaults

Several of the libraries supported by Spring Boot use caches to improve performance.
For example, [template engines](web.html#web.servlet.spring-mvc.template-engines) cache compiled templates to avoid repeatedly parsing template files.
Also, Spring MVC can add HTTP caching headers to responses when serving static resources.

While caching is very beneficial in production, it can be counter-productive during development, preventing you from seeing the changes you just made in your application.
For this reason, spring-boot-devtools disables the caching options by default.

Cache options are usually configured by settings in your `application.properties` file.
For example, Thymeleaf offers the `spring.thymeleaf.cache` property.
Rather than needing to set these properties manually, the `spring-boot-devtools` module automatically applies sensible development-time configuration.

Because you need more information about web requests while developing Spring MVC and Spring WebFlux applications, developer tools suggests you to enable `DEBUG` logging for the `web` logging group.
This will give you information about the incoming request, which handler is processing it, the response outcome, and other details.
If you wish to log all request details (including potentially sensitive information), you can turn on the `spring.mvc.log-request-details` or `spring.codec.log-request-details` configuration properties.

|   |If you do not want property defaults to be applied you can set `spring.devtools.add-properties` to `false` in your `application.properties`.|
|---|--------------------------------------------------------------------------------------------------------------------------------------------|

|   |For a complete list of the properties that are applied by the devtools, see [DevToolsPropertyDefaultsPostProcessor](https://github.com/spring-projects/spring-boot/tree/v2.6.4/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsPropertyDefaultsPostProcessor.java).|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

### 8.3. Automatic Restart

Applications that use `spring-boot-devtools` automatically restart whenever files on the classpath change.
This can be a useful feature when working in an IDE, as it gives a very fast feedback loop for code changes.
By default, any entry on the classpath that points to a directory is monitored for changes.
Note that certain resources, such as static assets and view templates, [do not need to restart the application](#using.devtools.restart.excluding-resources).

Triggering a restart

As DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath.
The way in which you cause the classpath to be updated depends on the IDE that you are using:

* In Eclipse, saving a modified file causes the classpath to be updated and triggers a restart.

* In IntelliJ IDEA, building the project (`Build +→+ Build Project`) has the same effect.

* If using a build plugin, running `mvn compile` for Maven or `gradle build` for Gradle will trigger a restart.

|   |If you are restarting with Maven or Gradle using the build plugin you must leave the `forking` set to `enabled`.<br/>If you disable forking, the isolated application classloader used by devtools will not be created and restarts will not operate properly.|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

|   |Automatic restart works very well when used with LiveReload.[See the LiveReload section](#using.devtools.livereload) for details.<br/>If you use JRebel, automatic restarts are disabled in favor of dynamic class reloading.<br/>Other devtools features (such as LiveReload and property overrides) can still be used.|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

|   |DevTools relies on the application context’s shutdown hook to close it during a restart.<br/>It does not work correctly if you have disabled the shutdown hook (`SpringApplication.setRegisterShutdownHook(false)`).|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

|   |DevTools needs to customize the `ResourceLoader` used by the `ApplicationContext`.<br/>If your application provides one already, it is going to be wrapped.<br/>Direct override of the `getResource` method on the `ApplicationContext` is not supported.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

|   |Automatic restart is not supported when using AspectJ weaving.|
|---|--------------------------------------------------------------|

Restart vs Reload

The restart technology provided by Spring Boot works by using two classloaders.
Classes that do not change (for example, those from third-party jars) are loaded into a *base* classloader.
Classes that you are actively developing are loaded into a *restart* classloader.
When the application is restarted, the *restart* classloader is thrown away and a new one is created.
This approach means that application restarts are typically much faster than “cold starts”, since the *base* classloader is already available and populated.

If you find that restarts are not quick enough for your applications or you encounter classloading issues, you could consider reloading technologies such as [JRebel](https://jrebel.com/software/jrebel/) from ZeroTurnaround.
These work by rewriting classes as they are loaded to make them more amenable to reloading.

#### 8.3.1. Logging changes in condition evaluation

By default, each time your application restarts, a report showing the condition evaluation delta is logged.
The report shows the changes to your application’s auto-configuration as you make changes such as adding or removing beans and setting configuration properties.

To disable the logging of the report, set the following property:

Properties

```
spring.devtools.restart.log-condition-evaluation-delta=false
```

Yaml

```
spring:
  devtools:
    restart:
      log-condition-evaluation-delta: false
```

#### 8.3.2. Excluding Resources

Certain resources do not necessarily need to trigger a restart when they are changed.
For example, Thymeleaf templates can be edited in-place.
By default, changing resources in `/META-INF/maven`, `/META-INF/resources`, `/resources`, `/static`, `/public`, or `/templates` does not trigger a restart but does trigger a [live reload](#using.devtools.livereload).
If you want to customize these exclusions, you can use the `spring.devtools.restart.exclude` property.
For example, to exclude only `/static` and `/public` you would set the following property:

Properties

```
spring.devtools.restart.exclude=static/**,public/**
```

Yaml

```
spring:
  devtools:
    restart:
      exclude: "static/**,public/**"
```

|   |If you want to keep those defaults and *add* additional exclusions, use the `spring.devtools.restart.additional-exclude` property instead.|
|---|------------------------------------------------------------------------------------------------------------------------------------------|

#### 8.3.3. Watching Additional Paths

You may want your application to be restarted or reloaded when you make changes to files that are not on the classpath.
To do so, use the `spring.devtools.restart.additional-paths` property to configure additional paths to watch for changes.
You can use the `spring.devtools.restart.exclude` property [described earlier](#using.devtools.restart.excluding-resources) to control whether changes beneath the additional paths trigger a full restart or a [live reload](#using.devtools.livereload).

#### 8.3.4. Disabling Restart

If you do not want to use the restart feature, you can disable it by using the `spring.devtools.restart.enabled` property.
In most cases, you can set this property in your `application.properties` (doing so still initializes the restart classloader, but it does not watch for file changes).

If you need to *completely* disable restart support (for example, because it does not work with a specific library), you need to set the `spring.devtools.restart.enabled` `System` property to `false` before calling `SpringApplication.run(…​)`, as shown in the following example:

```
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(MyApplication.class, args);
    }

}

```

#### 8.3.5. Using a Trigger File

If you work with an IDE that continuously compiles changed files, you might prefer to trigger restarts only at specific times.
To do so, you can use a “trigger file”, which is a special file that must be modified when you want to actually trigger a restart check.

|   |Any update to the file will trigger a check, but restart only actually occurs if Devtools has detected it has something to do.|
|---|------------------------------------------------------------------------------------------------------------------------------|

To use a trigger file, set the `spring.devtools.restart.trigger-file` property to the name (excluding any path) of your trigger file.
The trigger file must appear somewhere on your classpath.

For example, if you have a project with the following structure:

```
src
+- main
   +- resources
      +- .reloadtrigger
```

Then your `trigger-file` property would be:

Properties

```
spring.devtools.restart.trigger-file=.reloadtrigger
```

Yaml

```
spring:
  devtools:
    restart:
      trigger-file: ".reloadtrigger"
```

Restarts will now only happen when the `src/main/resources/.reloadtrigger` is updated.

|   |You might want to set `spring.devtools.restart.trigger-file` as a [global setting](#using.devtools.globalsettings), so that all your projects behave in the same way.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|

Some IDEs have features that save you from needing to update your trigger file manually.[Spring Tools for Eclipse](https://spring.io/tools) and [IntelliJ IDEA (Ultimate Edition)](https://www.jetbrains.com/idea/) both have such support.
With Spring Tools, you can use the “reload” button from the console view (as long as your `trigger-file` is named `.reloadtrigger`).
For IntelliJ IDEA, you can follow the [instructions in their documentation](https://www.jetbrains.com/help/idea/spring-boot.html#application-update-policies).

#### 8.3.6. Customizing the Restart Classloader

As described earlier in the [Restart vs Reload](#using.devtools.restart.restart-vs-reload) section, restart functionality is implemented by using two classloaders.
If this causes issues, you might need to customize what gets loaded by which classloader.

By default, any open project in your IDE is loaded with the “restart” classloader, and any regular `.jar` file is loaded with the “base” classloader.
The same is true if you use `mvn spring-boot:run` or `gradle bootRun`: the project containing your `@SpringBootApplication` is loaded with the “restart” classloader, and everything else with the “base” classloader.

You can instruct Spring Boot to load parts of your project with a different classloader by creating a `META-INF/spring-devtools.properties` file.
The `spring-devtools.properties` file can contain properties prefixed with `restart.exclude` and `restart.include`.
The `include` elements are items that should be pulled up into the “restart” classloader, and the `exclude` elements are items that should be pushed down into the “base” classloader.
The value of the property is a regex pattern that is applied to the classpath, as shown in the following example:

Properties

```
restart.exclude.companycommonlibs=/mycorp-common-[\\w\\d-\\.]+\\.jar
restart.include.projectcommon=/mycorp-myproj-[\\w\\d-\\.]+\\.jar
```

Yaml

```
restart:
  exclude:
    companycommonlibs: "/mycorp-common-[\\w\\d-\\.]+\\.jar"
  include:
    projectcommon: "/mycorp-myproj-[\\w\\d-\\.]+\\.jar"
```

|   |All property keys must be unique.<br/>As long as a property starts with `restart.include.` or `restart.exclude.` it is considered.|
|---|----------------------------------------------------------------------------------------------------------------------------------|

|   |All `META-INF/spring-devtools.properties` from the classpath are loaded.<br/>You can package files inside your project, or in the libraries that the project consumes.|
|---|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|

#### 8.3.7. Known Limitations

Restart functionality does not work well with objects that are deserialized by using a standard `ObjectInputStream`.
If you need to deserialize data, you may need to use Spring’s `ConfigurableObjectInputStream` in combination with `Thread.currentThread().getContextClassLoader()`.

Unfortunately, several third-party libraries deserialize without considering the context classloader.
If you find such a problem, you need to request a fix with the original authors.

### 8.4. LiveReload

The `spring-boot-devtools` module includes an embedded LiveReload server that can be used to trigger a browser refresh when a resource is changed.
LiveReload browser extensions are freely available for Chrome, Firefox and Safari from [livereload.com](http://livereload.com/extensions/).

If you do not want to start the LiveReload server when your application runs, you can set the `spring.devtools.livereload.enabled` property to `false`.

|   |You can only run one LiveReload server at a time.<br/>Before starting your application, ensure that no other LiveReload servers are running.<br/>If you start multiple applications from your IDE, only the first has LiveReload support.|
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

|   |To trigger LiveReload when a file changes, [Automatic Restart](#using.devtools.restart) must be enabled.|
|---|--------------------------------------------------------------------------------------------------------|

### 8.5. Global Settings

You can configure global devtools settings by adding any of the following files to the `$HOME/.config/spring-boot` directory:

1. `spring-boot-devtools.properties`

2. `spring-boot-devtools.yaml`

3. `spring-boot-devtools.yml`

Any properties added to these files apply to *all* Spring Boot applications on your machine that use devtools.
For example, to configure restart to always use a [trigger file](#using.devtools.restart.triggerfile), you would add the following property to your `spring-boot-devtools` file:

Properties

```
spring.devtools.restart.trigger-file=.reloadtrigger
```

Yaml

```
spring:
  devtools:
    restart:
      trigger-file: ".reloadtrigger"
```

By default, `$HOME` is the user’s home directory.
To customize this location, set the `SPRING_DEVTOOLS_HOME` environment variable or the `spring.devtools.home` system property.

|   |If devtools configuration files are not found in `$HOME/.config/spring-boot`, the root of the `$HOME` directory is searched for the presence of a `.spring-boot-devtools.properties` file.<br/>This allows you to share the devtools global configuration with applications that are on an older version of Spring Boot that does not support the `$HOME/.config/spring-boot` location.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

|   |Profiles are not supported in devtools properties/yaml files.<br/><br/>Any profiles activated in `.spring-boot-devtools.properties` will not affect the loading of [profile-specific configuration files](features.html#features.external-config.files.profile-specific).<br/>Profile specific filenames (of the form `spring-boot-devtools-<profile>.properties`) and `spring.config.activate.on-profile` documents in both YAML and Properties files are not supported.|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

#### 8.5.1. Configuring File System Watcher

[FileSystemWatcher](https://github.com/spring-projects/spring-boot/tree/v2.6.4/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java) works by polling the class changes with a certain time interval, and then waiting for a predefined quiet period to make sure there are no more changes.
Since Spring Boot relies entirely on the IDE to compile and copy files into the location from where Spring Boot can read them, you might find that there are times when certain changes are not reflected when devtools restarts the application.
If you observe such problems constantly, try increasing the `spring.devtools.restart.poll-interval` and `spring.devtools.restart.quiet-period` parameters to the values that fit your development environment:

Properties

```
spring.devtools.restart.poll-interval=2s
spring.devtools.restart.quiet-period=1s
```

Yaml

```
spring:
  devtools:
    restart:
      poll-interval: "2s"
      quiet-period: "1s"
```

The monitored classpath directories are now polled every 2 seconds for changes, and a 1 second quiet period is maintained to make sure there are no additional class changes.

### 8.6. Remote Applications

The Spring Boot developer tools are not limited to local development.
You can also use several features when running applications remotely.
Remote support is opt-in as enabling it can be a security risk.
It should only be enabled when running on a trusted network or when secured with SSL.
If neither of these options is available to you, you should not use DevTools' remote support.
You should never enable support on a production deployment.

To enable it, you need to make sure that `devtools` is included in the repackaged archive, as shown in the following listing:

```
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludeDevtools>false</excludeDevtools>
            </configuration>
        </plugin>
    </plugins>
</build>
```

Then you need to set the `spring.devtools.remote.secret` property.
Like any important password or secret, the value should be unique and strong such that it cannot be guessed or brute-forced.

Remote devtools support is provided in two parts: a server-side endpoint that accepts connections and a client application that you run in your IDE.
The server component is automatically enabled when the `spring.devtools.remote.secret` property is set.
The client component must be launched manually.

|   |Remote devtools is not supported for Spring WebFlux applications.|
|---|-----------------------------------------------------------------|

#### 8.6.1. Running the Remote Client Application

The remote client application is designed to be run from within your IDE.
You need to run `org.springframework.boot.devtools.RemoteSpringApplication` with the same classpath as the remote project that you connect to.
The application’s single required argument is the remote URL to which it connects.

For example, if you are using Eclipse or Spring Tools and you have a project named `my-app` that you have deployed to Cloud Foundry, you would do the following:

* Select `Run Configurations…​` from the `Run` menu.

* Create a new `Java Application` “launch configuration”.

* Browse for the `my-app` project.

* Use `org.springframework.boot.devtools.RemoteSpringApplication` as the main class.

* Add `https://myapp.cfapps.io` to the `Program arguments` (or whatever your remote URL is).

A running remote client might resemble the following listing:

```
  .   ____          _                                              __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _          ___               _      \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` |        | _ \___ _ __  ___| |_ ___ \ \ \ \
 \\/  ___)| |_)| | | | | || (_| []::::::[]   / -_) '  \/ _ \  _/ -_) ) ) ) )
  '  |____| .__|_| |_|_| |_\__, |        |_|_\___|_|_|_\___/\__\___|/ / / /
 =========|_|==============|___/===================================/_/_/_/
 :: Spring Boot Remote :: 2.6.4

2015-06-10 18:25:06.632  INFO 14938 --- [           main] o.s.b.devtools.RemoteSpringApplication   : Starting RemoteSpringApplication on pwmbp with PID 14938 (/Users/pwebb/projects/spring-boot/code/spring-boot-project/spring-boot-devtools/target/classes started by pwebb in /Users/pwebb/projects/spring-boot/code)
2015-06-10 18:25:06.671  INFO 14938 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]2a17b7b6: startup date [Wed Jun 10 18:25:06 PDT 2015]; root of context hierarchy
2015-06-10 18:25:07.043  WARN 14938 --- [           main] o.s.b.d.r.c.RemoteClientConfiguration    : The connection to http://localhost:8080 is insecure. You should use a URL starting with 'https://'.
2015-06-10 18:25:07.074  INFO 14938 --- [           main] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2015-06-10 18:25:07.130  INFO 14938 --- [           main] o.s.b.devtools.RemoteSpringApplication   : Started RemoteSpringApplication in 0.74 seconds (JVM running for 1.105)
```

|   |Because the remote client is using the same classpath as the real application it can directly read application properties.<br/>This is how the `spring.devtools.remote.secret` property is read and passed to the server for authentication.|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

|   |It is always advisable to use `https://` as the connection protocol, so that traffic is encrypted and passwords cannot be intercepted.|
|---|--------------------------------------------------------------------------------------------------------------------------------------|

|   |If you need to use a proxy to access the remote application, configure the `spring.devtools.remote.proxy.host` and `spring.devtools.remote.proxy.port` properties.|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------|

#### 8.6.2. Remote Update

The remote client monitors your application classpath for changes in the same way as the [local restart](#using.devtools.restart).
Any updated resource is pushed to the remote application and (*if required*) triggers a restart.
This can be helpful if you iterate on a feature that uses a cloud service that you do not have locally.
Generally, remote updates and restarts are much quicker than a full rebuild and deploy cycle.

On a slower development environment, it may happen that the quiet period is not enough, and the changes in the classes may be split into batches.
The server is restarted after the first batch of class changes is uploaded.
The next batch can’t be sent to the application, since the server is restarting.

This is typically manifested by a warning in the `RemoteSpringApplication` logs about failing to upload some of the classes, and a consequent retry.
But it may also lead to application code inconsistency and failure to restart after the first batch of changes is uploaded.
If you observe such problems constantly, try increasing the `spring.devtools.restart.poll-interval` and `spring.devtools.restart.quiet-period` parameters to the values that fit your development environment.
See the [Configuring File System Watcher](#using.devtools.globalsettings.configuring-file-system-watcher) section for configuring these properties.

|   |Files are only monitored when the remote client is running.<br/>If you change a file before starting the remote client, it is not pushed to the remote server.|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------|

## 9. Packaging Your Application for Production

Executable jars can be used for production deployment.
As they are self-contained, they are also ideally suited for cloud-based deployment.

For additional “production ready” features, such as health, auditing, and metric REST or JMX end-points, consider adding `spring-boot-actuator`.
See *[actuator.html](actuator.html#actuator)* for details.

## 10. What to Read Next

You should now understand how you can use Spring Boot and some best practices that you should follow.
You can now go on to learn about specific *[Spring Boot features](features.html#features)* in depth, or you could skip ahead and read about the “[production ready](actuator.html#actuator)” aspects of Spring Boot.