1
1
[[howto]]
2
- = '`How-to`' guides
2
+ = '`How-to`' guides
3
3
4
4
[partintro]
5
5
--
@@ -12,7 +12,7 @@ http://stackoverflow.com/tags/spring-boot[stackoverflow.com] to see if someone h
12
12
already provided an answer; this is also a great place to ask new questions (please use
13
13
the `spring-boot` tag).
14
14
15
- We're also more than happy to extend this section; If you want to add a '`how-to`' you
15
+ We're also more than happy to extend this section; If you want to add a '`how-to`' you
16
16
can send us a {github-code}[pull request].
17
17
--
18
18
@@ -25,7 +25,7 @@ can send us a {github-code}[pull request].
25
25
26
26
[[howto-troubleshoot-auto-configuration]]
27
27
=== Troubleshoot auto-configuration
28
- The Spring Boot auto-configuration tries its best to '`do the right thing`' , but
28
+ The Spring Boot auto-configuration tries its best to '`do the right thing`', but
29
29
sometimes things fail and it can be hard to tell why.
30
30
31
31
There is a really useful `AutoConfigurationReport` available in any Spring Boot
@@ -80,15 +80,15 @@ The `SpringApplication` sends some special `ApplicationEvents` to the listeners
80
80
some before the context is created), and then registers the listeners for events published
81
81
by the `ApplicationContext` as well. See
82
82
'<<spring-boot-features.adoc#boot-features-application-events-and-listeners>>' in the
83
- '`Spring Boot features`' section for a complete list.
83
+ '`Spring Boot features`' section for a complete list.
84
84
85
85
86
86
87
87
[[howto-build-an-application-context-hierarchy]]
88
88
=== Build an ApplicationContext hierarchy (adding a parent or root context)
89
89
You can use the `ApplicationBuilder` class to create parent/child `ApplicationContext`
90
90
hierarchies. See '<<spring-boot-features.adoc#boot-features-fluent-builder-api>>'
91
- in the '`Spring Boot features`' section for more information.
91
+ in the '`Spring Boot features`' section for more information.
92
92
93
93
94
94
@@ -136,7 +136,7 @@ underscores (`_`) as well as dashes (`-`) in property names.
136
136
=== Change the location of external properties of an application
137
137
By default properties from different sources are added to the Spring `Environment` in a
138
138
defined order (see '<<spring-boot-features.adoc#boot-features-external-config>>' in
139
- the '`Spring Boot features`' section for the exact order).
139
+ the '`Spring Boot features`' section for the exact order).
140
140
141
141
A nice way to augment and modify this is to add `@PropertySource` annotations to your
142
142
application sources. Classes passed to the `SpringApplication` static convenience
@@ -156,7 +156,7 @@ You can also provide System properties (or environment variables) to change the
156
156
command line.
157
157
158
158
No matter what you set in the environment, Spring Boot will always load
159
- `application.properties` as described above. If YAML is used then files with the '`.yml`'
159
+ `application.properties` as described above. If YAML is used then files with the '`.yml`'
160
160
extension are also added to the list by default.
161
161
162
162
See {sc-spring-boot}/context/config/ConfigFileApplicationListener.{sc-ext}[`ConfigFileApplicationListener`]
@@ -165,7 +165,7 @@ for more detail.
165
165
166
166
167
167
[[howto-use-short-command-line-arguments]]
168
- === Use '`short`' command line arguments
168
+ === Use '`short`' command line arguments
169
169
Some people like to use (for example) `--port=9000` instead of `--server.port=9000` to
170
170
set configuration properties on the command line. You can easily enable this by using
171
171
placeholders in `application.properties`, e.g.
@@ -223,7 +223,7 @@ The example YAML above corresponds to an `application.properties` file
223
223
----
224
224
225
225
See '<<spring-boot-features.adoc#boot-features-external-config-yaml>>' in
226
- the '`Spring Boot features`' section for more information
226
+ the '`Spring Boot features`' section for more information
227
227
about YAML.
228
228
229
229
[[howto-set-active-spring-profiles]]
@@ -250,7 +250,7 @@ but not by the `SpringApplicationBuilder.profiles()` method. Thus the latter Jav
250
250
be used to augment the profiles without changing the defaults.
251
251
252
252
See '<<spring-boot-features.adoc#boot-features-profiles>>' in
253
- the '`Spring Boot features`' section for more information.
253
+ the '`Spring Boot features`' section for more information.
254
254
255
255
256
256
@@ -285,8 +285,8 @@ Example:
285
285
port: 0
286
286
----
287
287
288
- In this example the default port is 9000, but if the Spring profile '`development`' is
289
- active then the port is 9001, and if '`production`' is active then it is 0.
288
+ In this example the default port is 9000, but if the Spring profile '`development`' is
289
+ active then the port is 9001, and if '`production`' is active then it is 0.
290
290
291
291
The YAML documents are merged in the order they are encountered (so later values override
292
292
earlier ones).
@@ -366,7 +366,7 @@ that and be sure that it has initialized is to add a `@Bean` of type
366
366
out of the event when it is published.
367
367
368
368
A really useful thing to do in is to use `@IntegrationTest` to set `server.port=0`
369
- and then inject the actual ('`local`' ) port as a `@Value`. For example:
369
+ and then inject the actual ('`local`') port as a `@Value`. For example:
370
370
371
371
[source,java,indent=0,subs="verbatim,quotes,attributes"]
372
372
----
@@ -772,45 +772,45 @@ added.
772
772
773
773
`WebMvcAutoConfiguration` adds the following `ViewResolvers` to your context:
774
774
775
- * An `InternalResourceViewResolver` with bean id '`defaultViewResolver`' . This one locates
775
+ * An `InternalResourceViewResolver` with bean id '`defaultViewResolver`'. This one locates
776
776
physical resources that can be rendered using the `DefaultServlet` (e.g. static
777
777
resources and JSP pages if you are using those). It applies a prefix and a suffix to the
778
778
view name and then looks for a physical resource with that path in the servlet context
779
779
(defaults are both empty, but accessible for external configuration via
780
780
`spring.view.prefix` and `spring.view.suffix`). It can be overridden by providing a
781
781
bean of the same type.
782
- * A `BeanNameViewResolver` with id '`beanNameViewResolver`' . This is a useful member of the
782
+ * A `BeanNameViewResolver` with id '`beanNameViewResolver`'. This is a useful member of the
783
783
view resolver chain and will pick up any beans with the same name as the `View` being
784
784
resolved. It shouldn't be necessary to override or replace it.
785
- * A `ContentNegotiatingViewResolver` with id '`viewResolver`' is only added if there *are*
786
- actually beans of type `View` present. This is a '`master`' resolver, delegating to all
787
- the others and attempting to find a match to the '`Accept`' HTTP header sent by the
785
+ * A `ContentNegotiatingViewResolver` with id '`viewResolver`' is only added if there *are*
786
+ actually beans of type `View` present. This is a '`master`' resolver, delegating to all
787
+ the others and attempting to find a match to the '`Accept`' HTTP header sent by the
788
788
client. There is a useful
789
789
https://spring.io/blog/2013/06/03/content-negotiation-using-views[blog about `ContentNegotiatingViewResolver`]
790
790
that you might like to study to learn more, and also look at the source code for detail.
791
791
You can switch off the auto-configured
792
- `ContentNegotiatingViewResolver` by defining a bean named '`viewResolver`' .
792
+ `ContentNegotiatingViewResolver` by defining a bean named '`viewResolver`'.
793
793
* If you use Thymeleaf you will also have a `ThymeleafViewResolver` with id
794
- '`thymeleafViewResolver`' . It looks for resources by surrounding the view name with a
794
+ '`thymeleafViewResolver`'. It looks for resources by surrounding the view name with a
795
795
prefix and suffix (externalized to `spring.thymeleaf.prefix` and
796
- `spring.thymeleaf.suffix`, defaults '`classpath:/templates/`' and '`.html`'
796
+ `spring.thymeleaf.suffix`, defaults '`classpath:/templates/`' and '`.html`'
797
797
respectively). It can be overridden by providing a bean of the same name.
798
798
* If you use FreeMarker you will also have a `FreeMarkerViewResolver` with id
799
- '`freeMarkerViewResolver`' . It looks for resources in a loader path (externalized to
800
- `spring.freemarker.templateLoaderPath`, default '`classpath:/templates/`' ) by
799
+ '`freeMarkerViewResolver`'. It looks for resources in a loader path (externalized to
800
+ `spring.freemarker.templateLoaderPath`, default '`classpath:/templates/`') by
801
801
surrounding the view name with a prefix and suffix (externalized to `spring.freemarker.prefix`
802
- and `spring.freemarker.suffix`, with empty and '`.ftl`' defaults respectively). It can
802
+ and `spring.freemarker.suffix`, with empty and '`.ftl`' defaults respectively). It can
803
803
be overridden by providing a bean of the same name.
804
804
* If you use Groovy templates (actually if groovy-templates is on your classpath) you will
805
- also have a `Groovy TemplateViewResolver` with id '`groovyTemplateViewResolver`' . It
805
+ also have a `Groovy TemplateViewResolver` with id '`groovyTemplateViewResolver`'. It
806
806
looks for resources in a loader path by surrounding the view name with a prefix and
807
807
suffix (externalized to `spring.groovy.template.prefix` and
808
- `spring.groovy.template.suffix`, defaults '`classpath:/templates/`' and '`.tpl`'
808
+ `spring.groovy.template.suffix`, defaults '`classpath:/templates/`' and '`.tpl`'
809
809
respectively). It can be overriden by providing a bean of the same name.
810
- * If you use Velocity you will also have a `VelocityViewResolver` with id '`velocityViewResolver`' .
810
+ * If you use Velocity you will also have a `VelocityViewResolver` with id '`velocityViewResolver`'.
811
811
It looks for resources in a loader path (externalized to `spring.velocity.resourceLoaderPath`,
812
- default '`classpath:/templates/`' ) by surrounding the view name with a prefix and suffix
813
- (externalized to `spring.velocity.prefix` and `spring.velocity.suffix`, with empty and '`.vm`'
812
+ default '`classpath:/templates/`') by surrounding the view name with a prefix and suffix
813
+ (externalized to `spring.velocity.prefix` and `spring.velocity.suffix`, with empty and '`.vm`'
814
814
defaults respectively). It can be overridden by providing a bean of the same name.
815
815
816
816
Check out {sc-spring-boot-autoconfigure}/web/WebMvcAutoConfiguration.{sc-ext}[`WebMvcAutoConfiguration`],
@@ -965,7 +965,7 @@ your own, and bind it to a set of `Environment` properties e.g.
965
965
----
966
966
967
967
See '<<spring-boot-features.adoc#boot-features-configure-datasource>>' in the
968
- '`Spring Boot features`' section and the
968
+ '`Spring Boot features`' section and the
969
969
{sc-spring-boot-autoconfigure}/jdbc/DataSourceAutoConfiguration.{sc-ext}[`DataSourceAutoConfiguration`]
970
970
class for more details.
971
971
@@ -1063,7 +1063,7 @@ for more details.
1063
1063
[[howto-use-custom-entity-manager]]
1064
1064
=== Use a custom EntityManagerFactory
1065
1065
To take full control of the configuration of the `EntityManagerFactory`, you need to add
1066
- a `@Bean` named '`entityManagerFactory`' . Spring Boot auto-configuration switches off its
1066
+ a `@Bean` named '`entityManagerFactory`'. Spring Boot auto-configuration switches off its
1067
1067
entity manager based on the presence of a bean of that type.
1068
1068
1069
1069
@@ -1118,7 +1118,7 @@ might be able to use a JTA transaction manager spanning both.
1118
1118
Spring doesn't require the use of XML to configure the JPA provider, and Spring Boot
1119
1119
assumes you want to take advantage of that feature. If you prefer to use `persistence.xml`
1120
1120
then you need to define your own `@Bean` of type `LocalEntityManagerFactoryBean` (with
1121
- id '`entityManagerFactory`' , and set the persistence unit name there.
1121
+ id '`entityManagerFactory`', and set the persistence unit name there.
1122
1122
1123
1123
See
1124
1124
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java[`JpaBaseConfiguration`]
@@ -1134,7 +1134,7 @@ automatically. If they are both present on the classpath, you might have to do s
1134
1134
configuration to tell Spring Boot which one (or both) you want to create repositories for
1135
1135
you. The most explicit way to do that is to use the standard Spring Data
1136
1136
`@Enable*Repositories` and tell it the location of your `Repository` interfaces
1137
- (where '`*`' is '`Jpa`' or '`Mongo`' or both).
1137
+ (where '`*`' is '`Jpa`' or '`Mongo`' or both).
1138
1138
1139
1139
There are also flags `spring.data.*.repositories.enabled` that you can use to switch the
1140
1140
auto-configured repositories on and off in external configuration. This is useful for
@@ -1173,7 +1173,7 @@ values are `none`, `validate`, `update`, `create-drop`. Spring Boot chooses a de
1173
1173
value for you based on whether it thinks your database is embedded (default `create-drop`)
1174
1174
or not (default `none`). An embedded database is detected by looking at the `Connection`
1175
1175
type: `hsqldb`, `h2` and `derby` are embedded, the rest are not. Be careful when switching
1176
- from in-memory to a '`real`' database that you don't make assumptions about the existence of
1176
+ from in-memory to a '`real`' database that you don't make assumptions about the existence of
1177
1177
the tables and data in the new platform. You either have to set `ddl-auto` explicitly, or
1178
1178
use one of the other mechanisms to initialize the database.
1179
1179
@@ -1199,7 +1199,7 @@ to start. The script locations can be changed by setting `spring.datasource.sche
1199
1199
1200
1200
To disable the failfast you can set `spring.datasource.continueOnError=true`. This can be
1201
1201
useful once an application has matured and been deployed a few times, since the scripts
1202
- can act as '`poor man's migrations`' -- inserts that fail mean that the data is already
1202
+ can act as '`poor man's migrations`' -- inserts that fail mean that the data is already
1203
1203
there, so there would be no need to prevent the application from running, for instance.
1204
1204
1205
1205
If you want to use the `schema.sql` initialization in a JPA app (with
@@ -1236,7 +1236,7 @@ To automatically run Flyway database migrations on startup, add the
1236
1236
`org.flywaydb:flyway-core` to your classpath.
1237
1237
1238
1238
The migrations are scripts in the form `V<VERSION>__<NAME>.sql` (with `<VERSION>` an
1239
- underscore-separated version, e.g. '`1`' or '`2_1`' ). By default they live in a folder
1239
+ underscore-separated version, e.g. '`1`' or '`2_1`'). By default they live in a folder
1240
1240
`classpath:db/migration` but you can modify that using `flyway.locations` (a list). See
1241
1241
the Flyway class from flyway-core for details of available settings like schemas etc. In
1242
1242
addition Spring Boot provides a small set of properties in
@@ -1315,13 +1315,13 @@ For more detail look at the
1315
1315
{sc-spring-boot-actuator}/autoconfigure/ManagementServerProperties.{sc-ext}[`ManagementServerProperties`]
1316
1316
source code and
1317
1317
'<<production-ready-features.adoc#production-ready-customizing-management-server-port>>'
1318
- in the '`Production-ready features`' section.
1318
+ in the '`Production-ready features`' section.
1319
1319
1320
1320
1321
1321
1322
1322
[[howto-customize-the-whitelabel-error-page]]
1323
- === Customize the '`whitelabel`' error page
1324
- Spring Boot installs a '`whitelabel`' error page that you will see in browser client if
1323
+ === Customize the '`whitelabel`' error page
1324
+ Spring Boot installs a '`whitelabel`' error page that you will see in browser client if
1325
1325
you encounter a server error (machine clients consuming JSON and other media types should
1326
1326
see a sensible response with the right error code). To switch it off you can set
1327
1327
`error.whitelabel.enabled=false`, but normally in addition or alternatively to that you
@@ -1530,7 +1530,7 @@ To configure IntelliJ correctly you can use the `idea` Gradle plugin:
1530
1530
NOTE: Intellij must be configured to use the same Java version as the command line Gradle
1531
1531
task and `springloaded` *must* be included as a `buildscript` dependency.
1532
1532
1533
- You can also additionally enable '`Make Project Automatically`' inside Intellij to
1533
+ You can also additionally enable '`Make Project Automatically`' inside Intellij to
1534
1534
automatically compile your code whenever a file is saved.
1535
1535
1536
1536
@@ -1568,7 +1568,7 @@ third-party dependencies. Overriding versions may cause compatibility issues.
1568
1568
1569
1569
[[howto-create-an-executable-jar-with-maven]]
1570
1570
=== Create an executable JAR with Maven
1571
- The `spring-boot-maven-plugin` can be used to create an executable '`fat`' JAR. If you
1571
+ The `spring-boot-maven-plugin` can be used to create an executable '`fat`' JAR. If you
1572
1572
are using the `spring-boot-starter-parent` POM you can simply declare the plugin and
1573
1573
your jars will be repackaged:
1574
1574
@@ -1617,7 +1617,7 @@ If you want to use your project as a library jar for other projects to depend on
1617
1617
addition have an executable (e.g. demo) version of it, you will want to configure the
1618
1618
build in a slightly different way.
1619
1619
1620
- For Maven the normal JAR plugin and the Spring Boot plugin both have a '`classifier`'
1620
+ For Maven the normal JAR plugin and the Spring Boot plugin both have a '`classifier`'
1621
1621
configuration that you can add to create an additional JAR. Example (using the Spring
1622
1622
Boot Starter Parent to manage the plugin versions and other configuration defaults):
1623
1623
@@ -1637,7 +1637,7 @@ Boot Starter Parent to manage the plugin versions and other configuration defaul
1637
1637
----
1638
1638
1639
1639
Two jars are produced, the default one, and an executable one using the Boot plugin with
1640
- classifier '`exec`' .
1640
+ classifier '`exec`'.
1641
1641
1642
1642
For Gradle users the steps are similar. Example:
1643
1643
@@ -1658,7 +1658,7 @@ jar support which assumes that the `jruby-complete.jar` is always directly avail
1658
1658
file in its own right.
1659
1659
1660
1660
To deal with any problematic libraries, you can flag that specific nested jars should be
1661
- automatically unpacked to the '`temp folder`' when the executable jar first runs.
1661
+ automatically unpacked to the '`temp folder`' when the executable jar first runs.
1662
1662
1663
1663
For example, to indicate that JRuby should be flagged for unpack using the Maven Plugin
1664
1664
you would add the following configuration:
@@ -1815,7 +1815,7 @@ archive as normal. To make it executable:
1815
1815
. Use the appropriate launcher as a `Main-Class`, e.g. `JarLauncher` for a jar file, and
1816
1816
specify the other properties it needs as manifest entries, principally a `Start-Class`.
1817
1817
1818
- . Add the runtime dependencies in a nested '`lib`' directory (for a jar) and the
1818
+ . Add the runtime dependencies in a nested '`lib`' directory (for a jar) and the
1819
1819
`provided` (embedded container) dependencies in a nested `lib-provided` directory.
1820
1820
Remember *not* to compress the entries in the archive.
1821
1821
@@ -1868,13 +1868,13 @@ after which you can run the application with
1868
1868
Use the `SpringBootServletInitializer` base class, which is picked up by Spring's
1869
1869
Servlet 3.0 support on deployment. Add an extension of that to your project and build a
1870
1870
war file as normal. For more detail, see the
1871
- http://spring.io/guides/gs/convert-jar-to-war['`Converting a jar Project to a war`' ] guide
1871
+ http://spring.io/guides/gs/convert-jar-to-war['`Converting a jar Project to a war`'] guide
1872
1872
on the spring.io website and the sample below.
1873
1873
1874
1874
The war file can also be executable if you use the Spring Boot build tools. In that case
1875
1875
the embedded container classes (to launch Tomcat for instance) have to be added to the
1876
1876
war in a `lib-provided` directory. The tools will take care of that as long as the
1877
- dependencies are marked as '`provided`' in Maven or Gradle. Here's a Maven example
1877
+ dependencies are marked as '`provided`' in Maven or Gradle. Here's a Maven example
1878
1878
{github-code}/spring-boot-samples/spring-boot-sample-traditional/pom.xml[in the Boot Samples].
1879
1879
1880
1880
0 commit comments