Browse Source

Upgrade to @SpringBootApplication

Greg Turnquist 9 years ago
parent
commit
68fa6e9546
3 changed files with 12 additions and 9 deletions
  1. 2
    0
      .gitignore
  2. 8
    5
      README.adoc
  3. 2
    4
      complete/src/main/java/hello/Application.java

+ 2
- 0
.gitignore View File

@@ -13,3 +13,5 @@ dependency-reduced-pom.xml
13 13
 /scratch
14 14
 .gradle
15 15
 README.html
16
+*.iml
17
+.idea

+ 8
- 5
README.adoc View File

@@ -127,11 +127,14 @@ Although it is possible to package this service as a traditional link:/understan
127 127
 include::complete/src/main/java/hello/Application.java[]
128 128
 ----
129 129
 
130
-The `main()` method defers to the http://docs.spring.io/spring-boot/docs/{spring_boot_version}/api/org/springframework/boot/SpringApplication.html[`SpringApplication`] helper class, providing `Application.class` as an argument to its `run()` method. This tells Spring to read the annotation metadata from `Application` and to manage it as a component in the link:/understanding/application-context[Spring application context].
131
-
132
-The `@ComponentScan` annotation tells Spring to search recursively through the `hello` package and its children for classes marked directly or indirectly with Spring's http://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/stereotype/Component.html[`@Component`] annotation. This directive ensures that Spring finds and registers the `GreetingController`, because it is marked with `@RestController`, which in turn is a kind of `@Component` annotation.
133
-
134
-The http://docs.spring.io/spring-boot/docs/{spring_boot_version}/api/org/springframework/boot/autoconfigure/EnableAutoConfiguration.html[`@EnableAutoConfiguration`] annotation switches on reasonable default behaviors based on the content of your classpath. For example, because the application depends on the embeddable version of Tomcat (tomcat-embed-core.jar), a Tomcat server is set up and configured with reasonable defaults on your behalf. And because the application also depends on Spring MVC (spring-webmvc.jar), a Spring MVC http://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html[`DispatcherServlet`] is configured and registered for you — no `web.xml` necessary! Auto-configuration is a powerful, flexible mechanism. See the http://docs.spring.io/spring-boot/docs/{spring_boot_version}/api/org/springframework/boot/autoconfigure/EnableAutoConfiguration.html[API documentation] for further details.
130
+`@SpringBootApplication` is a convenience annotation that adds all of the following:
131
+    
132
+- `@Configuration` tags the class as a source of bean definitions for the application context.
133
+- `@EnableAutoConfiguration` tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
134
+- Normally you would add `@EnableWebMvc` for a Spring MVC app, but Spring Boot adds it automatically when it sees **spring-webmvc** on the classpath. This flags the application as a web application and activates key behaviors such as setting up a `DispatcherServlet`.
135
+- `@ComponentScan` tells Spring to look for other components, configurations, and services in the the `hello` package, allowing it to find the `HelloController`.
136
+
137
+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.
135 138
 
136 139
 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_subhead.adoc[]
137 140
 

+ 2
- 4
complete/src/main/java/hello/Application.java View File

@@ -1,11 +1,9 @@
1 1
 package hello;
2 2
 
3
-import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4 3
 import org.springframework.boot.SpringApplication;
5
-import org.springframework.context.annotation.ComponentScan;
4
+import org.springframework.boot.autoconfigure.SpringBootApplication;
6 5
 
7
-@ComponentScan
8
-@EnableAutoConfiguration
6
+@SpringBootApplication
9 7
 public class Application {
10 8
 
11 9
     public static void main(String[] args) {