Sfoglia il codice sorgente

Polish up Spring Boot based on feedback from @philwebb

Greg Turnquist 13 anni fa
parent
commit
0076446db1

+ 35
- 19
README.ftl.md Vedi File

@@ -79,7 +79,7 @@ $ mvn package && java -jar target/${project_id}-0.1.0.jar
79 79
 
80 80
 You should see some output like this:
81 81
 
82
-```sh
82
+```
83 83
 Let's inspect the beans provided by Spring Boot:
84 84
 application
85 85
 beanNameHandlerMapping
@@ -130,28 +130,42 @@ Switch from Tomcat to Jetty
130 130
 ---------------------------
131 131
 What if you prefer Jetty over Tomcat? Jetty and Tomcat are both compliant servlet containers, so it should be easy to switch. With Spring Boot, it is!
132 132
 
133
-Add this to your `build.gradle` list of dependencies:
133
+Change your `build.gradle` to exclude Tomcat then add Jetty to the list of dependencies:
134 134
 
135 135
 ```groovy
136
+    compile("org.springframework.boot:spring-boot-starter-web:0.5.0.BUILD-SNAPSHOT") {
137
+        exclude module: "spring-boot-starter-tomcat"
138
+    }
136 139
     compile("org.springframework.boot:spring-boot-starter-jetty:0.5.0.BUILD-SNAPSHOT")
137 140
 ```
138 141
 
139
-If you are using Maven, add this to your list of dependencies:
142
+If you are using Maven, the changes look like this:
140 143
 
141 144
 ```xml
142 145
         <dependency>
143 146
             <groupId>org.springframework.boot</groupId>
147
+            <artifactId>spring-boot-starter-web</artifactId>
148
+            <exclusions>
149
+                <exclusion>
150
+                    <groupId>org.springframework.boot</groupId>
151
+                    <artifactId>spring-boot-starter-tomcat</artifactId>
152
+                </exclusion>
153
+            </exclusions>
154
+        </dependency>
155
+        <dependency>
156
+            <groupId>org.springframework.boot</groupId>
144 157
             <artifactId>spring-boot-starter-jetty</artifactId>
145 158
         </dependency>
146 159
 ```
147 160
 
148
-Add multipart upload support
149
--------------------------------
150
-Suppose you want to add file upload support. To do that, update your configuration by adding a `MultipartConfigElement` to the application context.
161
+This change isn't about comparing Tomcat vs. Jetty. Instead, it demonstrates how Spring Boot reacts to what is on your classpath.
162
+
163
+Another example is [Uploading Files][gs-uploading-files]. In that guide, you can see how Spring Boot reacts to adding a `MultipartConfigElement` to your application context. Spring Boot automatically plugs that bean into the DispatcherServlet and adds some other beans needed to support file uploads.
164
+
165
+As you can see below, the code is the same as before:
151 166
 
152 167
     <@snippet path="src/main/java/hello/Application.java" prefix="complete"/>
153 168
     
154
-> **Note:** A production version of `MultipartConfigElement` would not be empty; it would specify target upload path, file size upload limits, and so forth.
155 169
 
156 170
 Re-run the application
157 171
 ----------------------
@@ -170,20 +184,21 @@ $ mvn package && java -jar target/${project_id}-0.1.0.jar
170 184
 
171 185
 Now check out the output:
172 186
 
173
-```sh
187
+```
174 188
 Let's inspect the beans provided by Spring Boot:
175 189
 application
176 190
 beanNameHandlerMapping
177 191
 defaultServletHandlerMapping
178 192
 dispatcherServlet
179 193
 embeddedServletContainerCustomizerBeanPostProcessor
194
+faviconHandlerMapping
195
+faviconRequestHandler
180 196
 handlerExceptionResolver
181 197
 helloController
198
+hiddenHttpMethodFilter
182 199
 httpRequestHandlerAdapter
183 200
 jettyEmbeddedServletContainerFactory
184 201
 messageSource
185
-multipartConfigElement
186
-multipartResolver
187 202
 mvcContentNegotiationManager
188 203
 mvcConversionService
189 204
 mvcValidator
@@ -192,8 +207,10 @@ org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
192 207
 org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration
193 208
 org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$DispatcherServletConfiguration
194 209
 org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$EmbeddedJetty
