“越来越多的企业选择使用spring boot 开发系统,spring boot牛在什么地方?难不难学?心动不如行动,让我们一起开始学习吧!”
使用Spring boot ,可以轻松的创建独立运行的程序,非常容易构建独立的服务组件,是实现分布式架构、微服务架构利器。Spring boot简化了第三方包的引用,通过提供的starter,简化了依赖包的配置。
Spring boot的优点
- 轻松创建独立的Spring应用程序。
- 内嵌Tomcat、jetty等web容器,不需要部署WAR文件。
- 提供一系列的“starter” 来简化的Maven配置。
- 开箱即用,尽可能自动配置Spring。
spring boot 快速入门
通过构建简单的REST应用,了解spring boot的开发基本流程,验证其简单、易用特性。
环境要求
Spring Boot 2.0.0.BUILD-SNAPSHOT 要求 Java 8 和 Spring Framework 5.0.2以上,Maven 3.2 以上或者Gradle 4。
本文使用 Spring Boot 1.5.9 、 Java8 和 Spring Framework 5.0.2.RELEASE以上,Maven 3.2。开发工具使用sping官方提供的spring suit tool 3.9.1(STS)。
创建项目
在STS中,通过NEW->Spring starter project创建spring boot 项目。
输入maven的group 和artifact。
选择spring boot版本和starter
点击下一步,进入如下界面。
选择spring boot的版本,这里选择1.5.9 版本。
选择starter,通过搜索找到web 并勾选。点击完成。
创建项目的结构
点击finish 按钮。创建项目如下:
目录结构如图。
- Src/main/java。编写代码存放的目录。自动生成了程序入口代码 SpringBootDemo1Application.java。
- Src/main/resources。资源文件存放目录。自动生成了配置文件 application.properties
- Src/test/java。测试代码存放目录。自动生成了测试代码SpringBootDemo1ApplicationTests.java
POM文件说明
spring boot项目默认使用maven来构建,生成的POM文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.xi</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
其中,
设置spring-boot-starter-parent为父亲项目
这种方式可以比较容易的使用父项目中的starters的依赖。 当然也可以不用继承spring-boot-starter-parent为父亲,这种方式在以后我们会介绍。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
引入web依赖
Web starter依赖引入,会增加web容器、springweb、springmvc、jackson-databind等相关的依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
依赖层级关系如图
引入测试依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
启动程序SpringBootDemo1Application 说明
我们通过此类的main函数来启动spring boot程序。
启动程序SpringBootDemo1Application是自动生成的,代码如下:
package com.xi.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
其中是@SpringBootApplication组合注解,兼备了@EnableAutoConfiguration和@ComponentScan 注解的功能。
增加一个controller
在包com.xi.demo.controller下面增加类SampleController
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleController {
@RequestMapping("/")
String home() {
return "Hello World!";
}
}
运行启动程序后,访问http://localhost:8080/就可以访问这个controller的功能了。
启动很简单,直接选择DemoApplication.java文件,使用java application方式运行即可:
D:\sofware\android\jdk1.8\bin\java -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=57477 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -javaagent:D:\sofware\java\ideaIU\lib\idea_rt.jar=57478:D:\sofware\java\ideaIU\bin -Dfile.encoding=UTF-8 -classpath D:\sofware\android\jdk1.8\jre\lib\charsets.jar;D:\sofware\android\jdk1.8\jre\lib\deploy.jar;D:\sofware\android\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\sofware\android\jdk1.8\jre\lib\ext\cldrdata.jar;D:\sofware\android\jdk1.8\jre\lib\ext\dnsns.jar;D:\sofware\android\jdk1.8\jre\lib\ext\jaccess.jar;D:\sofware\android\jdk1.8\jre\lib\ext\jfxrt.jar;D:\sofware\android\jdk1.8\jre\lib\ext\localedata.jar;D:\sofware\android\jdk1.8\jre\lib\ext\nashorn.jar;D:\sofware\android\jdk1.8\jre\lib\ext\sunec.jar;D:\sofware\android\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\sofware\android\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\sofware\android\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\sofware\android\jdk1.8\jre\lib\ext\zipfs.jar;D:\sofware\android\jdk1.8\jre\lib\javaws.jar;D:\sofware\android\jdk1.8\jre\lib\jce.jar;D:\sofware\android\jdk1.8\jre\lib\jfr.jar;D:\sofware\android\jdk1.8\jre\lib\jfxswt.jar;D:\sofware\android\jdk1.8\jre\lib\jsse.jar;D:\sofware\android\jdk1.8\jre\lib\management-agent.jar;D:\sofware\android\jdk1.8\jre\lib\plugin.jar;D:\sofware\android\jdk1.8\jre\lib\resources.jar;D:\sofware\android\jdk1.8\jre\lib\rt.jar;F:\DaoEclipseWorkplace\SpringBootDemo\target\classes;C:\Users\xijie\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.0.4.RELEASE\spring-boot-starter-web-2.0.4.RELEASE.jar;C:\Users\xijie\.m2\repository\org\springframework\boot\spring-boot-starter\2.0.4.RELEASE\spring-boot-starter-2.0.4.RELEASE.jar;C:\Users\xijie\.m2\repository\org\springframework\boot\spring-boot\2.0.4.RELEASE\spring-boot-2.0.4.RELEASE.jar;C:\Users\xijie\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.0.4.RELEASE\spring-boot-autoconfigure-2.0.4.RELEASE.jar;C:\Users\xijie\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.0.4.RELEASE\spring-boot-starter-logging-2.0.4.RELEASE.jar;C:\Users\xijie\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\xijie\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\xijie\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;C:\Users\xijie\.m2\repository\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;C:\Users\xijie\.m2\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;C:\Users\xijie\.m2\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;C:\Users\xijie\.m2\repository\org\yaml\snakeyaml\1.19\snakeyaml-1.19.jar;C:\Users\xijie\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.0.4.RELEASE\spring-boot-starter-json-2.0.4.RELEASE.jar;C:\Users\xijie\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.9.6\jackson-databind-2.9.6.jar;C:\Users\xijie\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;C:\Users\xijie\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.9.6\jackson-core-2.9.6.jar;C:\Users\xijie\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.6\jackson-datatype-jdk8-2.9.6.jar;C:\Users\xijie\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.6\jackson-datatype-jsr310-2.9.6.jar;C:\Users\xijie\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.6\jackson-module-parameter-names-2.9.6.jar;C:\Users\xijie\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.0.4.RELEASE\spring-boot-starter-tomcat-2.0.4.RELEASE.jar;C:\Users\xijie\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\8.5.32\tomcat-embed-core-8.5.32.jar;C:\Users\xijie\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\8.5.32\tomcat-embed-el-8.5.32.jar;C:\Users\xijie\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.32\tomcat-embed-websocket-8.5.32.jar;C:\Users\xijie\.m2\repository\org\hibernate\validator\hibernate-validator\6.0.11.Final\hibernate-validator-6.0.11.Final.jar;C:\Users\xijie\.m2\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;C:\Users\xijie\.m2\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\Users\xijie\.m2\repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;C:\Users\xijie\.m2\repository\org\springframework\spring-web\5.0.8.RELEASE\spring-web-5.0.8.RELEASE.jar;C:\Users\xijie\.m2\repository\org\springframework\spring-beans\5.0.8.RELEASE\spring-beans-5.0.8.RELEASE.jar;C:\Users\xijie\.m2\repository\org\springframework\spring-webmvc\5.0.8.RELEASE\spring-webmvc-5.0.8.RELEASE.jar;C:\Users\xijie\.m2\repository\org\springframework\spring-aop\5.0.8.RELEASE\spring-aop-5.0.8.RELEASE.jar;C:\Users\xijie\.m2\repository\org\springframework\spring-context\5.0.8.RELEASE\spring-context-5.0.8.RELEASE.jar;C:\Users\xijie\.m2\repository\org\springframework\spring-expression\5.0.8.RELEASE\spring-expression-5.0.8.RELEASE.jar;C:\Users\xijie\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\xijie\.m2\repository\org\springframework\spring-core\5.0.8.RELEASE\spring-core-5.0.8.RELEASE.jar;C:\Users\xijie\.m2\repository\org\springframework\spring-jcl\5.0.8.RELEASE\spring-jcl-5.0.8.RELEASE.jar com.xi.demo.DemoApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.4.RELEASE)
2018-07-31 20:10:32.027 INFO 7752 --- [ main] com.xi.demo.DemoApplication : Starting DemoApplication on DESKTOP-XIJIE with PID 7752 (F:\DaoEclipseWorkplace\SpringBootDemo\target\classes started by xijie in F:\DaoEclipseWorkplace\SpringBootDemo)
2018-07-31 20:10:32.029 INFO 7752 --- [ main] com.xi.demo.DemoApplication : No active profile set, falling back to default profiles: default
2018-07-31 20:10:32.068 INFO 7752 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@79ca92b9: startup date [Tue Jul 31 20:10:32 CST 2018]; root of context hierarchy
2018-07-31 20:10:32.963 INFO 7752 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-07-31 20:10:32.977 INFO 7752 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-07-31 20:10:32.977 INFO 7752 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.32
2018-07-31 20:10:32.980 INFO 7752 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\sofware\android\jdk1.8\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\sofware\android\jdk1.8\bin;D:\sofware\android\jdk1.8\jre\bin;D:\SDKQuanBao\adt-bundle-windows-x86_64-20140702\sdk\platform-tools;D:\sofware\android\svn\bin;C:\Go\bin;C:\Program Files\Git\cmd;C:\Python27;D:\sofware\java\MySQLServer5.5\bin;D:\sofware\java\redis-64.3.0.503;D:\sofware\java\apache-maven-3.3.9\bin;.]
2018-07-31 20:10:33.087 INFO 7752 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-07-31 20:10:33.087 INFO 7752 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1024 ms
2018-07-31 20:10:33.142 INFO 7752 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-07-31 20:10:33.144 INFO 7752 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-07-31 20:10:33.145 INFO 7752 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-07-31 20:10:33.145 INFO 7752 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-07-31 20:10:33.145 INFO 7752 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-07-31 20:10:33.231 INFO 7752 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-31 20:10:33.351 INFO 7752 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@79ca92b9: startup date [Tue Jul 31 20:10:32 CST 2018]; root of context hierarchy
2018-07-31 20:10:33.386 INFO 7752 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.xi.demo.controller.SampleController.home()
2018-07-31 20:10:33.389 INFO 7752 --- [ 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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-07-31 20:10:33.390 INFO 7752 --- [ 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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-07-31 20:10:33.405 INFO 7752 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-31 20:10:33.405 INFO 7752 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-31 20:10:33.507 INFO 7752 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-07-31 20:10:33.535 INFO 7752 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-07-31 20:10:33.538 INFO 7752 --- [ main] com.xi.demo.DemoApplication : Started DemoApplication in 1.773 seconds (JVM running for 2.41)
访问效果: