|
|
|
|
|
|
3
|
What you'll build
|
3
|
What you'll build
|
|
4
|
-----------------
|
4
|
-----------------
|
|
5
|
|
5
|
|
|
6
|
-This guide provides an introduction to Spring Boot.
|
|
|
|
7
|
-
|
|
|
|
|
|
6
|
+This guide provides an introduction to Spring Boot by building a simple web application. This doesn't demonstrate all of its features but will help you get started with the concepts Spring Boot has to offer.
|
|
8
|
|
7
|
|
|
9
|
What you'll need
|
8
|
What you'll need
|
|
10
|
----------------
|
9
|
----------------
|
|
|
|
|
|
|
31
|
Warming up with Spring Boot
|
30
|
Warming up with Spring Boot
|
|
32
|
---------------------------
|
31
|
---------------------------
|
|
33
|
|
32
|
|
|
34
|
-What does Spring Boot provide? At the core, it offers a much faster way to build applications because it looks at what is on your classpath and makes some reasonable assumptions.
|
|
|
|
|
|
33
|
+What does Spring Boot provide? At the core, it offers a much faster way to build applications because it make reasonable assumptions such as looking at your classpath and other beans you have configured to see what you're missing.
|
|
35
|
|
34
|
|
|
36
|
For example:
|
35
|
For example:
|
|
37
|
-- Got Spring MVC? There are a handful of needed beans people almost always use in that situation. But why stop there? A Spring MVC app 99.9% of the time needs a servlet container so Spring Boot will autoconfigure embedded Tomcat.
|
|
|
|
38
|
-- Got Jetty? You probably do NOT want Tomcat, but instead embedded Jetty.
|
|
|
|
39
|
-- Got Thymeleaf? There are a few beans that must always be added to your application context. Why should you have to track that?
|
|
|
|
40
|
-- Doing multipart file uploads? The [MultipartConfigElement](http://docs.oracle.com/javaee/6/api/javax/servlet/MultipartConfigElement.html) is part of the servlet 3.0 spec and let's you define upload parameters in pure Java. Why should you have to worry about plugging one into a servlet? Define one in your application context and Spring Boot will snatch it up and plug it into Spring MVC's `DispatcherServlet`.
|
|
|
|
|
|
36
|
+- Got Spring MVC? There are a handful of needed beans people almost always use in that situation. Spring Boot adds them automatically. But why stop there? A Spring MVC app needs a servlet container so Spring Boot automatically configures embedded Tomcat.
|
|
|
|
37
|
+- Got Jetty? You probably do NOT want Tomcat, but instead embedded Jetty. Don't lift a finger; Spring Boot handles it for you.
|
|
|
|
38
|
+- Got Thymeleaf? There are a few beans that must always be added to your application context. Why should you have to deal with that? Let Spring Boot handle it for you.
|
|
|
|
39
|
+- Doing multipart file uploads? The [MultipartConfigElement](http://docs.oracle.com/javaee/6/api/javax/servlet/MultipartConfigElement.html) is part of the servlet 3.0 spec and let's you define upload parameters in pure Java. Why should you have to worry about plugging that into your servlet? Define one in your application context and Spring Boot will snatch it up and plug it into Spring MVC's battle tested `DispatcherServlet`.
|
|
41
|
|
40
|
|
|
42
|
-It doesn't stop there. These are just a few examples of the automatic configuration support Spring Boot provides. But they don't get in your way. Spring Boot may make assumptions and add a `SpringTemplateEngine` for your Thymeleaf-based application. But if you proceed to define your own `SpringTemplateEngine`, Spring Boot will step aside and prefer your choice.
|
|
|
|
|
|
41
|
+It doesn't stop there. These are just a few examples of the automatic configuration provided by Spring Boot. But it doesn't get in your way. For example, Spring Boot may make assumptions and add a `SpringTemplateEngine` for your Thymeleaf-based application, unless you've already defined one. At that point, Spring Boot automatically steps aside and lets you take control.
|
|
43
|
|
42
|
|
|
44
|
Creating a simple web application
|
43
|
Creating a simple web application
|
|
45
|
---------------------------------
|
44
|
---------------------------------
|
|
|
|
|
|
|
47
|
|
46
|
|
|
48
|
<@snippet path="src/main/java/hello/HelloController.java" prefix="initial"/>
|
47
|
<@snippet path="src/main/java/hello/HelloController.java" prefix="initial"/>
|
|
49
|
|
48
|
|
|
50
|
-The class is flagged as a `@Controller` meaing it's ready for use by Spring MVC to handle web requests. `@RequestMapping` maps `GET /` to the `index()` method. It returns pure text thanks to the `@ResponseBody` annotation.
|
|
|
|
|
|
49
|
+The class is flagged as a `@Controller` meaning it's ready for use by Spring MVC to handle web requests. `@RequestMapping` maps `/` to the `index()` method. When invoked from a browser or using curl on the command line, it returns pure text thanks to the `@ResponseBody` annotation.
|
|
51
|
|
50
|
|
|
52
|
To make it executable, create an `Application` class:
|
51
|
To make it executable, create an `Application` class:
|
|
53
|
|
52
|
|
|
54
|
<@snippet path="src/main/java/hello/Application.java" prefix="initial"/>
|
53
|
<@snippet path="src/main/java/hello/Application.java" prefix="initial"/>
|
|
55
|
|
54
|
|
|
56
|
-- `@Configuration` tags the class as the source for defining beans for the application context.
|
|
|
|
57
|
-- `@EnableAutoConfiguration` tells Spring Boot to get going and start adding beans based on classpath settings, other beans, and property settings.
|
|
|
|
58
|
-- `@EnableWebMvc` signals Spring MVC that this application is a web application and to activate key behaviors for that.
|
|
|
|
|
|
55
|
+- `@Configuration` tags the class as a source of bean definitions for the application context.
|
|
|
|
56
|
+- `@EnableAutoConfiguration` tells Spring Boot to get going and start adding beans based on classpath settings, other beans, and various property settings.
|
|
|
|
57
|
+- `@EnableWebMvc` signals Spring MVC that this application is a web application and to activate key behaviors such as setting up a `DispatcherServlet`.
|
|
59
|
- `@ComponentScanning` tells Spring to look for other components, configurations, and services in the the `hello` package, allowing it to find the `HelloController`.
|
58
|
- `@ComponentScanning` tells Spring to look for other components, configurations, and services in the the `hello` package, allowing it to find the `HelloController`.
|
|
60
|
|
59
|
|
|
61
|
-The `main()` method uses Spring Boot's `SpringApplication.run()` method to launch an application. The `run()` method returns an `ApplicationContext` and this application then retrieves all the beans that were created either by your app or were automatically added thanks to Spring Boot.
|
|
|
|
|
|
60
|
+The `main()` method uses Spring Boot's `SpringApplication.run()` method to launch an application. Did you notice that there wasn't a single line of XML? No **web.xml** file either. This web application is 100% pure Java and you didn't have to deal with configuring any plumbing or infrastructure.
|
|
|
|
61
|
+
|
|
|
|
62
|
+The `run()` method returns an `ApplicationContext` and this application then retrieves all the beans that were created either by your app or were automatically added thanks to Spring Boot. It sorts them and prints them out.
|
|
62
|
|
63
|
|
|
63
|
To run it, execute:
|
64
|
To run it, execute:
|
|
64
|
|
65
|
|
|
|
|
|
|
|
117
|
|
118
|
|
|
118
|
Switching to Jetty
|
119
|
Switching to Jetty
|
|
119
|
------------------
|
120
|
------------------
|
|
120
|
-What if you preferred Jetty over Tomcat? They're both compliant choices, so it should be darn simple to switch. And it is!
|
|
|
|
|
|
121
|
+What if you preferred Jetty over Tomcat? They're both compliant servlet containers, so it should be darn simple to switch. And it is!
|
|
121
|
|
122
|
|
|
122
|
Add this to your build file's list of dependencies:
|
123
|
Add this to your build file's list of dependencies:
|
|
123
|
|
124
|
|
|
|
|
|
|
|
134
|
|
135
|
|
|
135
|
<@snippet path="src/main/java/hello/Application.java" prefix="complete"/>
|
136
|
<@snippet path="src/main/java/hello/Application.java" prefix="complete"/>
|
|
136
|
|
137
|
|
|
137
|
-> **Note:** This `MultipartConfigElement` may not have any settings other than an empty string. A production version would specify things like target upload path, file size upload limits, etc.
|
|
|
|
|
|
138
|
+> **Note:** A production version of `MultipartConfigElement` would not be empty but instead specify things like target upload path, file size upload limits, etc.
|
|
138
|
|
139
|
|
|
139
|
Re-run the app
|
140
|
Re-run the app
|
|
140
|
--------------
|
141
|
--------------
|
|
|
|
|
|
|
188
|
viewControllerHandlerMapping
|
189
|
viewControllerHandlerMapping
|
|
189
|
```
|
190
|
```
|
|
190
|
|
191
|
|
|
191
|
-There is little change from the previous output, except there is no `tomcatEmbeddedServletContainerFactory`. Instead, there is a new `jettyEmbeddedServletContainer`.
|
|
|
|
|
|
192
|
+There is little change from the previous output, except there is no longer a `tomcatEmbeddedServletContainerFactory`. Instead, there is a new `jettyEmbeddedServletContainer`.
|
|
192
|
|
193
|
|
|
193
|
-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).
|
|
|
|
|
|
194
|
+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.
|
|
194
|
|
195
|
|
|
195
|
-Other than that, everything else appears the same, as it should be. Most the beans listed above provide Spring MVC's production-grade features. Just swapping one aspect, the container, and adding some upload support shouldn't cause a system wide ripple.
|
|
|
|
|
|
196
|
+Other than that, everything else appears the same, as it should be. Most the beans listed above provide Spring MVC's production-grade features. Just swapping one aspect, the container, and adding upload support shouldn't cause a system wide ripple.
|
|
196
|
|
197
|
|
|
197
|
That is not all
|
198
|
That is not all
|
|
198
|
---------------
|
199
|
---------------
|