先想一下,正常咱們想要建立一個web服務,首先須要下載tomcat,建立web工程,配置各類web.xml,引入spring的配置,各類配置文件一頓倒騰.....下載有了spring boot,你建立一個web工程只須要一個java類,甚至都不須要下載tomcat,直接右鍵執行就能啓動一個web服務。聽起來就讓人感受興奮!html
最近我也是工做有須要,須要新建一個微服務的模塊。正好公司比較開放,支持搞搞新技術,因而就在同事的慫恿下采用Spring Boot建立了一個工程。使用後發現若是熟練掌握一些配置的技巧,那麼實際上是事半功倍的。(固然你須要花點時間熟悉一下Spring Boot的流程)。不過建立這樣一個工程真的是很簡單,下面就先看看效果:java
前提條件確定是要安裝jdk和maven,配置好環境變量,這個就很少說了:git
xinghailong@DESKTOP-JB5HET6 MINGW64 ~/Documents/workspace/my $ java -version java version "1.8.0_66" Java(TM) SE Runtime Environment (build 1.8.0_66-b17) Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode) xinghailong@DESKTOP-JB5HET6 MINGW64 ~/Documents/workspace/my $ mvn -version Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00) Maven home: C:\Users\xinghailong\Documents\soft\apache-maven-3.3.9 Java version: 1.8.0_66, vendor: Oracle Corporation Java home: C:\Program Files\Java\jdk1.8.0_66\jre Default locale: zh_CN, platform encoding: GBK OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>xingoo</groupId> <artifactId>SimpleSpringBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <!-- 自動依賴父pom,能夠省略不少的配置--> <!-- 若是已經有了父依賴,那麼能夠參考:http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/#using-boot-maven-without-a-parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
package main.java.xingoo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** * Created by xinghailong on 2017/7/21. */ /*@Configuration @EnableAutoConfiguration @ComponentScan*/ @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
而且建立一個Controller(你也能夠直接在上面的類中建立請求Mapping)github
package main.java.xingoo.web; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by xinghailong on 2017/7/21. */ @RestController public class TestController { @RequestMapping("/hello") public String hello(){ return "hello world!"; } }
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.4.RELEASE) 2017-07-21 23:46:50.580 INFO 22236 --- [ main] main.java.xingoo.Application : Starting Application on DESKTOP-JB5HET6 with PID 22236 (C:\Users\xinghailong\Documents\workspace\my\SimpleSpringBoot\target\classes started by xinghailong in C:\Users\xinghailong\Documents\workspace\tiangou\code\workbench) 2017-07-21 23:46:50.588 INFO 22236 --- [ main] main.java.xingoo.Application : No active profile set, falling back to default profiles: default 2017-07-21 23:46:50.700 INFO 22236 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4da4253: startup date [Fri Jul 21 23:46:50 CST 2017]; root of context hierarchy 2017-07-21 23:46:54.086 INFO 22236 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2017-07-21 23:46:54.117 INFO 22236 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2017-07-21 23:46:54.118 INFO 22236 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15 2017-07-21 23:46:54.346 INFO 22236 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2017-07-21 23:46:54.346 INFO 22236 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3653 ms 2017-07-21 23:46:54.684 INFO 22236 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2017-07-21 23:46:54.713 INFO 22236 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2017-07-21 23:46:54.715 INFO 22236 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2017-07-21 23:46:54.717 INFO 22236 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2017-07-21 23:46:54.717 INFO 22236 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2017-07-21 23:46:55.302 INFO 22236 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4da4253: startup date [Fri Jul 21 23:46:50 CST 2017]; root of context hierarchy 2017-07-21 23:46:55.472 INFO 22236 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String main.java.xingoo.web.TestController.hello() 2017-07-21 23:46:55.480 INFO 22236 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2017-07-21 23:46:55.481 INFO 22236 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2017-07-21 23:46:55.564 INFO 22236 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-07-21 23:46:55.564 INFO 22236 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-07-21 23:46:55.632 INFO 22236 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-07-21 23:46:56.004 INFO 22236 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2017-07-21 23:46:56.134 INFO 22236 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2017-07-21 23:46:56.142 INFO 22236 --- [ main] main.java.xingoo.Application : Started Application in 6.834 seconds (JVM running for 8.969)
就能看到輸出信息了。web
hello world!
代碼我已經上傳到github上面,若是有興趣的能夠直接clone下來使用:https://github.com/xinghalo/SimpleSpringBoot.gitspring
@EnableAutoConfiguration
其餘的內容就不說了,跟以前部署到tomcat差很少,不一樣的是多了這個註解,這個註解的做用是會去根據配置的pom依賴,自動加載一些類,好比數據庫的dataSource等。數據庫
SpringBoot的項目能夠直接打成一個 可執行的jar包,即fat jar
。通常狀況下java是不支持內嵌jar的,它會在你打包的時候把class抽離出來放在一個jar裏面,若是有兩個class名稱和目錄都相同,那麼就會出現衝突。所以Spring Boot提供了本身的打包插件,這就須要在build當中引入該plugin:apache
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
而後,在pom.xml同層目錄下,執行命令mvn clean package
就能夠打包了。windows
com +- example +- myproject +- Application.java | +- domain | +- Customer.java | +- CustomerRepository.java | +- service | +- CustomerService.java | +- web +- CustomerController.java
在Spring Boot中,通常不多使用xml進行配置,都是基於Class來配置的。若是有一些配置項,那麼能夠把這個類加上註解@Configuration
。若是額外須要引入xml,也可使用註解@ImportResource
添加xml文件tomcat
好比你的項目根本不須要引入數據庫鏈接池,那麼就可使用exclude進行排除:
@Configuration @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) public class MyConfiguration { }
這個註解能夠當作是@Configuration, @EnableAutoConfiguration and @ComponentScan
的合體
執行下面的命令,也能夠經過maven啓動spring boot
mvn spring-boot:run