创建Spring boot工程
Spring boot可以使用很少的配置,来快速创建基于spring的应用,并且内嵌了Tomcat,Jetty等组件,只需要启动run即可。
创建Spring boot之前需要检查jdk及maven配置是否正常。
Spring boot提供了很多Starter-POMs可以是开发者很容易的将所需的jar添加到classpath中。
新建maven工程并编辑pom文件:
首先需要添加spring-boot-starter-parent:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
但是有的公司或者个人自己封装了标准接口,这样也可以将spring-boot-starter-parent替换为spring-boot-dependencies添加到pom中:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.2.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
创建web工程还需要添加spring-boot-starter-web:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
创建主方法:
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
public String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(SampleController.class, args);
}
}
这样一个Spring boot工程就创建好了,运行main方法,在地址中访问http://127.0.0.1:8080/即可看到“Hello World!”。
创建一个可执行的jar,需要将所有编译的文件,以及代码所依赖的jar包全部打包进来,只需要将spring-boot-maven-plugin添加到pom中:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.test.SampleController</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
打包成jar之后的项目大小应该在13M左右,在工程的target目录下可通过java -jar springboot-0.0.1-SNAPSHOT.jar来启动服务。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.0.RELEASE)
2016-08-21 16:24:30.908 INFO 51544 --- [ main] com.test.SampleController : Starting SampleController on TN-00-50000814 with PID 51544 (D:\work\code\springboot\target\classes started by lijie12 in D:\work\code\springboot)
2016-08-21 16:24:30.910 INFO 51544 --- [ main] com.test.SampleController : No active profile set, falling back to default profiles: default
2016-08-21 16:24:30.945 INFO 51544 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@21de60b4: startup date [Sun Aug 21 16:24:30 CST 2016]; root of context hierarchy
2016-08-21 16:24:32.055 INFO 51544 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-08-21 16:24:32.065 INFO 51544 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-08-21 16:24:32.066 INFO 51544 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-08-21 16:24:32.784 INFO 51544 --- [ main] com.test.SampleController : Started SampleController in 2.294 seconds (JVM running for 2.624)
备注:
- @RestController
告诉spring处理对应的请求,并返回response给DispatcherServlet;
- @EnableAutoConfiguration
告诉Spring boot我们想怎么去配置spring,当我们添加了tomcat和mvc,Spring boot将为我们启动一个web应用并自动配置spring。