mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Merge branch '3.2.x'
Closes gh-40942
This commit is contained in:
commit
c479c56836
@ -44,7 +44,7 @@ When reading the code, remember the following rules of thumb:
|
|||||||
|
|
||||||
* Look for classes called `+*AutoConfiguration+` and read their sources.
|
* Look for classes called `+*AutoConfiguration+` and read their sources.
|
||||||
Pay special attention to the `+@Conditional*+` annotations to find out what features they enable and when.
|
Pay special attention to the `+@Conditional*+` annotations to find out what features they enable and when.
|
||||||
Add `--debug` to the command line or a System property `-Ddebug` to get a log on the console of all the auto-configuration decisions that were made in your app.
|
Add `--debug` to the command line or the System property `-Ddebug` to get a log on the console of all the auto-configuration decisions that were made in your app.
|
||||||
In a running application with actuator enabled, look at the `conditions` endpoint (`/actuator/conditions` or the JMX equivalent) for the same information.
|
In a running application with actuator enabled, look at the `conditions` endpoint (`/actuator/conditions` or the JMX equivalent) for the same information.
|
||||||
* Look for classes that are `@ConfigurationProperties` (such as xref:api:java/org/springframework/boot/autoconfigure/web/ServerProperties.html[`ServerProperties`]) and read from there the available external configuration options.
|
* Look for classes that are `@ConfigurationProperties` (such as xref:api:java/org/springframework/boot/autoconfigure/web/ServerProperties.html[`ServerProperties`]) and read from there the available external configuration options.
|
||||||
The `@ConfigurationProperties` annotation has a `name` attribute that acts as a prefix to external properties.
|
The `@ConfigurationProperties` annotation has a `name` attribute that acts as a prefix to external properties.
|
||||||
@ -109,5 +109,5 @@ Not all Spring applications have to be web applications (or web services).
|
|||||||
If you want to execute some code in a `main` method but also bootstrap a Spring application to set up the infrastructure to use, you can use the `SpringApplication` features of Spring Boot.
|
If you want to execute some code in a `main` method but also bootstrap a Spring application to set up the infrastructure to use, you can use the `SpringApplication` features of Spring Boot.
|
||||||
A `SpringApplication` changes its `ApplicationContext` class, depending on whether it thinks it needs a web application or not.
|
A `SpringApplication` changes its `ApplicationContext` class, depending on whether it thinks it needs a web application or not.
|
||||||
The first thing you can do to help it is to leave server-related dependencies (such as the servlet API) off the classpath.
|
The first thing you can do to help it is to leave server-related dependencies (such as the servlet API) off the classpath.
|
||||||
If you cannot do that (for example, you run two applications from the same code base) then you can explicitly call `setWebApplicationType(WebApplicationType.NONE)` on your `SpringApplication` instance or set the `applicationContextClass` property (through the Java API or with external properties).
|
If you cannot do that (for example, if you run two applications from the same code base) then you can explicitly call `setWebApplicationType(WebApplicationType.NONE)` on your `SpringApplication` instance or set the `applicationContextClass` property (through the Java API or with external properties).
|
||||||
Application code that you want to run as your business logic can be implemented as a `CommandLineRunner` and dropped into the context as a `@Bean` definition.
|
Application code that you want to run as your business logic can be implemented as a `CommandLineRunner` and dropped into the context as a `@Bean` definition.
|
||||||
|
@ -17,7 +17,7 @@ The following example shows how to define a data source in a bean:
|
|||||||
|
|
||||||
include-code::custom/MyDataSourceConfiguration[]
|
include-code::custom/MyDataSourceConfiguration[]
|
||||||
|
|
||||||
The following example shows how to define a data source by setting properties:
|
The following example shows how to define a data source by setting its properties:
|
||||||
|
|
||||||
[configprops%novalidate,yaml]
|
[configprops%novalidate,yaml]
|
||||||
----
|
----
|
||||||
@ -31,7 +31,7 @@ app:
|
|||||||
Assuming that `SomeDataSource` has regular JavaBean properties for the URL, the username, and the pool size, these settings are bound automatically before the `DataSource` is made available to other components.
|
Assuming that `SomeDataSource` has regular JavaBean properties for the URL, the username, and the pool size, these settings are bound automatically before the `DataSource` is made available to other components.
|
||||||
|
|
||||||
Spring Boot also provides a utility builder class, called `DataSourceBuilder`, that can be used to create one of the standard data sources (if it is on the classpath).
|
Spring Boot also provides a utility builder class, called `DataSourceBuilder`, that can be used to create one of the standard data sources (if it is on the classpath).
|
||||||
The builder can detect the one to use based on what is available on the classpath.
|
The builder can detect which one to use based on what is available on the classpath.
|
||||||
It also auto-detects the driver based on the JDBC URL.
|
It also auto-detects the driver based on the JDBC URL.
|
||||||
|
|
||||||
The following example shows how to create a data source by using a `DataSourceBuilder`:
|
The following example shows how to create a data source by using a `DataSourceBuilder`:
|
||||||
@ -72,7 +72,7 @@ app:
|
|||||||
You can fix that by forcing the connection pool to use and return a dedicated implementation rather than `DataSource`.
|
You can fix that by forcing the connection pool to use and return a dedicated implementation rather than `DataSource`.
|
||||||
You cannot change the implementation at runtime, but the list of options will be explicit.
|
You cannot change the implementation at runtime, but the list of options will be explicit.
|
||||||
|
|
||||||
The following example shows how create a `HikariDataSource` with `DataSourceBuilder`:
|
The following example shows how to create a `HikariDataSource` with `DataSourceBuilder`:
|
||||||
|
|
||||||
include-code::simple/MyDataSourceConfiguration[]
|
include-code::simple/MyDataSourceConfiguration[]
|
||||||
|
|
||||||
@ -110,7 +110,7 @@ See "`xref:reference:data/sql.adoc#data.sql.datasource[Configure a DataSource]`"
|
|||||||
[[howto.data-access.configure-two-datasources]]
|
[[howto.data-access.configure-two-datasources]]
|
||||||
== Configure Two DataSources
|
== Configure Two DataSources
|
||||||
|
|
||||||
If you need to configure multiple data sources, you can apply the same tricks that are described in the previous section.
|
If you need to configure multiple data sources, you can apply the same tricks that were described in the previous section.
|
||||||
You must, however, mark one of the `DataSource` instances as `@Primary`, because various auto-configurations down the road expect to be able to get one by type.
|
You must, however, mark one of the `DataSource` instances as `@Primary`, because various auto-configurations down the road expect to be able to get one by type.
|
||||||
|
|
||||||
If you create your own `DataSource`, the auto-configuration backs off.
|
If you create your own `DataSource`, the auto-configuration backs off.
|
||||||
@ -225,7 +225,7 @@ If you use other forms, such as `batchSize` or `batch-size`, Hibernate will not
|
|||||||
====
|
====
|
||||||
|
|
||||||
TIP: If you need to apply advanced customization to Hibernate properties, consider registering a `HibernatePropertiesCustomizer` bean that will be invoked prior to creating the `EntityManagerFactory`.
|
TIP: If you need to apply advanced customization to Hibernate properties, consider registering a `HibernatePropertiesCustomizer` bean that will be invoked prior to creating the `EntityManagerFactory`.
|
||||||
This takes precedence to anything that is applied by the auto-configuration.
|
This takes precedence over anything that is applied by the auto-configuration.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -311,7 +311,7 @@ It scans entities located in the same package as `Order`.
|
|||||||
It is possible to map additional JPA properties using the `app.first.jpa` namespace.
|
It is possible to map additional JPA properties using the `app.first.jpa` namespace.
|
||||||
|
|
||||||
NOTE: When you create a bean for `LocalContainerEntityManagerFactoryBean` yourself, any customization that was applied during the creation of the auto-configured `LocalContainerEntityManagerFactoryBean` is lost.
|
NOTE: When you create a bean for `LocalContainerEntityManagerFactoryBean` yourself, any customization that was applied during the creation of the auto-configured `LocalContainerEntityManagerFactoryBean` is lost.
|
||||||
For example, in case of Hibernate, any properties under the `spring.jpa.hibernate` prefix will not be automatically applied to your `LocalContainerEntityManagerFactoryBean`.
|
For example, in the case of Hibernate, any properties under the `spring.jpa.hibernate` prefix will not be automatically applied to your `LocalContainerEntityManagerFactoryBean`.
|
||||||
If you were relying on these properties for configuring things like the naming strategy or the DDL mode, you will need to explicitly configure that when creating the `LocalContainerEntityManagerFactoryBean` bean.
|
If you were relying on these properties for configuring things like the naming strategy or the DDL mode, you will need to explicitly configure that when creating the `LocalContainerEntityManagerFactoryBean` bean.
|
||||||
|
|
||||||
You should provide a similar configuration for any additional data sources for which you need JPA access.
|
You should provide a similar configuration for any additional data sources for which you need JPA access.
|
||||||
@ -379,7 +379,7 @@ If you need to specify an order, make sure it is higher than 0.
|
|||||||
|
|
||||||
If you want to configure a component that JPA uses, then you need to ensure that the component is initialized before JPA.
|
If you want to configure a component that JPA uses, then you need to ensure that the component is initialized before JPA.
|
||||||
When the component is auto-configured, Spring Boot takes care of this for you.
|
When the component is auto-configured, Spring Boot takes care of this for you.
|
||||||
For example, when Flyway is auto-configured, Hibernate is configured to depend upon Flyway so that Flyway has a chance to initialize the database before Hibernate tries to use it.
|
For example, when Flyway is auto-configured, Hibernate is configured to depend on Flyway so that Flyway has a chance to initialize the database before Hibernate tries to use it.
|
||||||
|
|
||||||
If you are configuring a component yourself, you can use an `EntityManagerFactoryDependsOnPostProcessor` subclass as a convenient way of setting up the necessary dependencies.
|
If you are configuring a component yourself, you can use an `EntityManagerFactoryDependsOnPostProcessor` subclass as a convenient way of setting up the necessary dependencies.
|
||||||
For example, if you use Hibernate Search with Elasticsearch as its index manager, any `EntityManagerFactory` beans must be configured to depend on the `elasticsearchClient` bean, as shown in the following example:
|
For example, if you use Hibernate Search with Elasticsearch as its index manager, any `EntityManagerFactory` beans must be configured to depend on the `elasticsearchClient` bean, as shown in the following example:
|
||||||
|
@ -16,7 +16,7 @@ This is possible in both Maven and Gradle.
|
|||||||
[[howto.properties-and-configuration.expand-properties.maven]]
|
[[howto.properties-and-configuration.expand-properties.maven]]
|
||||||
=== Automatic Property Expansion Using Maven
|
=== Automatic Property Expansion Using Maven
|
||||||
|
|
||||||
You can automatically expand properties from the Maven project by using resource filtering.
|
You can automatically expand properties in the Maven project by using resource filtering.
|
||||||
If you use the `spring-boot-starter-parent`, you can then refer to your Maven '`project properties`' with `@..@` placeholders, as shown in the following example:
|
If you use the `spring-boot-starter-parent`, you can then refer to your Maven '`project properties`' with `@..@` placeholders, as shown in the following example:
|
||||||
|
|
||||||
[configprops%novalidate,yaml]
|
[configprops%novalidate,yaml]
|
||||||
@ -131,7 +131,7 @@ spring:
|
|||||||
banner-mode: "console"
|
banner-mode: "console"
|
||||||
----
|
----
|
||||||
|
|
||||||
The actual application will show the banner (as overridden by configuration) and uses three sources for the `ApplicationContext`.
|
The actual application will show the banner (as overridden by configuration) and use three sources for the `ApplicationContext`.
|
||||||
The application sources are:
|
The application sources are:
|
||||||
|
|
||||||
. `MyApplication` (from the code)
|
. `MyApplication` (from the code)
|
||||||
@ -152,7 +152,7 @@ You can also provide the following System properties (or environment variables)
|
|||||||
A separate `Environment` property source is set up for this document and it can be overridden by system properties, environment variables, or the command line.
|
A separate `Environment` property source is set up for this document and it can be overridden by system properties, environment variables, or the command line.
|
||||||
|
|
||||||
No matter what you set in the environment, Spring Boot always loads `application.properties` as described above.
|
No matter what you set in the environment, Spring Boot always loads `application.properties` as described above.
|
||||||
By default, if YAML is used, then files with the '`.yaml`' and '`.yml`' extension are also added to the list.
|
By default, if YAML is used, then files with the '`.yaml`' and '`.yml`' extensions are also added to the list.
|
||||||
|
|
||||||
TIP: If you want detailed information about the files that are being loaded you can xref:reference:features/logging.adoc#features.logging.log-levels[set the logging level] of `org.springframework.boot.context.config` to `trace`.
|
TIP: If you want detailed information about the files that are being loaded you can xref:reference:features/logging.adoc#features.logging.log-levels[set the logging level] of `org.springframework.boot.context.config` to `trace`.
|
||||||
|
|
||||||
@ -174,7 +174,7 @@ TIP: If you inherit from the `spring-boot-starter-parent` POM, the default filte
|
|||||||
If you have enabled Maven filtering for the `application.properties` directly, you may want to also change the default filter token to use https://maven.apache.org/plugins/maven-resources-plugin/resources-mojo.html#delimiters[other delimiters].
|
If you have enabled Maven filtering for the `application.properties` directly, you may want to also change the default filter token to use https://maven.apache.org/plugins/maven-resources-plugin/resources-mojo.html#delimiters[other delimiters].
|
||||||
|
|
||||||
NOTE: In this specific case, the port binding works in a PaaS environment such as Heroku or Cloud Foundry.
|
NOTE: In this specific case, the port binding works in a PaaS environment such as Heroku or Cloud Foundry.
|
||||||
In those two platforms, the `PORT` environment variable is set automatically and Spring can bind to capitalized synonyms for `Environment` properties.
|
On those two platforms, the `PORT` environment variable is set automatically and Spring can bind to capitalized synonyms for `Environment` properties.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -140,7 +140,7 @@ It has some useful methods to access the default and user-enhanced message conve
|
|||||||
|
|
||||||
As in normal MVC usage, any `WebMvcConfigurer` beans that you provide can also contribute converters by overriding the `configureMessageConverters` method.
|
As in normal MVC usage, any `WebMvcConfigurer` beans that you provide can also contribute converters by overriding the `configureMessageConverters` method.
|
||||||
However, unlike with normal MVC, you can supply only additional converters that you need (because Spring Boot uses the same mechanism to contribute its defaults).
|
However, unlike with normal MVC, you can supply only additional converters that you need (because Spring Boot uses the same mechanism to contribute its defaults).
|
||||||
Finally, if you opt out of the Spring Boot default MVC configuration by providing your own `@EnableWebMvc` configuration, you can take control completely and do everything manually by using `getMessageConverters` from `WebMvcConfigurationSupport`.
|
Finally, if you opt out of the default Spring Boot MVC configuration by providing your own `@EnableWebMvc` configuration, you can take control completely and do everything manually by using `getMessageConverters` from `WebMvcConfigurationSupport`.
|
||||||
|
|
||||||
See the {code-spring-boot-autoconfigure-src}/web/servlet/WebMvcAutoConfiguration.java[`WebMvcAutoConfiguration`] source code for more details.
|
See the {code-spring-boot-autoconfigure-src}/web/servlet/WebMvcAutoConfiguration.java[`WebMvcAutoConfiguration`] source code for more details.
|
||||||
|
|
||||||
@ -158,7 +158,7 @@ The multipart support is helpful when you want to receive multipart encoded file
|
|||||||
|
|
||||||
See the {code-spring-boot-autoconfigure-src}/web/servlet/MultipartAutoConfiguration.java[`MultipartAutoConfiguration`] source for more details.
|
See the {code-spring-boot-autoconfigure-src}/web/servlet/MultipartAutoConfiguration.java[`MultipartAutoConfiguration`] source for more details.
|
||||||
|
|
||||||
NOTE: It is recommended to use the container's built-in support for multipart uploads rather than introducing an additional dependency such as Apache Commons File Upload.
|
NOTE: It is recommended to use the container's built-in support for multipart uploads rather than introduce an additional dependency such as Apache Commons File Upload.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -40,6 +40,6 @@ include-code::MySecurityConfiguration[]
|
|||||||
|
|
||||||
include-code::MyDatasourceConfiguration[]
|
include-code::MyDatasourceConfiguration[]
|
||||||
|
|
||||||
Having a single configuration class can be inefficient when beans of a certain domain need to be included in slice tests.
|
Having a single configuration class can be inefficient when beans from a certain domain need to be included in slice tests.
|
||||||
Instead, structuring the application's configuration as multiple granular classes with beans for a specific domain can enable importing them only for specific slice tests.
|
Instead, structuring the application's configuration as multiple granular classes with beans for a specific domain can enable importing them only for specific slice tests.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user