spring-boot-reference.pdf

(2018 KB) Pobierz
Spring Boot Reference Guide
1.5.7.RELEASE
Phillip Webb , Dave Syer , Josh Long , Stéphane Nicoll , Rob Winch , Andy
Wilkinson , Marcel Overdijk , Christian Dupuis , Sébastien Deleuze , Michael Simons
Copyright © 2012-2017
Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee
for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.
Spring Boot Reference Guide
Table of Contents
I. Spring Boot Documentation ...................................................................................................... 1
1. About the documentation ................................................................................................ 2
2. Getting help .................................................................................................................... 3
3. First steps ...................................................................................................................... 4
4. Working with Spring Boot ................................................................................................ 5
5. Learning about Spring Boot features ................................................................................ 6
6. Moving to production ....................................................................................................... 7
7. Advanced topics ............................................................................................................. 8
II. Getting started ........................................................................................................................ 9
8. Introducing Spring Boot ................................................................................................. 10
9. System Requirements ................................................................................................... 11
9.1. Servlet containers ............................................................................................... 11
10. Installing Spring Boot .................................................................................................. 12
10.1. Installation instructions for the Java developer ................................................... 12
Maven installation ............................................................................................. 12
Gradle installation ............................................................................................. 13
10.2. Installing the Spring Boot CLI ........................................................................... 14
Manual installation ............................................................................................ 14
Installation with SDKMAN! ................................................................................. 14
OSX Homebrew installation ............................................................................... 15
MacPorts installation ......................................................................................... 15
Command-line completion ................................................................................. 15
Quick start Spring CLI example ......................................................................... 15
10.3. Upgrading from an earlier version of Spring Boot ............................................... 16
11. Developing your first Spring Boot application ................................................................ 17
11.1. Creating the POM ............................................................................................ 17
11.2. Adding classpath dependencies ........................................................................ 18
11.3. Writing the code ............................................................................................... 18
The @RestController and @RequestMapping annotations .................................. 19
The @EnableAutoConfiguration annotation ........................................................ 19
The “main” method ........................................................................................... 19
11.4. Running the example ........................................................................................ 19
11.5. Creating an executable jar ................................................................................ 20
12. What to read next ....................................................................................................... 22
III. Using Spring Boot ................................................................................................................ 23
13. Build systems ............................................................................................................. 24
13.1. Dependency management ................................................................................ 24
13.2. Maven .............................................................................................................. 24
Inheriting the starter parent ............................................................................... 24
Using Spring Boot without the parent POM ........................................................ 25
Changing the Java version ................................................................................ 26
Using the Spring Boot Maven plugin .................................................................. 26
13.3. Gradle .............................................................................................................. 26
13.4. Ant ................................................................................................................... 27
13.5. Starters ............................................................................................................ 28
14. Structuring your code .................................................................................................. 33
14.1. Using the “default” package .............................................................................. 33
1.5.7.RELEASE
Spring Boot
ii
Spring Boot Reference Guide
14.2. Locating the main application class ................................................................... 33
15. Configuration classes .................................................................................................. 35
15.1. Importing additional configuration classes .......................................................... 35
15.2. Importing XML configuration .............................................................................. 35
16. Auto-configuration ....................................................................................................... 36
16.1. Gradually replacing auto-configuration ............................................................... 36
16.2. Disabling specific auto-configuration .................................................................. 36
17. Spring Beans and dependency injection ....................................................................... 37
18. Using the @SpringBootApplication annotation .............................................................. 38
19. Running your application ............................................................................................. 39
19.1. Running from an IDE ........................................................................................ 39
19.2. Running as a packaged application ................................................................... 39
19.3. Using the Maven plugin .................................................................................... 39
19.4. Using the Gradle plugin .................................................................................... 40
19.5. Hot swapping ................................................................................................... 40
20. Developer tools ........................................................................................................... 41
20.1. Property defaults .............................................................................................. 41
20.2. Automatic restart .............................................................................................. 42
Excluding resources .......................................................................................... 43
Watching additional paths .................................................................................. 43
Disabling restart ................................................................................................ 43
Using a trigger file ............................................................................................ 44
Customizing the restart classloader .................................................................... 44
Known limitations .............................................................................................. 44
20.3. LiveReload ....................................................................................................... 45
20.4. Global settings ................................................................................................. 45
20.5. Remote applications ......................................................................................... 45
Running the remote client application ................................................................. 46
Remote update ................................................................................................. 47
Remote debug tunnel ........................................................................................ 47
21. Packaging your application for production ..................................................................... 49
22. What to read next ....................................................................................................... 50
IV. Spring Boot features ............................................................................................................ 51
23. SpringApplication ......................................................................................................... 52
23.1. Startup failure ................................................................................................... 52
23.2. Customizing the Banner .................................................................................... 53
23.3. Customizing SpringApplication .......................................................................... 54
23.4. Fluent builder API ............................................................................................. 54
23.5. Application events and listeners ........................................................................ 55
23.6. Web environment ............................................................................................. 55
23.7. Accessing application arguments ....................................................................... 56
23.8. Using the ApplicationRunner or CommandLineRunner ........................................ 56
23.9. Application exit ................................................................................................. 57
23.10. Admin features ............................................................................................... 57
24. Externalized Configuration ........................................................................................... 59
24.1. Configuring random values ............................................................................... 60
24.2. Accessing command line properties .................................................................. 60
24.3. Application property files ................................................................................... 61
24.4. Profile-specific properties .................................................................................. 62
24.5. Placeholders in properties ................................................................................. 63
1.5.7.RELEASE
Spring Boot
iii
Spring Boot Reference Guide
24.6. Using YAML instead of Properties ..................................................................... 63
Loading YAML .................................................................................................. 63
Exposing YAML as properties in the Spring Environment .................................... 64
Multi-profile YAML documents ........................................................................... 64
YAML shortcomings .......................................................................................... 65
Merging YAML lists ........................................................................................... 65
24.7. Type-safe Configuration Properties .................................................................... 66
Third-party configuration .................................................................................... 69
Relaxed binding ................................................................................................ 69
Properties conversion ........................................................................................ 70
@ConfigurationProperties Validation .................................................................. 70
@ConfigurationProperties vs. @Value ............................................................... 71
25. Profiles ....................................................................................................................... 72
25.1. Adding active profiles ....................................................................................... 72
25.2. Programmatically setting profiles ....................................................................... 72
25.3. Profile-specific configuration files ....................................................................... 73
26. Logging ....................................................................................................................... 74
26.1. Log format ....................................................................................................... 74
26.2. Console output ................................................................................................. 74
Color-coded output ............................................................................................ 75
26.3. File output ........................................................................................................ 76
26.4. Log Levels ....................................................................................................... 76
26.5. Custom log configuration .................................................................................. 77
26.6. Logback extensions .......................................................................................... 78
Profile-specific configuration .............................................................................. 79
Environment properties ...................................................................................... 79
27. Developing web applications ........................................................................................ 80
27.1. The ‘Spring Web MVC framework’ .................................................................... 80
Spring MVC auto-configuration .......................................................................... 80
HttpMessageConverters .................................................................................... 81
Custom JSON Serializers and Deserializers ....................................................... 81
MessageCodesResolver .................................................................................... 82
Static Content ................................................................................................... 82
Custom Favicon ................................................................................................ 84
ConfigurableWebBindingInitializer ...................................................................... 84
Template engines .............................................................................................. 84
Error Handling .................................................................................................. 84
Custom error pages .................................................................................. 85
Mapping error pages outside of Spring MVC .............................................. 86
Error Handling on WebSphere Application Server ....................................... 86
Spring HATEOAS .............................................................................................. 87
CORS support .................................................................................................. 87
27.2. JAX-RS and Jersey .......................................................................................... 87
27.3. Embedded servlet container support .................................................................. 88
Servlets, Filters, and listeners ............................................................................ 89
Registering Servlets, Filters, and listeners as Spring beans ......................... 89
Servlet Context Initialization ............................................................................... 89
Scanning for Servlets, Filters, and listeners ................................................ 89
The EmbeddedWebApplicationContext ............................................................... 89
Customizing embedded servlet containers .......................................................... 90
1.5.7.RELEASE
Spring Boot
iv
Spring Boot Reference Guide
Programmatic customization ...................................................................... 90
Customizing ConfigurableEmbeddedServletContainer directly ...................... 91
JSP limitations .................................................................................................. 91
28. Security ...................................................................................................................... 92
28.1. OAuth2 ............................................................................................................ 93
Authorization Server .......................................................................................... 93
Resource Server ............................................................................................... 93
28.2. Token Type in User Info ................................................................................... 94
28.3. Customizing the User Info RestTemplate ........................................................... 94
Client ................................................................................................................ 95
Single Sign On ................................................................................................. 96
28.4. Actuator Security .............................................................................................. 97
29. Working with SQL databases ....................................................................................... 98
29.1. Configure a DataSource ................................................................................... 98
Embedded Database Support ............................................................................ 98
Connection to a production database ................................................................. 99
Connection to a JNDI DataSource ................................................................... 100
29.2. Using JdbcTemplate ....................................................................................... 100
29.3. JPA and ‘Spring Data’ .................................................................................... 101
Entity Classes ................................................................................................. 101
Spring Data JPA Repositories .......................................................................... 102
Creating and dropping JPA databases ............................................................. 103
Open EntityManager in View ........................................................................... 103
29.4. Using H2’s web console ................................................................................. 103
Changing the H2 console’s path ...................................................................... 103
Securing the H2 console ................................................................................. 104
29.5. Using jOOQ ................................................................................................... 104
Code Generation ............................................................................................. 104
Using DSLContext ........................................................................................... 104
Customizing jOOQ .......................................................................................... 105
30. Working with NoSQL technologies ............................................................................. 106
30.1. Redis ............................................................................................................. 106
Connecting to Redis ........................................................................................ 106
30.2. MongoDB ....................................................................................................... 106
Connecting to a MongoDB database ................................................................ 106
MongoTemplate .............................................................................................. 107
Spring Data MongoDB repositories .................................................................. 108
Embedded Mongo ........................................................................................... 108
30.3. Neo4j ............................................................................................................. 109
Connecting to a Neo4j database ...................................................................... 109
Using the embedded mode .............................................................................. 109
Neo4jSession .................................................................................................. 109
Spring Data Neo4j repositories ........................................................................ 110
Repository example ......................................................................................... 110
30.4. Gemfire .......................................................................................................... 110
30.5. Solr ................................................................................................................ 110
Connecting to Solr .......................................................................................... 111
Spring Data Solr repositories ........................................................................... 111
30.6. Elasticsearch .................................................................................................. 111
Connecting to Elasticsearch using Jest ............................................................ 111
1.5.7.RELEASE
Spring Boot
v
Zgłoś jeśli naruszono regulamin