logo资料库

springboot 英文文档.pdf

第1页 / 共386页
第2页 / 共386页
第3页 / 共386页
第4页 / 共386页
第5页 / 共386页
第6页 / 共386页
第7页 / 共386页
第8页 / 共386页
资料共386页,剩余部分请下载后查看
Spring Boot Reference Guide
Table of Contents
Part I. Spring Boot Documentation
1. About the documentation
2. Getting help
3. First steps
4. Working with Spring Boot
5. Learning about Spring Boot features
6. Moving to production
7. Advanced topics
Part II. Getting started
8. Introducing Spring Boot
9. System Requirements
9.1 Servlet containers
10. Installing Spring Boot
10.1 Installation instructions for the Java developer
Maven installation
Gradle installation
10.2 Installing the Spring Boot CLI
Manual installation
Installation with SDKMAN!
OSX Homebrew installation
MacPorts installation
Command-line completion
Quick start Spring CLI example
10.3 Upgrading from an earlier version of Spring Boot
11. Developing your first Spring Boot application
11.1 Creating the POM
11.2 Adding classpath dependencies
11.3 Writing the code
The @RestController and @RequestMapping annotations
The @EnableAutoConfiguration annotation
The “main” method
11.4 Running the example
11.5 Creating an executable jar
12. What to read next
Part III. Using Spring Boot
13. Build systems
13.1 Dependency management
13.2 Maven
Inheriting the starter parent
Using Spring Boot without the parent POM
Changing the Java version
Using the Spring Boot Maven plugin
13.3 Gradle
13.4 Ant
13.5 Starters
14. Structuring your code
14.1 Using the “default” package
14.2 Locating the main application class
15. Configuration classes
15.1 Importing additional configuration classes
15.2 Importing XML configuration
16. Auto-configuration
16.1 Gradually replacing auto-configuration
16.2 Disabling specific auto-configuration
17. Spring Beans and dependency injection
18. Using the @SpringBootApplication annotation
19. Running your application
19.1 Running from an IDE
19.2 Running as a packaged application
19.3 Using the Maven plugin
19.4 Using the Gradle plugin
19.5 Hot swapping
20. Developer tools
20.1 Property defaults
20.2 Automatic restart
Excluding resources
Watching additional paths
Disabling restart
Using a trigger file
Customizing the restart classloader
Known limitations
20.3 LiveReload
20.4 Global settings
20.5 Remote applications
Running the remote client application
Remote update
Remote debug tunnel
21. Packaging your application for production
22. What to read next
Part IV. Spring Boot features
23. SpringApplication
23.1 Startup failure
23.2 Customizing the Banner
23.3 Customizing SpringApplication
23.4 Fluent builder API
23.5 Application events and listeners
23.6 Web environment
23.7 Accessing application arguments
23.8 Using the ApplicationRunner or CommandLineRunner
23.9 Application exit
23.10 Admin features
24. Externalized Configuration
24.1 Configuring random values
24.2 Accessing command line properties
24.3 Application property files
24.4 Profile-specific properties
24.5 Placeholders in properties
24.6 Using YAML instead of Properties
Loading YAML
Exposing YAML as properties in the Spring Environment
Multi-profile YAML documents
YAML shortcomings
Merging YAML lists
24.7 Type-safe Configuration Properties
Third-party configuration
Relaxed binding
Properties conversion
@ConfigurationProperties Validation
@ConfigurationProperties vs. @Value
25. Profiles
25.1 Adding active profiles
25.2 Programmatically setting profiles
25.3 Profile-specific configuration files
26. Logging
26.1 Log format
26.2 Console output
Color-coded output
26.3 File output
26.4 Log Levels
26.5 Custom log configuration
26.6 Logback extensions
Profile-specific configuration
Environment properties
27. Developing web applications
27.1 The ‘Spring Web MVC framework’
Spring MVC auto-configuration
HttpMessageConverters
Custom JSON Serializers and Deserializers
MessageCodesResolver
Static Content
Custom Favicon
ConfigurableWebBindingInitializer
Template engines
Error Handling
Custom error pages
Mapping error pages outside of Spring MVC
Error Handling on WebSphere Application Server
Spring HATEOAS
CORS support
27.2 JAX-RS and Jersey
27.3 Embedded servlet container support
Servlets, Filters, and listeners
Registering Servlets, Filters, and listeners as Spring beans
Servlet Context Initialization
Scanning for Servlets, Filters, and listeners
The EmbeddedWebApplicationContext
Customizing embedded servlet containers
Programmatic customization
Customizing ConfigurableEmbeddedServletContainer directly
JSP limitations
28. Security
28.1 OAuth2
Authorization Server
Resource Server
28.2 Token Type in User Info
28.3 Customizing the User Info RestTemplate
Client
Single Sign On
28.4 Actuator Security
29. Working with SQL databases
29.1 Configure a DataSource
Embedded Database Support
Connection to a production database
Connection to a JNDI DataSource
29.2 Using JdbcTemplate
29.3 JPA and ‘Spring Data’
Entity Classes
Spring Data JPA Repositories
Creating and dropping JPA databases
Open EntityManager in View
29.4 Using H2’s web console
Changing the H2 console’s path
Securing the H2 console
29.5 Using jOOQ
Code Generation
Using DSLContext
Customizing jOOQ
30. Working with NoSQL technologies
30.1 Redis
Connecting to Redis
30.2 MongoDB
Connecting to a MongoDB database
MongoTemplate
Spring Data MongoDB repositories
Embedded Mongo
30.3 Neo4j
Connecting to a Neo4j database
Using the embedded mode
Neo4jSession
Spring Data Neo4j repositories
Repository example
30.4 Gemfire
30.5 Solr
Connecting to Solr
Spring Data Solr repositories
30.6 Elasticsearch
Connecting to Elasticsearch using Jest
Connecting to Elasticsearch using Spring Data
Spring Data Elasticsearch repositories
30.7 Cassandra
Connecting to Cassandra
Spring Data Cassandra repositories
30.8 Couchbase
Connecting to Couchbase
Spring Data Couchbase repositories
30.9 LDAP
Connecting to an LDAP server
Spring Data LDAP repositories
Embedded in-memory LDAP server
31. Caching
31.1 Supported cache providers
Generic
JCache (JSR-107)
EhCache 2.x
Hazelcast
Infinispan
Couchbase
Redis
Caffeine
Guava (deprecated)
Simple
None
32. Messaging
32.1 JMS
ActiveMQ support
Artemis support
Using a JNDI ConnectionFactory
Sending a message
Receiving a message
32.2 AMQP
RabbitMQ support
Sending a message
Receiving a message
32.3 Apache Kafka Support
Sending a Message
Receiving a Message
Additional Kafka Properties
33. Calling REST services
33.1 RestTemplate customization
34. Validation
35. Sending email
36. Distributed Transactions with JTA
36.1 Using an Atomikos transaction manager
36.2 Using a Bitronix transaction manager
36.3 Using a Narayana transaction manager
36.4 Using a Java EE managed transaction manager
36.5 Mixing XA and non-XA JMS connections
36.6 Supporting an alternative embedded transaction manager
37. Hazelcast
38. Spring Integration
39. Spring Session
40. Monitoring and management over JMX
41. Testing
41.1 Test scope dependencies
41.2 Testing Spring applications
41.3 Testing Spring Boot applications
Detecting test configuration
Excluding test configuration
Working with random ports
Using JMX
Mocking and spying beans
Auto-configured tests
Auto-configured JSON tests
Auto-configured Spring MVC tests
Auto-configured Data JPA tests
Auto-configured JDBC tests
Auto-configured Data MongoDB tests
Auto-configured REST clients
Auto-configured Spring REST Docs tests
User configuration and slicing
Using Spock to test Spring Boot applications
41.4 Test utilities
ConfigFileApplicationContextInitializer
EnvironmentTestUtils
OutputCapture
TestRestTemplate
42. WebSockets
43. Web Services
44. Creating your own auto-configuration
44.1 Understanding auto-configured beans
44.2 Locating auto-configuration candidates
44.3 Condition annotations
Class conditions
Bean conditions
Property conditions
Resource conditions
Web application conditions
SpEL expression conditions
44.4 Creating your own starter
Naming
Autoconfigure module
Starter module
45. What to read next
Part V. Spring Boot Actuator: Production-ready features
46. Enabling production-ready features
47. Endpoints
47.1 Customizing endpoints
47.2 Hypermedia for actuator MVC endpoints
47.3 CORS support
47.4 Adding custom endpoints
47.5 Health information
47.6 Security with HealthIndicators
Auto-configured HealthIndicators
Writing custom HealthIndicators
47.7 Application information
Auto-configured InfoContributors
Custom application info information
Git commit information
Build information
Writing custom InfoContributors
48. Monitoring and management over HTTP
48.1 Accessing sensitive endpoints
48.2 Customizing the management endpoint paths
48.3 Customizing the management server port
48.4 Configuring management-specific SSL
48.5 Customizing the management server address
48.6 Disabling HTTP endpoints
48.7 HTTP health endpoint format and access restrictions
49. Monitoring and management over JMX
49.1 Customizing MBean names
49.2 Disabling JMX endpoints
49.3 Using Jolokia for JMX over HTTP
Customizing Jolokia
Disabling Jolokia
50. Monitoring and management using a remote shell (deprecated)
50.1 Connecting to the remote shell
Remote shell credentials
50.2 Extending the remote shell
Remote shell commands
Remote shell plugins
51. Loggers
51.1 Configure a Logger
52. Metrics
52.1 System metrics
52.2 DataSource metrics
52.3 Cache metrics
52.4 Tomcat session metrics
52.5 Recording your own metrics
52.6 Adding your own public metrics
52.7 Special features with Java 8
52.8 Metric writers, exporters and aggregation
Example: Export to Redis
Example: Export to Open TSDB
Example: Export to Statsd
Example: Export to JMX
52.9 Aggregating metrics from multiple sources
52.10 Dropwizard Metrics
52.11 Message channel integration
53. Auditing
54. Tracing
54.1 Custom tracing
55. Process monitoring
55.1 Extend configuration
55.2 Programmatically
56. Cloud Foundry support
56.1 Disabling extended Cloud Foundry actuator support
56.2 Cloud Foundry self signed certificates
56.3 Custom security configuration
57. What to read next
Part VI. Deploying Spring Boot applications
58. Deploying to the cloud
58.1 Cloud Foundry
Binding to services
58.2 Heroku
58.3 OpenShift
58.4 Amazon Web Services (AWS)
AWS Elastic Beanstalk
Using the Tomcat platform
Using the Java SE platform
Best practices
Uploading binaries instead of sources
Reduce costs by setting the environment type
Summary
58.5 Boxfuse and Amazon Web Services
58.6 Google Cloud
59. Installing Spring Boot applications
59.1 Supported operating systems
59.2 Unix/Linux services
Installation as an init.d service (System V)
Securing an init.d service
Installation as a systemd service
Customizing the startup script
Customizing script when it’s written
Customizing script when it runs
59.3 Microsoft Windows services
60. What to read next
Part VII. Spring Boot CLI
61. Installing the CLI
62. Using the CLI
62.1 Running applications using the CLI
Deduced “grab” dependencies
Deduced “grab” coordinates
Default import statements
Automatic main method
Custom dependency management
62.2 Testing your code
62.3 Applications with multiple source files
62.4 Packaging your application
62.5 Initialize a new project
62.6 Using the embedded shell
62.7 Adding extensions to the CLI
63. Developing application with the Groovy beans DSL
64. Configuring the CLI with settings.xml
65. What to read next
Part VIII. Build tool plugins
66. Spring Boot Maven plugin
66.1 Including the plugin
66.2 Packaging executable jar and war files
67. Spring Boot Gradle plugin
67.1 Including the plugin
67.2 Gradle dependency management
67.3 Packaging executable jar and war files
67.4 Running a project in-place
67.5 Spring Boot plugin configuration
67.6 Repackage configuration
67.7 Repackage with custom Gradle configuration
Configuration options
Available layouts
Using a custom layout
67.8 Understanding how the Gradle plugin works
67.9 Publishing artifacts to a Maven repository using Gradle
Configuring Gradle to produce a pom that inherits dependency management
Configuring Gradle to produce a pom that imports dependency management
68. Spring Boot AntLib module
68.1 Spring Boot Ant tasks
spring-boot:exejar
Examples
68.2 spring-boot:findmainclass
Examples
69. Supporting other build systems
69.1 Repackaging archives
69.2 Nested libraries
69.3 Finding a main class
69.4 Example repackage implementation
70. What to read next
Part IX. ‘How-to’ guides
71. Spring Boot application
71.1 Create your own FailureAnalyzer
71.2 Troubleshoot auto-configuration
71.3 Customize the Environment or ApplicationContext before it starts
71.4 Build an ApplicationContext hierarchy (adding a parent or root context)
71.5 Create a non-web application
72. Properties & configuration
72.1 Automatically expand properties at build time
Automatic property expansion using Maven
Automatic property expansion using Gradle
72.2 Externalize the configuration of SpringApplication
72.3 Change the location of external properties of an application
72.4 Use ‘short’ command line arguments
72.5 Use YAML for external properties
72.6 Set the active Spring profiles
72.7 Change configuration depending on the environment
72.8 Discover built-in options for external properties
73. Embedded servlet containers
73.1 Add a Servlet, Filter or Listener to an application
Add a Servlet, Filter or Listener using a Spring bean
Disable registration of a Servlet or Filter
Add Servlets, Filters, and Listeners using classpath scanning
73.2 Change the HTTP port
73.3 Use a random unassigned HTTP port
73.4 Discover the HTTP port at runtime
73.5 Configure SSL
73.6 Configure Access Logging
73.7 Use behind a front-end proxy server
Customize Tomcat’s proxy configuration
73.8 Configure Tomcat
73.9 Enable Multiple Connectors with Tomcat
73.10 Use Tomcat’s LegacyCookieProcessor
73.11 Use Jetty instead of Tomcat
73.12 Configure Jetty
73.13 Use Undertow instead of Tomcat
73.14 Configure Undertow
73.15 Enable Multiple Listeners with Undertow
73.16 Use Tomcat 7.x or 8.0
Use Tomcat 7.x or 8.0 with Maven
Use Tomcat 7.x or 8.0 with Gradle
73.17 Use Jetty 9.2
Use Jetty 9.2 with Maven
Use Jetty 9.2 with Gradle
73.18 Use Jetty 8
Use Jetty 8 with Maven
Use Jetty 8 with Gradle
73.19 Create WebSocket endpoints using @ServerEndpoint
73.20 Enable HTTP response compression
74. Spring MVC
74.1 Write a JSON REST service
74.2 Write an XML REST service
74.3 Customize the Jackson ObjectMapper
74.4 Customize the @ResponseBody rendering
74.5 Handling Multipart File Uploads
74.6 Switch off the Spring MVC DispatcherServlet
74.7 Switch off the Default MVC configuration
74.8 Customize ViewResolvers
74.9 Use Thymeleaf 3
75. Jersey
75.1 Secure Jersey endpoints with Spring Security
76. HTTP clients
76.1 Configure RestTemplate to use a proxy
77. Logging
77.1 Configure Logback for logging
Configure logback for file only output
77.2 Configure Log4j for logging
Use YAML or JSON to configure Log4j 2
78. Data Access
78.1 Configure a custom DataSource
78.2 Configure Two DataSources
78.3 Use Spring Data repositories
78.4 Separate @Entity definitions from Spring configuration
78.5 Configure JPA properties
78.6 Configure Hibernate Naming Strategy
78.7 Use a custom EntityManagerFactory
78.8 Use Two EntityManagers
78.9 Use a traditional persistence.xml
78.10 Use Spring Data JPA and Mongo repositories
78.11 Expose Spring Data repositories as REST endpoint
78.12 Configure a component that is used by JPA
78.13 Configure jOOQ with Two DataSources
79. Database initialization
79.1 Initialize a database using JPA
79.2 Initialize a database using Hibernate
79.3 Initialize a database
79.4 Initialize a Spring Batch database
79.5 Use a higher-level database migration tool
Execute Flyway database migrations on startup
Execute Liquibase database migrations on startup
80. Messaging
80.1 Disable transacted JMS session
81. Batch applications
81.1 Execute Spring Batch jobs on startup
82. Actuator
82.1 Change the HTTP port or address of the actuator endpoints
82.2 Customize the ‘whitelabel’ error page
82.3 Sanitize sensible values
82.4 Actuator and Jersey
83. Security
83.1 Switch off the Spring Boot security configuration
83.2 Change the AuthenticationManager and add user accounts
83.3 Enable HTTPS when running behind a proxy server
84. Hot swapping
84.1 Reload static content
84.2 Reload templates without restarting the container
Thymeleaf templates
FreeMarker templates
Groovy templates
84.3 Fast application restarts
84.4 Reload Java classes without restarting the container
Configuring Spring Loaded for use with Maven
Configuring Spring Loaded for use with Gradle and IntelliJ IDEA
85. Build
85.1 Generate build information
85.2 Generate git information
85.3 Customize dependency versions
85.4 Create an executable JAR with Maven
85.5 Use a Spring Boot application as a dependency
85.6 Extract specific libraries when an executable jar runs
85.7 Create a non-executable JAR with exclusions
85.8 Remote debug a Spring Boot application started with Maven
85.9 Remote debug a Spring Boot application started with Gradle
85.10 Build an executable archive from Ant without using spring-boot-antlib
85.11 How to use Java 6
Embedded servlet container compatibility
Jackson
JTA API compatibility
86. Traditional deployment
86.1 Create a deployable war file
86.2 Create a deployable war file for older servlet containers
86.3 Convert an existing application to Spring Boot
86.4 Deploying a WAR to WebLogic
86.5 Deploying a WAR in an Old (Servlet 2.5) Container
Part X. Appendices
Appendix A. Common application properties
Appendix B. Configuration meta-data
B.1 Meta-data format
Group Attributes
Property Attributes
Hint Attributes
Repeated meta-data items
B.2 Providing manual hints
Value hint
Value provider
Any
Class reference
Handle As
Logger name
Spring bean reference
Spring profile name
B.3 Generating your own meta-data using the annotation processor
Nested properties
Adding additional meta-data
Appendix C. Auto-configuration classes
C.1 From the “spring-boot-autoconfigure” module
C.2 From the “spring-boot-actuator” module
Appendix D. Test auto-configuration annotations
Appendix E. The executable jar format
E.1 Nested JARs
The executable jar file structure
The executable war file structure
E.2 Spring Boot’s “JarFile” class
Compatibility with the standard Java “JarFile”
E.3 Launching executable jars
Launcher manifest
Exploded archives
E.4 PropertiesLauncher Features
E.5 Executable jar restrictions
Zip entry compression
System ClassLoader
E.6 Alternative single jar solutions
Appendix F. Dependency versions
Spring Boot Reference Guide 1.5.18.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-2018 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.18.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 ............................................................................................. 40 19.1. Running from an IDE ........................................................................................ 40 19.2. Running as a packaged application ................................................................... 40 19.3. Using the Maven plugin .................................................................................... 40 19.4. Using the Gradle plugin .................................................................................... 41 19.5. Hot swapping ................................................................................................... 41 20. Developer tools ........................................................................................................... 42 20.1. Property defaults .............................................................................................. 42 20.2. Automatic restart .............................................................................................. 43 Excluding resources .......................................................................................... 44 Watching additional paths .................................................................................. 44 Disabling restart ................................................................................................ 44 Using a trigger file ............................................................................................ 44 Customizing the restart classloader .................................................................... 45 Known limitations .............................................................................................. 45 20.3. LiveReload ....................................................................................................... 46 20.4. Global settings ................................................................................................. 46 20.5. Remote applications ......................................................................................... 46 Running the remote client application ................................................................. 47 Remote update ................................................................................................. 48 Remote debug tunnel ........................................................................................ 48 21. Packaging your application for production ..................................................................... 50 22. What to read next ....................................................................................................... 51 IV. Spring Boot features ............................................................................................................ 52 23. SpringApplication ......................................................................................................... 53 23.1. Startup failure ................................................................................................... 53 23.2. Customizing the Banner .................................................................................... 54 23.3. Customizing SpringApplication .......................................................................... 55 23.4. Fluent builder API ............................................................................................. 55 23.5. Application events and listeners ........................................................................ 56 23.6. Web environment ............................................................................................. 57 23.7. Accessing application arguments ....................................................................... 57 23.8. Using the ApplicationRunner or CommandLineRunner ........................................ 57 23.9. Application exit ................................................................................................. 58 23.10. Admin features ............................................................................................... 58 24. Externalized Configuration ........................................................................................... 60 24.1. Configuring random values ............................................................................... 61 24.2. Accessing command line properties .................................................................. 61 24.3. Application property files ................................................................................... 62 24.4. Profile-specific properties .................................................................................. 63 24.5. Placeholders in properties ................................................................................. 64 1.5.18.RELEASE Spring Boot iii
Spring Boot Reference Guide 24.6. Using YAML instead of Properties ..................................................................... 64 Loading YAML .................................................................................................. 64 Exposing YAML as properties in the Spring Environment .................................... 65 Multi-profile YAML documents ........................................................................... 65 YAML shortcomings .......................................................................................... 66 Merging YAML lists ........................................................................................... 66 24.7. Type-safe Configuration Properties .................................................................... 67 Third-party configuration .................................................................................... 70 Relaxed binding ................................................................................................ 70 Properties conversion ........................................................................................ 71 @ConfigurationProperties Validation .................................................................. 71 @ConfigurationProperties vs. @Value ............................................................... 72 25. Profiles ....................................................................................................................... 73 25.1. Adding active profiles ....................................................................................... 73 25.2. Programmatically setting profiles ....................................................................... 73 25.3. Profile-specific configuration files ....................................................................... 74 26. Logging ....................................................................................................................... 75 26.1. Log format ....................................................................................................... 75 26.2. Console output ................................................................................................. 75 Color-coded output ............................................................................................ 76 26.3. File output ........................................................................................................ 77 26.4. Log Levels ....................................................................................................... 77 26.5. Custom log configuration .................................................................................. 78 26.6. Logback extensions .......................................................................................... 79 Profile-specific configuration .............................................................................. 80 Environment properties ...................................................................................... 80 27. Developing web applications ........................................................................................ 81 27.1. The ‘Spring Web MVC framework’ .................................................................... 81 Spring MVC auto-configuration .......................................................................... 81 HttpMessageConverters .................................................................................... 82 Custom JSON Serializers and Deserializers ....................................................... 82 MessageCodesResolver .................................................................................... 83 Static Content ................................................................................................... 83 Custom Favicon ................................................................................................ 85 ConfigurableWebBindingInitializer ...................................................................... 85 Template engines .............................................................................................. 85 Error Handling .................................................................................................. 85 Custom error pages .................................................................................. 86 Mapping error pages outside of Spring MVC .............................................. 87 Error Handling on WebSphere Application Server ....................................... 87 Spring HATEOAS .............................................................................................. 88 CORS support .................................................................................................. 88 27.2. JAX-RS and Jersey .......................................................................................... 88 27.3. Embedded servlet container support .................................................................. 89 Servlets, Filters, and listeners ............................................................................ 90 Registering Servlets, Filters, and listeners as Spring beans ......................... 90 Servlet Context Initialization ............................................................................... 90 Scanning for Servlets, Filters, and listeners ................................................ 90 The EmbeddedWebApplicationContext ............................................................... 90 Customizing embedded servlet containers .......................................................... 91 1.5.18.RELEASE Spring Boot iv
Spring Boot Reference Guide Programmatic customization ...................................................................... 91 Customizing ConfigurableEmbeddedServletContainer directly ...................... 92 JSP limitations .................................................................................................. 92 28. Security ...................................................................................................................... 93 28.1. OAuth2 ............................................................................................................ 94 Authorization Server .......................................................................................... 94 Resource Server ............................................................................................... 94 28.2. Token Type in User Info ................................................................................... 95 28.3. Customizing the User Info RestTemplate ........................................................... 95 Client ................................................................................................................ 96 Single Sign On ................................................................................................. 97 28.4. Actuator Security .............................................................................................. 98 29. Working with SQL databases ....................................................................................... 99 29.1. Configure a DataSource ................................................................................... 99 Embedded Database Support ............................................................................ 99 Connection to a production database ............................................................... 100 Connection to a JNDI DataSource ................................................................... 101 29.2. Using JdbcTemplate ....................................................................................... 101 29.3. JPA and ‘Spring Data’ .................................................................................... 102 Entity Classes ................................................................................................. 102 Spring Data JPA Repositories .......................................................................... 103 Creating and dropping JPA databases ............................................................. 104 Open EntityManager in View ........................................................................... 104 29.4. Using H2’s web console ................................................................................. 104 Changing the H2 console’s path ...................................................................... 104 Securing the H2 console ................................................................................. 105 29.5. Using jOOQ ................................................................................................... 105 Code Generation ............................................................................................. 105 Using DSLContext ........................................................................................... 105 Customizing jOOQ .......................................................................................... 106 30. Working with NoSQL technologies ............................................................................. 107 30.1. Redis ............................................................................................................. 107 Connecting to Redis ........................................................................................ 107 30.2. MongoDB ....................................................................................................... 107 Connecting to a MongoDB database ................................................................ 107 MongoTemplate .............................................................................................. 108 Spring Data MongoDB repositories .................................................................. 109 Embedded Mongo ........................................................................................... 109 30.3. Neo4j ............................................................................................................. 110 Connecting to a Neo4j database ...................................................................... 110 Using the embedded mode .............................................................................. 110 Neo4jSession .................................................................................................. 111 Spring Data Neo4j repositories ........................................................................ 111 Repository example ......................................................................................... 111 30.4. Gemfire .......................................................................................................... 111 30.5. Solr ................................................................................................................ 112 Connecting to Solr .......................................................................................... 112 Spring Data Solr repositories ........................................................................... 112 30.6. Elasticsearch .................................................................................................. 112 Connecting to Elasticsearch using Jest ............................................................ 112 1.5.18.RELEASE Spring Boot v
Spring Boot Reference Guide Connecting to Elasticsearch using Spring Data ................................................. 113 Spring Data Elasticsearch repositories ............................................................. 113 30.7. Cassandra ...................................................................................................... 114 Connecting to Cassandra ................................................................................ 114 Spring Data Cassandra repositories ................................................................. 114 30.8. Couchbase ..................................................................................................... 114 Connecting to Couchbase ................................................................................ 114 Spring Data Couchbase repositories ................................................................ 115 30.9. LDAP ............................................................................................................. 116 Connecting to an LDAP server ........................................................................ 116 Spring Data LDAP repositories ........................................................................ 116 Embedded in-memory LDAP server ................................................................. 117 31. Caching .................................................................................................................... 118 31.1. Supported cache providers ............................................................................. 119 Generic ........................................................................................................... 120 JCache (JSR-107) ........................................................................................... 120 EhCache 2.x ................................................................................................... 121 Hazelcast ........................................................................................................ 121 Infinispan ........................................................................................................ 121 Couchbase ...................................................................................................... 121 Redis .............................................................................................................. 122 Caffeine .......................................................................................................... 122 Guava (deprecated) ......................................................................................... 123 Simple ............................................................................................................ 123 None ............................................................................................................... 123 32. Messaging ................................................................................................................ 124 32.1. JMS ............................................................................................................... 124 ActiveMQ support ............................................................................................ 124 Artemis support ............................................................................................... 125 Using a JNDI ConnectionFactory ..................................................................... 125 Sending a message ........................................................................................ 125 Receiving a message ...................................................................................... 126 32.2. AMQP ............................................................................................................ 127 RabbitMQ support ........................................................................................... 127 Sending a message ........................................................................................ 127 Receiving a message ...................................................................................... 128 32.3. Apache Kafka Support .................................................................................... 129 Sending a Message ........................................................................................ 129 Receiving a Message ...................................................................................... 130 Additional Kafka Properties .............................................................................. 130 33. Calling REST services ............................................................................................... 132 33.1. RestTemplate customization ............................................................................ 132 34. Validation .................................................................................................................. 134 35. Sending email ........................................................................................................... 135 36. Distributed Transactions with JTA .............................................................................. 136 36.1. Using an Atomikos transaction manager .......................................................... 136 36.2. Using a Bitronix transaction manager .............................................................. 136 36.3. Using a Narayana transaction manager ........................................................... 137 36.4. Using a Java EE managed transaction manager .............................................. 137 36.5. Mixing XA and non-XA JMS connections ......................................................... 137 1.5.18.RELEASE Spring Boot vi
Spring Boot Reference Guide 36.6. Supporting an alternative embedded transaction manager ................................ 138 37. Hazelcast .................................................................................................................. 139 38. Spring Integration ...................................................................................................... 140 39. Spring Session .......................................................................................................... 141 40. Monitoring and management over JMX ...................................................................... 142 41. Testing ...................................................................................................................... 143 41.1. Test scope dependencies ............................................................................... 143 41.2. Testing Spring applications ............................................................................. 143 41.3. Testing Spring Boot applications ..................................................................... 144 Detecting test configuration .............................................................................. 144 Excluding test configuration ............................................................................. 145 Working with random ports .............................................................................. 146 Using JMX ...................................................................................................... 146 Mocking and spying beans .............................................................................. 146 Auto-configured tests ....................................................................................... 147 Auto-configured JSON tests ............................................................................. 148 Auto-configured Spring MVC tests ................................................................... 149 Auto-configured Data JPA tests ....................................................................... 150 Auto-configured JDBC tests ............................................................................. 151 Auto-configured Data MongoDB tests ............................................................... 152 Auto-configured REST clients .......................................................................... 153 Auto-configured Spring REST Docs tests ......................................................... 153 User configuration and slicing .......................................................................... 154 Using Spock to test Spring Boot applications .................................................... 155 41.4. Test utilities .................................................................................................... 155 ConfigFileApplicationContextInitializer ............................................................... 155 EnvironmentTestUtils ....................................................................................... 156 OutputCapture ................................................................................................. 156 TestRestTemplate ........................................................................................... 156 42. WebSockets .............................................................................................................. 158 43. Web Services ............................................................................................................ 159 44. Creating your own auto-configuration ......................................................................... 160 44.1. Understanding auto-configured beans .............................................................. 160 44.2. Locating auto-configuration candidates ............................................................ 160 44.3. Condition annotations ..................................................................................... 160 Class conditions .............................................................................................. 161 Bean conditions .............................................................................................. 161 Property conditions .......................................................................................... 162 Resource conditions ........................................................................................ 162 Web application conditions .............................................................................. 162 SpEL expression conditions ............................................................................. 162 44.4. Creating your own starter ................................................................................ 162 Naming ........................................................................................................... 162 Autoconfigure module ...................................................................................... 163 Starter module ................................................................................................ 163 45. What to read next ..................................................................................................... 164 V. Spring Boot Actuator: Production-ready features .................................................................. 165 46. Enabling production-ready features ............................................................................ 166 47. Endpoints .................................................................................................................. 167 47.1. Customizing endpoints .................................................................................... 168 1.5.18.RELEASE Spring Boot vii
Spring Boot Reference Guide 47.2. Hypermedia for actuator MVC endpoints .......................................................... 169 47.3. CORS support ................................................................................................ 169 47.4. Adding custom endpoints ................................................................................ 169 47.5. Health information .......................................................................................... 170 47.6. Security with HealthIndicators ......................................................................... 170 Auto-configured HealthIndicators ...................................................................... 170 Writing custom HealthIndicators ....................................................................... 171 47.7. Application information .................................................................................... 172 Auto-configured InfoContributors ...................................................................... 172 Custom application info information .................................................................. 172 Git commit information ..................................................................................... 173 Build information ............................................................................................. 173 Writing custom InfoContributors ....................................................................... 173 48. Monitoring and management over HTTP .................................................................... 175 48.1. Accessing sensitive endpoints ......................................................................... 175 48.2. Customizing the management endpoint paths .................................................. 176 48.3. Customizing the management server port ........................................................ 176 48.4. Configuring management-specific SSL ............................................................. 177 48.5. Customizing the management server address .................................................. 177 48.6. Disabling HTTP endpoints ............................................................................... 177 48.7. HTTP health endpoint format and access restrictions ........................................ 178 49. Monitoring and management over JMX ...................................................................... 180 49.1. Customizing MBean names ............................................................................. 180 49.2. Disabling JMX endpoints ................................................................................. 180 49.3. Using Jolokia for JMX over HTTP ................................................................... 180 Customizing Jolokia ......................................................................................... 180 Disabling Jolokia ............................................................................................. 180 50. Monitoring and management using a remote shell (deprecated) ................................... 182 50.1. Connecting to the remote shell ........................................................................ 182 Remote shell credentials ................................................................................. 183 50.2. Extending the remote shell .............................................................................. 183 Remote shell commands ................................................................................. 183 Remote shell plugins ....................................................................................... 184 51. Loggers ..................................................................................................................... 185 51.1. Configure a Logger ......................................................................................... 185 52. Metrics ...................................................................................................................... 186 52.1. System metrics ............................................................................................... 186 52.2. DataSource metrics ........................................................................................ 187 52.3. Cache metrics ................................................................................................ 187 52.4. Tomcat session metrics .................................................................................. 188 52.5. Recording your own metrics ............................................................................ 188 52.6. Adding your own public metrics ....................................................................... 188 52.7. Special features with Java 8 ........................................................................... 189 52.8. Metric writers, exporters and aggregation ......................................................... 189 Example: Export to Redis ................................................................................ 190 Example: Export to Open TSDB ...................................................................... 190 Example: Export to Statsd ............................................................................... 191 Example: Export to JMX .................................................................................. 191 52.9. Aggregating metrics from multiple sources ....................................................... 192 52.10. Dropwizard Metrics ....................................................................................... 192 1.5.18.RELEASE Spring Boot viii
分享到:
收藏