195
-org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration
196 210
 org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration
211
+org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
212
+org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter
213
+org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter$FaviconConfiguration
197 214
 org.springframework.boot.context.embedded.properties.ServerProperties
198 215
 org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
199 216
 org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
@@ -211,11 +228,9 @@ simpleControllerHandlerAdapter
211 228
 viewControllerHandlerMapping
212 229
 ```
213 230
 
214
-Little changes from the previous output, except there is no longer a `tomcatEmbeddedServletContainerFactory`. Instead, there is a new `jettyEmbeddedServletContainer`. 
231
+There is little change from the previous output, except there is no longer a `tomcatEmbeddedServletContainerFactory`. Instead, there is a new `jettyEmbeddedServletContainer`. 
215 232
 
216
-There is also the `multipartConfigElement` you added. But along with it came a `multipartResolver` [courtesy of Spring Boot](https://github.com/SpringSource/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java), a bean recommended to support file uploads with Spring MVC.
217
-
218
-Otherwise, everything is the same, as it should be. Most beans listed above provide Spring MVC's production-grade features. Simply swapping one part, the servlet container, and adding upload support shouldn't cause a system-wide ripple.
233
+Otherwise, everything is the same, as it should be. Most beans listed above provide Spring MVC's production-grade features. Simply swapping one part, the servlet container, shouldn't cause a system-wide ripple.
219 234
 
220 235
 Add consumer-grade services
221 236
 ------------------------------
@@ -250,7 +265,7 @@ $ mvn package && java -jar target/${project_id}-0.1.0.jar
250 265
 
251 266
 You will see a new set of RESTful end points added to the application. These are management services provided by Spring Boot.
252 267
 
253
-```sh
268
+```
254 269
 2013-08-01 08:03:42.592  INFO 43851 ... Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.Map<java.lang.String, java.lang.Object> org.springframework.boot.ops.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
255 270
 2013-08-01 08:03:42.592  INFO 43851 ... Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.ops.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
256 271
 2013-08-01 08:03:42.844  INFO 43851 ... Mapped URL path [/env] onto handler of type [class org.springframework.boot.ops.endpoint.EnvironmentEndpoint]
@@ -306,9 +321,9 @@ JAR support and Groovy support
306 321
 ------------------------------
307 322
 The last example showed how Spring Boot makes it easy to wire beans you may not be aware that you need. And it showed how to turn on convenient management services.
308 323
 
309
-But Spring Boot does yet more. It supports not only traditional WAR file deployments, but also makes it easy to put together executable JARs thanks to Spring Boot's loader module. The various guides demonstrate this dual support through the `spring-boot-gradle-plugin`.
324
+But Spring Boot does yet more. It supports not only traditional WAR file deployments, but also makes it easy to put together executable JARs thanks to Spring Boot's loader module. The various guides demonstrate this dual support through the `spring-boot-gradle-plugin` and `spring-boot-maven-plugin`.
310 325
 
311
-On top of that, Spring Boot also has Groovy support, allowing you to build web apps with as little as a single file.
326
+On top of that, Spring Boot also has Groovy support, allowing you to build Spring MVC web apps with as little as a single file.
312 327
 
313 328
 Create a new file called **app.groovy** and put the following code in it:
314 329
 
@@ -343,7 +358,7 @@ $ curl localhost:8080
343 358
 Hello World!
344 359
 ```
345 360
 
