|
@@ -73,7 +73,7 @@ include::initial/src/main/java/hello/Application.java[]
|
73
|
73
|
|
74
|
74
|
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.
|
75
|
75
|
|
76
|
|
-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.
|
|
76
|
+There is also a `CommandLineRunner` method marked as a `@Bean` and this runs on start up. It 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.
|
77
|
77
|
|
78
|
78
|
== Run the application
|
79
|
79
|
To run the application, execute:
|
|
@@ -165,7 +165,7 @@ Now write a simple unit test that mocks the servlet request and response through
|
165
|
165
|
include::complete/src/test/java/hello/HelloControllerTest.java[]
|
166
|
166
|
----
|
167
|
167
|
|
168
|
|
-Note the use of the `MockServletContext` to set up an empty `WebApplicationContext` so the `HelloController` can be created in the `@Before` and passed to `MockMvcBuilders.standaloneSetup()`. An alternative would be to create the full application context using the `Application` class and `@Autowired` the `HelloController` into the test. The `MockMvc` comes from Spring Test and allows you, via a set of convenient builder classes, to send HTTP requests into the `DispatcherServlet` and make assertions about the result.
|
|
168
|
+The `MockMvc` comes from Spring Test and allows you, via a set of convenient builder classes, to send HTTP requests into the `DispatcherServlet` and make assertions about the result. Note the use of the `@AutoConfigureMockMvc` together with `@SpringBootTest` to inject a `MockMvc` instance. Having used `@SpringBootTest` we are asking for the whole application context to be created. An alternative would be to ask Spring Boot to create only the web layers of the context using the `@WebMvcTest`. Spring Boot automatically tries to locate the main application class of your application in either case, but you can override it, or narrow it down, if you want to build something different.
|
169
|
169
|
|
170
|
170
|
As well as mocking the HTTP request cycle we can also use Spring Boot to write a very simple full-stack integration test. For example, instead of (or as well as) the mock test above we could do this:
|
171
|
171
|
|
|
@@ -175,7 +175,7 @@ As well as mocking the HTTP request cycle we can also use Spring Boot to write a
|
175
|
175
|
include::complete/src/test/java/hello/HelloControllerIT.java[]
|
176
|
176
|
----
|
177
|
177
|
|
178
|
|
-The embedded server is started up on a random port by virtue of the `@IntegrationTest("${server.port=0}")` and the actual port is discovered at runtime with the `@Value("${local.server.port}")`.
|
|
178
|
+The embedded server is started up on a random port by virtue of the `webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT` and the actual port is discovered at runtime with the `@LocalServerPort`.
|
179
|
179
|
|
180
|
180
|
== Add production-grade services
|
181
|
181
|
If you are building a web site for your business, you probably need to add some management services. Spring Boot provides several out of the box with its http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#production-ready[actuator module], such as health, audits, beans, and more.
|
|
@@ -238,7 +238,7 @@ It's easy to check the health of the app.
|
238
|
238
|
|
239
|
239
|
----
|
240
|
240
|
$ curl localhost:8080/health
|
241
|
|
-{"status":"UP"}
|
|
241
|
+{"status":"UP","diskSpace":{"status":"UP","total":397635555328,"free":328389529600,"threshold":10485760}}}
|
242
|
242
|
----
|
243
|
243
|
|
244
|
244
|
You can try to invoke shutdown through curl.
|
|
@@ -295,7 +295,7 @@ $ curl localhost:8080
|
295
|
295
|
Hello World!
|
296
|
296
|
----
|
297
|
297
|
|
298
|
|
-Spring Boot does this by dynamically adding key annotations to your code and leveraging http://groovy.codehaus.org/Grape[Groovy Grape] to pull down needed libraries to make the app run.
|
|
298
|
+Spring Boot does this by dynamically adding key annotations to your code and using http://groovy.codehaus.org/Grape[Groovy Grape] to pull down libraries needed to make the app run.
|
299
|
299
|
|
300
|
300
|
== Summary
|
301
|
301
|
Congratulations! You built a simple web application with Spring Boot and learned how it can ramp up your development pace. You also turned on some handy production services.
|