SpringBoot使用笔记
其實(shí)也是參考官方的:http://spring.io/guides/gs/rest-service/?,在官方代碼基礎(chǔ)上加入了很多實(shí)用的東西,比如運(yùn)行環(huán)境啟動(dòng)命令等等。
官方文檔:http://docs.spring.io/spring-boot/docs/current/reference/html/
?
SpringBoot并不神秘,其最大的好處是可以幫你省略引用一堆jar包,需要神秘jar它自動(dòng)幫你引用,集成tomcat,集成配置等待好處太多,總之就是更方便開(kāi)發(fā)而已。
還是自己體驗(yàn)下比較好。
?
1.建立java應(yīng)用程序
起一個(gè)Maven的java應(yīng)用程序,注意不要再起Web應(yīng)用程序了:
?
2.maven配置文件
pom.xml:
?
<?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.xxx</groupId><artifactId>spring-boot-hello</artifactId><version>1.0.0</version><packaging>jar</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.3.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></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><dependency><groupId>com.jayway.jsonpath</groupId><artifactId>json-path</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>
3.新建類文件
?
?
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
?
public class Greeting {private final long id;private final String content;public Greeting(long id, String content) {this.id = id;this.content = content;}public long getId() {return id;}public String getContent() {return content;}
}
?
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class GreetingController {private static final String template = "Hello, %s!";private final AtomicLong counter = new AtomicLong();@RequestMapping("/greeting")public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {return new Greeting(counter.incrementAndGet(),String.format(template, name));}
}
4.運(yùn)行項(xiàng)目
?
注意:SpringBoot已經(jīng)集成了tomcat所以無(wú)需再安裝了,默認(rèn)是8080不要和現(xiàn)有的tomcat沖突
成功啟動(dòng)會(huì)提示:FrameworkServlet 'dispatcherServlet': initialization completed in 10 ms
?
5.驗(yàn)證
http://localhost:8080/greeting
6.集成依賴文件
我們?nèi)タ聪孪到y(tǒng)依賴文件,發(fā)現(xiàn)springmvc都自動(dòng)被引入,如果不適用springboot就需要一個(gè)個(gè)去單獨(dú)引用。
?
7.命令行下啟動(dòng)
實(shí)際項(xiàng)目中肯定是到命令行下啟動(dòng)
Windows下
#?java -jar spring-boot-hello-1.0.0.jar
?
Linux下
實(shí)際是需要再后臺(tái)運(yùn)行的加個(gè)&符號(hào),比如這樣
# nohup java -jar spring-boot-hello-1.0.0.jar >/dev/null 2>&1 &
?
定制化JVM啟動(dòng)參數(shù)
#?nohup java -Xms1024m -Xmx1024m -Xss1024K -XX:PermSize=64m -XX:MaxPermSize=128m -jar spring-boot-hello-1.0.0.jar >/dev/null 2>&1 &
?
8.Tomcat下運(yùn)行
其實(shí)完全沒(méi)必要再到tomcat下再去運(yùn)行了,如果真的還需要這樣可以打war包即可。
<artifactId>spring-boot-hello</artifactId><version>1.0.0</version><packaging>jar</packaging>
改為
<artifactId>spring-boot-hello</artifactId><version>1.0.0</version><packaging>war</packaging>
需要注意的是jdk需要1.8以上版本,Tomcat需要8.0以上版本
?
9.Spring Boot配置
如果你需要修改配置,可以在resources文件夾下創(chuàng)建一個(gè)application.properties或者application.yml文件,這個(gè)文件會(huì)被發(fā)布到classpath。
?
比如修改默認(rèn)的tomcat端口為80
application.properties
server.port: 80
server.tomcat.uri-encoding: UTF-8
?
application.yml
server:port: 80tomcat:uri-encoding: UTF-8
?
再重新運(yùn)行tomcat就是80端口了:
?
更多配置參考:http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
?
另外:推薦一本書(shū)《深入實(shí)踐Spring Boot》
?
10.spring boot多環(huán)境配置
一般會(huì)有開(kāi)發(fā)環(huán)境,測(cè)試環(huán)境,正式環(huán)境,甚至于預(yù)發(fā)布環(huán)境。
配置文件不管理好,如果這個(gè)靠手工去做非常不靠譜,很容易出錯(cuò)。
Spring boot可以配置多個(gè)配置文件,自動(dòng)切換。
?
application-online.properties
server.port: 80
server.tomcat.uri-encoding: UTF-8
application-test.properties
server.port: 8080
server.tomcat.uri-encoding: UTF-8
application.properties里激活需要的環(huán)境即可
spring.profiles.active=test
?
?
也可以通過(guò)命令行方式啟動(dòng)
將程序打包之后 通過(guò) java -jar xx.jar --spring.profiles.active=dev的方式啟動(dòng)
參考:??SpringBoot學(xué)習(xí)筆記(2) Spring Boot的一些配置
SpringBoot四種讀取properties文件的方式
springBoot 獲取配置的方式(5種)
使用IDEA構(gòu)建spring boot項(xiàng)目簡(jiǎn)單示例?
SpringBoot之1分鐘快速搭建Web項(xiàng)目
總結(jié)
以上是生活随笔為你收集整理的SpringBoot使用笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 我想上电视,但我不会唱歌跳舞,有适合我的
- 下一篇: Java的SPI机制