346
-Spring Boot dynamically adds key annotations to your code and leverages [Groovy Grapes](http://groovy.codehaus.org/Grape) to pull down needed libraries to make the app run.
361
+Spring Boot does this by dynamically adding key annotations to your code and leveraging [Groovy Grapes](http://groovy.codehaus.org/Grape) to pull down needed libraries to make the app run.
347 362
 
348 363
 Summary
349 364
 ----------------
@@ -351,3 +366,4 @@ Congratulations! You built a simple web application with Spring Boot and learned
351 366
 
352 367
 [spring-boot]: https://github.com/SpringSource/spring-boot
353 368
 [spring-boot-actuator]: https://github.com/SpringSource/spring-boot/blob/master/spring-boot-actuator/README.md
369
+[gs-uploading-files]: /guides/gs/uploading-files

+ 35
- 29
README.md Vedi File

@@ -148,7 +148,6 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
148 148
 import org.springframework.context.ApplicationContext;
149 149
 import org.springframework.context.annotation.ComponentScan;
150 150
 import org.springframework.context.annotation.Configuration;
151
-import org.springframework.web.servlet.config.annotation.EnableWebMvc;
152 151
 
153 152
 @Configuration
154 153
 @EnableAutoConfiguration
@@ -195,7 +194,7 @@ $ mvn package && java -jar target/gs-spring-boot-0.1.0.jar
195 194
 
196 195
 You should see some output like this:
197 196
 
198
-```sh
197
+```
199 198
 Let's inspect the beans provided by Spring Boot:
200 199
 application
201 200
 beanNameHandlerMapping
@@ -246,24 +245,39 @@ Switch from Tomcat to Jetty
246 245
 ---------------------------
247 246
 What if you prefer Jetty over Tomcat? Jetty and Tomcat are both compliant servlet containers, so it should be easy to switch. With Spring Boot, it is!
248 247
 
249
-Add this to your `build.gradle` list of dependencies:
248
+Change your `build.gradle` to exclude Tomcat then add Jetty to the list of dependencies:
250 249
 
251 250
 ```groovy
251
+    compile("org.springframework.boot:spring-boot-starter-web:0.5.0.BUILD-SNAPSHOT") {
252
+        exclude module: "spring-boot-starter-tomcat"
253
+    }
252 254
     compile("org.springframework.boot:spring-boot-starter-jetty:0.5.0.BUILD-SNAPSHOT")
253 255
 ```
254 256
 
255
-If you are using Maven, add this to your list of dependencies:
257
+If you are using Maven, the changes look like this:
256 258
 
257 259
 ```xml
258 260
         <dependency>
259 261
             <groupId>org.springframework.boot</groupId>
262
+            <artifactId>spring-boot-starter-web</artifactId>
263
+            <exclusions>
264
+                <exclusion>
265
+                    <groupId>org.springframework.boot</groupId>
266
+                    <artifactId>spring-boot-starter-tomcat</artifactId>
267
+                </exclusion>
268
+            </exclusions>
269
+        </dependency>
270
+        <dependency>
271
+            <groupId>org.springframework.boot</groupId>
260 272
             <artifactId>spring-boot-starter-jetty</artifactId>
261 273
         </dependency>
262 274
 ```
263 275
 
264
-Add multipart upload support
265
--------------------------------
266
-Suppose you want to add file upload support. To do that, update your configuration by adding a `MultipartConfigElement` to the application context.
276
+This change isn't about comparing Tomcat vs. Jetty. Instead, it demonstrates how Spring Boot reacts to what is on your classpath.
277
+
278
+Another example is [Uploading Files][gs-uploading-files]. In that guide, you can see how Spring Boot reacts to adding a `MultipartConfigElement` to your application context. Spring Boot automatically plugs that bean into the DispatcherServlet and adds some other beans needed to support file uploads.
279
+
280
+As you can see below, the code is the same as before:
267 281
 
268 282
 `src/main/java/hello/Application.java`
269 283
 ```java
@@ -271,26 +285,17 @@ package hello;
271 285
 
272 286
 import java.util.Arrays;
273 287
 
274
-import javax.servlet.MultipartConfigElement;
275
-
276 288
 import org.springframework.boot.SpringApplication;
277 289
 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
278 290
 import org.springframework.context.ApplicationContext;
279
-import org.springframework.context.annotation.Bean;
280 291
 import org.springframework.context.annotation.ComponentScan;
281 292
 import org.springframework.context.annotation.Configuration;
282
-import org.springframework.web.servlet.config.annotation.EnableWebMvc;
283 293
 
284 294
 @Configuration
285 295
 @EnableAutoConfiguration
286 296
 @ComponentScan
287 297
 public class Application {
288 298
     
289
-    @Bean
290
-    MultipartConfigElement multipartConfigElement() {
291
-        return new MultipartConfigElement("");
292
-    }
293
-    
294 299
     public static void main(String[] args) {
295 300
         ApplicationContext ctx = SpringApplication.run(Application.class, args);
296 301
         
@@ -306,7 +311,6 @@ public class Application {
306 311
 }
307 312
 ```
308 313
     
309
-> **Note:** A production version of `MultipartConfigElement` would not be empty; it would specify target upload path, file size upload limits, and so forth.
310 314
 
311 315
 Re-run the application
312 316
 ----------------------
@@ -325,20 +329,21 @@ $ mvn package && java -jar target/gs-spring-boot-0.1.0.jar
325 329
 
326 330
 Now check out the output:
327 331
 
328
-```sh
332
+```
329 333
 Let's inspect the beans provided by Spring Boot:
330 334
 application
331 335
 beanNameHandlerMapping
332 336
 defaultServletHandlerMapping
333 337
 dispatcherServlet
334 338
 embeddedServletContainerCustomizerBeanPostProcessor
339
+faviconHandlerMapping
340
+faviconRequestHandler
335 341
 handlerExceptionResolver
336 342
 helloController
343
+hiddenHttpMethodFilter
337 344
 httpRequestHandlerAdapter
338 345
 jettyEmbeddedServletContainerFactory
339 346
 messageSource
340
-multipartConfigElement
341
-multipartResolver
342 347
 mvcContentNegotiationManager
343 348
 mvcConversionService
344 349
 mvcValidator
@@ -347,8 +352,10 @@ org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
347 352
 org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration
348 353
 org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$DispatcherServletConfiguration
349 354
 org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$EmbeddedJetty
350
-org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration
351 355
 org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration
356
+org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
357
+org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter
358
+org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter$FaviconConfiguration
352 359
 org.springframework.boot.context.embedded.properties.ServerProperties
353 360
 org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
354 361
 org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
@@ -366,11 +373,9 @@ simpleControllerHandlerAdapter
366 373
 viewControllerHandlerMapping
367 374
 ```
368 375
 
369
-Little changes from the previous output, except there is no longer a `tomcatEmbeddedServletContainerFactory`. Instead, there is a new `jettyEmbeddedServletContainer`. 
370
-
371
-There is also the `multipartConfigElement` you added. But along with it came a `multipartResolver` [courtesy of Spring Boot](https://github.com/SpringSource/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java), a bean recommended to support file uploads with Spring MVC.
376
+There is little change from the previous output, except there is no longer a `tomcatEmbeddedServletContainerFactory`. Instead, there is a new `jettyEmbeddedServletContainer`. 
372 377
 
373
-Otherwise, everything is the same, as it should be. Most beans listed above provide Spring MVC's production-grade features. Simply swapping one part, the servlet container, and adding upload support shouldn't cause a system-wide ripple.
378
+Otherwise, everything is the same, as it should be. Most beans listed above provide Spring MVC's production-grade features. Simply swapping one part, the servlet container, shouldn't cause a system-wide ripple.
374 379
 
375 380
 Add consumer-grade services
376 381
 ------------------------------
@@ -405,7 +410,7 @@ $ mvn package && java -jar target/gs-spring-boot-0.1.0.jar
405 410
 
406 411
 You will see a new set of RESTful end points added to the application. These are management services provided by Spring Boot.
407 412
 
408
-```sh
413
+```
409 414
 2013-08-01 08:03:42.592  INFO 43851 ... Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.Map<java.lang.String, java.lang.Object> org.springframework.boot.ops.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
410 415
 2013-08-01 08:03:42.592  INFO 43851 ... Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.ops.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
411 416
 2013-08-01 08:03:42.844  INFO 43851 ... Mapped URL path [/env] onto handler of type [class org.springframework.boot.ops.endpoint.EnvironmentEndpoint]
@@ -461,9 +466,9 @@ JAR support and Groovy support
461 466
 ------------------------------
462 467
 The last example showed how Spring Boot makes it easy to wire beans you may not be aware that you need. And it showed how to turn on convenient management services.
463 468
 
464
-But Spring Boot does yet more. It supports not only traditional WAR file deployments, but also makes it easy to put together executable JARs thanks to Spring Boot's loader module. The various guides demonstrate this dual support through the `spring-boot-gradle-plugin`.
469
+But Spring Boot does yet more. It supports not only traditional WAR file deployments, but also makes it easy to put together executable JARs thanks to Spring Boot's loader module. The various guides demonstrate this dual support through the `spring-boot-gradle-plugin` and `spring-boot-maven-plugin`.
465 470
 
466
-On top of that, Spring Boot also has Groovy support, allowing you to build web apps with as little as a single file.
471
+On top of that, Spring Boot also has Groovy support, allowing you to build Spring MVC web apps with as little as a single file.
467 472
 
468 473
 Create a new file called **app.groovy** and put the following code in it:
469 474
 
@@ -498,7 +503,7 @@ $ curl localhost:8080
498 503
 Hello World!
499 504
 ```
500 505
 
501
-Spring Boot dynamically adds key annotations to your code and leverages [Groovy Grapes](http://groovy.codehaus.org/Grape) to pull down needed libraries to make the app run.
506
+Spring Boot does this by dynamically adding key annotations to your code and leveraging [Groovy Grapes](http://groovy.codehaus.org/Grape) to pull down needed libraries to make the app run.
502 507
 
503 508
 Summary
504 509
 ----------------
@@ -506,3 +511,4 @@ Congratulations! You built a simple web application with Spring Boot and learned
506 511
 
507 512
 [spring-boot]: https://github.com/SpringSource/spring-boot
508 513
 [spring-boot-actuator]: https://github.com/SpringSource/spring-boot/blob/master/spring-boot-actuator/README.md
514
+[gs-uploading-files]: /guides/gs/uploading-files

+ 3
- 1
complete/build.gradle Vedi File

@@ -24,7 +24,9 @@ repositories {
24 24
 }
25 25
 
26 26
 dependencies {
27
-    compile("org.springframework.boot:spring-boot-starter-web:0.5.0.BUILD-SNAPSHOT")
27
+    compile("org.springframework.boot:spring-boot-starter-web:0.5.0.BUILD-SNAPSHOT") {
28
+        exclude module: "spring-boot-starter-tomcat"
29
+    }
28 30
     compile("org.springframework.boot:spring-boot-starter-jetty:0.5.0.BUILD-SNAPSHOT")
29 31
     compile("org.springframework.boot:spring-boot-starter-actuator:0.5.0.BUILD-SNAPSHOT")
30 32
     testCompile("junit:junit:4.11")

+ 6
- 0
complete/pom.xml Vedi File

@@ -17,6 +17,12 @@
17 17
         <dependency>
18 18
             <groupId>org.springframework.boot</groupId>
19 19
             <artifactId>spring-boot-starter-web</artifactId>
20
+            <exclusions>
21
+                <exclusion>
22
+                    <groupId>org.springframework.boot</groupId>
23
+                    <artifactId>spring-boot-starter-tomcat</artifactId>
24
+                </exclusion>
25
+	    </exclusions>
20 26
         </dependency>
21 27
         <dependency>
22 28
             <groupId>org.springframework.boot</groupId>

+ 0
- 9
complete/src/main/java/hello/Application.java Vedi File

@@ -2,26 +2,17 @@ package hello;
2 2
 
3 3
 import java.util.Arrays;
4 4
 
5
-import javax.servlet.MultipartConfigElement;
6
-
7 5
 import org.springframework.boot.SpringApplication;
8 6
 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
9 7
 import org.springframework.context.ApplicationContext;
10
-import org.springframework.context.annotation.Bean;
11 8
 import org.springframework.context.annotation.ComponentScan;
12 9
 import org.springframework.context.annotation.Configuration;
13
-import org.springframework.web.servlet.config.annotation.EnableWebMvc;
14 10
 
15 11
 @Configuration
16 12
 @EnableAutoConfiguration
17 13
 @ComponentScan
18 14
 public class Application {
19 15
     
20
-    @Bean
21
-    MultipartConfigElement multipartConfigElement() {
22
-        return new MultipartConfigElement("");
23
-    }
24
-    
25 16
     public static void main(String[] args) {
26 17
         ApplicationContext ctx = SpringApplication.run(Application.class, args);
27 18
         

+ 0
- 1
initial/src/main/java/hello/Application.java Vedi File

@@ -7,7 +7,6 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
7 7
 import org.springframework.context.ApplicationContext;
8 8
 import org.springframework.context.annotation.ComponentScan;
9 9
 import org.springframework.context.annotation.Configuration;
10
-import org.springframework.web.servlet.config.annotation.EnableWebMvc;
11 10
 
12 11
 @Configuration
13 12
 @EnableAutoConfiguration