先想一下,正常咱们想要建立一个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