|
|
@@ -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
|