Spring Boot Reference Guide

Authors

Phillip Webb , Dave Syer , Josh Long , Stéphane Nicoll

1.0.0.RC5

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.


Table of Contents

I. Spring Boot Documentation
1. Getting help
2. First steps
3. Working with Spring Boot
4. Learning about Spring Boot features
5. Moving to production
6. Advanced topics
II. Getting started
7. Introducing Spring Boot
8. Installing Spring Boot
8.1. Installation instructions for the Java developer
8.1.1. Maven installation
8.1.2. Gradle installation
8.2. Installing the Spring Boot CLI
8.2.1. Manual installation
8.2.2. Installation with GVM
8.2.3. OSX Homebrew installation
8.2.4. Command-line completion
8.2.5. Quick start Spring CLI example
9. Developing your first Spring Boot application
9.1. Creating the POM
9.2. Adding classpath dependencies
9.3. Writing the code
9.3.1. The @Controller, @RequestMapping and @ResponseBody annotations
9.3.2. The @EnableAutoConfiguration annotation
9.3.3. The “main” method
9.4. Running the example
9.5. Creating an executable jar
10. What to read next
III. Using Spring Boot
11. Build systems
11.1. Maven
11.1.1. Inheriting the starter parent
11.1.2. Using your own parent POM
11.1.3. Changing the Java version
11.1.4. Using the Spring Boot Maven plugin
11.2. Gradle
11.3. Ant
11.4. Starter POMs
12. Structuring your code
12.1. Using the “default” package
12.2. Locating the main application class
13. Configuration classes
13.1. Importing additional configuration classes
13.2. Importing XML configuration
14. Auto-configuration
14.1. Gradually replacing auto-configuration
14.2. Disabling specific auto-configuration
15. Spring Beans and dependency injection
16. Running your application
16.1. Running from an IDE
16.2. Running as a packaged application
16.3. Using the Maven plugin
16.4. Using the Gradle plugin
16.5. Hot swapping
17. Packaging your application for production
18. What to read next
IV. Spring Boot features
19. SpringApplication
19.1. Customizing SpringApplication
19.2. Fluent builder API
19.3. Application events and listeners
19.4. Web environment
19.5. Using the CommandLineRunner
19.6. Application exit
20. Externalized Configuration
20.1. Accessing command line properties
20.2. Application property files
20.3. Profile specific properties
20.4. Placeholders in properties
20.5. Using YAML instead of Properties
20.5.1. Loading YAML
20.5.2. Exposing YAML as properties in the Spring Environment
20.5.3. Multi-profile YAML documents
20.5.4. YAML shortcomings
20.6. Typesafe Configuration Properties
20.6.1. Relaxed binding
20.6.2. @ConfigurationProperties Validation
21. Profiles
21.1. Adding active profiles
21.2. Programmatically setting profiles
21.3. Profile specific configuration files
22. Logging
22.1. Log format
22.2. Console output
22.3. File output
22.4. Custom log configuration
23. Developing web applications
23.1. The “Spring Web MVC framework”
23.1.1. Spring MVC auto-configuration
23.1.2. HttpMessageConverters
23.1.3. Static Content
23.1.4. Template engines
23.2. Embedded servlet container support
23.2.1. Servlets and Filters
23.2.2. The EmbeddedWebApplicationContext
23.2.3. Customizing embedded servlet containers
Programmatic customization
Customizing ConfigurableEmbeddedServletContainerFactory directly
23.2.4. JSP limitations
24. Working with SQL databases
24.1. Configure a DataSource
24.1.1. Embedded Database Support
24.1.2. Connection to a production database
24.2. Using JdbcTemplate
24.3. JPA and “Spring Data”
24.3.1. Entity Classes
24.3.2. Spring Data JPA Repositories
24.3.3. Creating and dropping JPA databases
25. Working with NoSQL technologies
25.1. MongoDB
25.1.1. Connecting to a MongoDB database
25.1.2. MongoTemplate
25.1.3. Spring Data MongoDB repositories
26. Testing
26.1. Test scope dependencies
26.2. Testing Spring applications
26.3. Testing Spring Boot applications
26.4. Test utilities
26.4.1. ConfigFileApplicationContextInitializer
26.4.2. EnvironmentTestUtils
26.4.3. OutputCapture
26.4.4. RestTemplates
27. Developing auto-configuration and using conditions
27.1. Understanding auto-configured beans
27.2. Locating auto-configuration candidates
27.3. Condition annotations
27.3.1. Class conditions
27.3.2. Bean conditions
27.3.3. Resource conditions
27.3.4. Web Application Conditions
27.3.5. SpEL expression conditions
28. What to read next
V. Production-ready features
29. Enabling production-ready features.
30. Endpoints
30.1. Customizing endpoints
30.2. Custom health information
30.3. Custom application info information
30.3.1. Git commit information
31. Monitoring and management over HTTP
31.1. Exposing sensitive endpoints
31.2. Customizing the management server context path
31.3. Customizing the management server port
31.4. Customizing the management server address
31.5. Disabling HTTP endpoints
32. Monitoring and management over JMX
32.1. Customizing MBean names
32.2. Disabling JMX endpoints
32.3. Using Jolokia for JMX over HTTP
32.3.1. Customizing Jolokia
32.3.2. Disabling Jolokia
33. Monitoring and management using a remote shell
33.1. Connecting to the remote shell
33.1.1. Remote shell credentials
33.2. Extending the remote shell
33.2.1. Remote shell commands
33.2.2. Remote shell plugins
34. Metrics
34.1. Recording your own metrics
34.2. Metric repositories
34.3. Coda Hale Metrics
34.4. Message channel integration
35. Auditing
36. Tracing
36.1. Custom tracing
37. Error Handling
38. What to read next
VI. Deploying to the cloud
39. Cloud Foundry
39.1. Binding to services
40. Heroku
41. CloudBees
42. What to read next
VII. Spring Boot CLI
43. Installing the CLI
44. Using the CLI
44.1. Running applications using the CLI
44.1.1. Deduced “grab” dependencies
44.1.2. Default import statements
44.1.3. Automatic main method
44.2. Testing your code
44.3. Applications with multiple source files
44.4. Packaging your application
44.5. Using the embedded shell
45. Developing application with the Groovy beans DSL
46. What to read next
VIII. Build tool plugins
47. Spring Boot Maven plugin
47.1. Including the plugin
47.2. Packaging executable jar and war files
47.3. Repackage configuration
47.3.1. Required parameters
47.3.2. Optional parameters
47.4. Running applications
47.5. Run configuration
47.6. Required parameters
47.7. Optional parameters
48. Spring Boot Gradle plugin
48.1. Including the plugin
48.2. Declaring dependencies without versions
48.3. Packaging executable jar and war files
48.4. Running a project in-place
48.5. Repackage configuration
48.6. Repackage with custom Gradle configuration
48.6.1. Configuration options
48.7. Understanding how the Gradle plugin works
49. Supporting other build systems
49.1. Repackaging archives
49.2. Nested libraries
49.3. Finding a main class
49.4. Example repackage implementation
50. What to read next
IX. “How-to” guides
51. Spring Boot application
51.1. Troubleshoot auto-configuration
51.2. Customize the Environment or ApplicationContext before it starts
51.3. Build an ApplicationContext hierarchy (adding a parent or root context)
51.4. Create a non-web application
52. Properties & configuration
52.1. Externalize the configuration of SpringApplication
52.2. Change the location of external properties of an application
52.3. Use “short” command line arguments
52.4. Use YAML for external properties
52.5. Set the active Spring profiles
52.6. Change configuration depending on the environment
52.7. Discover built-in options for external properties
53. Embedded servlet containers
53.1. Add a Servlet, Filter or ServletContextListener to an application
53.2. Change the HTTP port
53.3. Use a random unassigned HTTP port
53.4. Discover the HTTP port at runtime
53.5. Configure Tomcat
53.6. Terminate SSL in Tomcat
53.7. Enable Multiple Connectors Tomcat
53.8. Use Jetty instead of Tomcat
53.9. Configure Jetty
53.10. Use Tomcat 8
53.11. Use Jetty 9
54. Spring MVC
54.1. Write a JSON REST service
54.2. Customize the Jackson ObjectMapper
54.3. Customize the @ResponseBody rendering
54.4. Switch off the Spring MVC DispatcherServlet
54.5. Switch off the Default MVC configuration
54.6. Customize ViewResolvers
55. Logging
55.1. Configure Logback for logging
55.2. Configure Log4j for logging
56. Data Access
56.1. Configure a DataSource
56.2. Use Spring Data repositories
56.3. Separate @Entity definitions from Spring configuration
56.4. Configure JPA properties
56.5. Use a traditional persistence.xml
57. Database initialization
57.1. Initialize a database using JPA
57.2. Initialize a database using Hibernate
57.3. Initialize a database using Spring JDBC
57.4. Initialize a Spring Batch database
57.5. Use a higher level database migration tool
58. Batch applications
58.1. Execute Spring Batch jobs on startup
59. Actuator
59.1. Change the HTTP port or address of the actuator endpoints
59.2. Customize the “whitelabel” error page
60. Security
60.1. Secure an application
60.2. Switch off the Spring Boot security configuration
60.3. Change the AuthenticationManager and add user accounts
60.4. Enable HTTPS
61. Hot swapping
61.1. Reload static content
61.2. Reload Thymeleaf templates without restarting the container
61.3. Reload Java classes without restarting the container
62. Build
62.1. Build an executable archive with Ant
63. Traditional deployment
63.1. Create a deployable war file
63.2. Create a deployable war file for older servlet containers
63.3. Convert an existing application to Spring Boot
X. Appendices
A. Common application properties
B. Auto-configuration classes
B.1. From the “spring-boot-autoconfigure” module
B.2. From the “spring-boot-actuator” module
C. The executable jar format
C.1. Nested JARs
C.1.1. The executable jar file structure
C.1.2. The executable war file structure
C.2. Spring Boot’s “JarFile” class
C.2.1. Compatibility with the standard Java “JarFile”
C.3. Launching executable jars
C.3.1. Launcher manifest
C.3.2. Exploded archives
C.4. PropertiesLauncher Features
C.5. Executable jar restrictions
C.5.1. Zip entry compression
C.5.2. System ClassLoader
C.6. Alternative single jar solutions