1、Creating the POM
建一个标准的maven工程,生成一个pom文件
We need to start by creating a Maven pom.xml file. The pom.xml is the recipe that will be used to build your project. Open your favorite text editor and add the following:
4.0.0
feifei
jjf
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.3.2.RELEASE
org.springframework.boot
spring-boot-starter-web
2、添加依赖
首先将spring-boot-starter-parent作为被继承的工程,这样就有了默认的maven配置,然后再添加自己的依赖。
Spring Boot provides a number of “Starter POMs” that make easy to add jars to your classpath. Our sample application has already used spring-boot-starter-parent in the parent section of the POM. The spring-boot-starter-parent is a special starter that provides useful Maven defaults. It also provides a dependency-management section so that you can omit version tags for “blessed” dependencies.
Other “Starter POMs” simply provide dependencies that you are likely to need when developing a specific type of application. Since we are developing a web application, we will add a spring-boot-starter-web dependency — but before that, let’s look at what we currently have.
可以通过运行mvn dependency:tree命令来查看依赖
If you run mvn dependency:tree again, you will see that there are now a number of additional dependencies, including the Tomcat web server and Spring Boot itself.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building jjf 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ jjf ---
[INFO] feifei:jjf:jar:1.0-SNAPSHOT
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:1.3.2.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter:jar:1.3.2.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot:jar:1.3.2.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:1.3.2.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:1.3.2.RELEASE:compile
[INFO] | | +- ch.qos.logback:logback-classic:jar:1.1.3:compile
[INFO] | | | +- ch.qos.logback:logback-core:jar:1.1.3:compile
[INFO] | | | \- org.slf4j:slf4j-api:jar:1.7.13:compile
[INFO] | | +- org.slf4j:jcl-over-slf4j:jar:1.7.13:compile
[INFO] | | +- org.slf4j:jul-to-slf4j:jar:1.7.13:compile
[INFO] | | \- org.slf4j:log4j-over-slf4j:jar:1.7.13:compile
[INFO] | +- org.springframework:spring-core:jar:4.2.4.RELEASE:compile
[INFO] | \- org.yaml:snakeyaml:jar:1.16:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.3.2.RELEASE:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.30:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.30:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.30:compile
[INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.30:compile
[INFO] +- org.springframework.boot:spring-boot-starter-validation:jar:1.3.2.RELEASE:compile
[INFO] | \- org.hibernate:hibernate-validator:jar:5.2.2.Final:compile
[INFO] | +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] | +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] | \- com.fasterxml:classmate:jar:1.1.0:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.6.5:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.5:compile
[INFO] | \- com.fasterxml.jackson.core:jackson-core:jar:2.6.5:compile
[INFO] +- org.springframework:spring-web:jar:4.2.4.RELEASE:compile
[INFO] | +- org.springframework:spring-aop:jar:4.2.4.RELEASE:compile
[INFO] | | \- aopalliance:aopalliance:jar:1.0:compile
[INFO] | +- org.springframework:spring-beans:jar:4.2.4.RELEASE:compile
[INFO] | \- org.springframework:spring-context:jar:4.2.4.RELEASE:compile
[INFO] \- org.springframework:spring-webmvc:jar:4.2.4.RELEASE:compile
[INFO] \- org.springframework:spring-expression:jar:4.2.4.RELEASE:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.573 s
[INFO] Finished at: 2016-02-10T17:45:11+08:00
[INFO] Final Memory: 19M/176M
[INFO] ------------------------------------------------------------------------
3、 Writing the code
编写项目代码,maven工程从src/main/java找源代码
To finish our application we need to create a single Java file. Maven will compile sources from src/main/java by default so you need to create that folder structure, then add a file named src/main/java/Example.java:
/** * Created by liuh on 16/2/10. */
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
3.1 The @RestController and @RequestMapping annotations
The first annotation on our Example class is @RestController. This is known as a stereotype annotation. It provides hints for people reading the code, and for Spring, that the class plays a specific role. In this case, our class is a web @Controller so Spring will consider it when handling incoming web requests.
The @RequestMapping annotation provides “routing” information. It is telling Spring that any HTTP request with the path “/” should be mapped to the home method. The @RestController annotation tells Spring to render the resulting string directly back to the caller.
@RestController和@RequestMapping两个注解都是Spring MVC的注解,前者告诉大家代码是一个web controller会处理外部的web请求。后者是一个路由。
The @RestController and @RequestMapping annotations are Spring MVC annotations (they are not specific to Spring Boot). See the MVC section in the Spring Reference Documentation for more details.
3.2 The @EnableAutoConfiguration annotation
@EnableAutoConfiguration是用来告诉spring boot通过添加的jar包依赖来配置Spring。
The second class-level annotation is @EnableAutoConfiguration
. This annotation tells Spring Boot to “guess” how you will want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration will assume that you are developing a web application and setup Spring accordingly.
Starter POMs and Auto-Configuration
Auto-configuration is designed to work well with “Starter POMs”, but the two concepts are not directly tied. You are free to pick-and-choose jar dependencies outside of the starter POMs and Spring Boot will still do its best to auto-configure your application.
3.3 The “main” method
标准的java方法。调用SpringApplication中的run方法,传入Example.class告诉SpringApplication哪个是主Spring组件。
The final part of our application is the main method. This is just a standard method that follows the Java convention for an application entry point. Our main method delegates to Spring Boot’s SpringApplication class by calling run. SpringApplication will bootstrap our application, starting Spring which will in turn start the auto-configured Tomcat web server. We need to pass Example.class as an argument to the run method to tell SpringApplication which is the primary Spring component. The args array is also passed through to expose any command-line arguments.
5、 Creating an executable jar
Let’s finish our example by creating a completely self-contained executable jar file that we could run in production. Executable jars (sometimes called “fat jars”) are archives containing your compiled classes along with all of the jar dependencies that your code needs to run.
在pom中增加 maven-plugin告诉maven能够打包成可以执行的jar包
To create an executable jar we need to add the spring-boot-maven-plugin to our pom.xml. Insert the following lines just below thedependencies section:
org.springframework.boot
spring-boot-maven-plugin