一、單元測試
生成的demo里面包含spring-boot-starter-test :測試模塊,包括JUnit、Hamcrest、Mockito,沒有的手動加上。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test
</artifactId> </dependency>
添加測試類:
@RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests { private MockMvc mvc; @Before public void setUp(){ mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build(); } @Test public void getHello() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().string(equalTo("Hello World"))); } }
二、修改訪問端口和路徑
1、修改端口號
- 使用properties文件方式:
server.port=8088
- 使用yml文件方式:
server: port:8088
2、修改項目訪問路徑
使用properties文件方式:在application.properties,添加如下配置即可修改項目訪問路徑:
server.servlet.context-path=/springboot-demo
- 使用yml文件方式:
server: port:8088
servlet:
context-path: /springboot-demo
3、修改默認容器
在之前默認使用的 WEB 容器是 Tomcat 容器,實際上在 SpringBoot 里面如果用戶有需要也可以將容器更換為 jetty 容器,如果 現在要想使用這個容器,則只需要追加一些依賴即可:
1、移除默認容器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
2、添加jetty
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
三、加載讀取多個配置文件
1、application.properties 和 application.yml的優先級
那么這個時候將優先進行application.properties配置文件的加載,如果現在兩個配置項的作用沖突了,則以 properties 為主,如果不沖突,則以存在的為主。
2、多配置文件支持(yml和property)
格式必須是application-xxx.properties或者是application-xxx.yml命
在application.properties或application.yml中引入,有兩個用法:
1、直接引入
創建application-message.properties,內容如下:
code=123434-message content=消息不存在
在application.properties或application.yml中引入,多個文件用逗號隔開
spring.profiles.include=message
注入到bean中
@Component public class Message{ @Value("${code}") private String code; @Value("${content}") private String content; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
使用:
package com.example.demo.controller; import com.example.demo.Property.BaseMsg; import com.example.demo.Property.Message; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Properties; @RestController public class HelloWorldController { @Autowired private Message message; @RequestMapping("/message") public String message() { System.out.println(message.getCode()); return "message:"+message.getCode(); } }
測試,訪問:http://localhost:8082/message
2、特定環境激活
spring.profiles.active=devMsg
或通過啟動參數激活,在啟動命令中添加
-Dspring.profiles.active="devMsg"
@Profile表示只有當spring.profiles.active激活配置一致時才會注入,用法示例:
@Component @Profile("testMsg") public class TestMsg extends BaseMsg{ @Value("${msg.code}") private String msgCode; public String getMsgCode() { return msgCode; } public void setMsgCode(String msgCode) { this.msgCode = msgCode; } }
四、springboot多環境配置
多環境配置有兩種方式,一種配置在同一個文件中,另一種時時配置在多個文件中。
1、配置在多個文件中
在Spring Boot中多環境配置文件名需要滿足application-{profile}.properties
的格式,其中{profile}
對應你的環境標識,比如:
application-dev.properties:開發環境
application-test.properties:測試環境
application-prod.properties:生產環境
至於哪個具體的配置文件會被加載,需要在application.properties
文件中通過spring.profiles.active
屬性來設置,其值對應{profile}
值。
spring.profiles.active=test
就會加載
application-test.properties
配置文件內容
或
執行java -jar xxx.jar --spring.profiles.active=test,也就是測試環境的配置(test)
2、配置同一個文件中
spring: profiles: active: pro
server: servlet: context-path: /springboot-demo --- #開發環境配置 spring: profiles: dev server: port: 8080 --- #測試環境配置 spring: profiles: test server: port: 8081 --- #生產環境配置 spring: profiles: pro server: port: 8082
通過spring.profiles.active: pro或者java -jar xxx.jar --spring.profiles.active=pro動態切換環境,---分隔符,分開后相當於單獨一個文件,---前是公共配置。System.getProperty()方法獲取系統變量和啟動參數。
五、打包發布
1、打包
- 運行maven package,如果項目有改動需要先運行maven clean
- 打包完,target下面會有項目jar包,demo-0.0.1-SNAPSHOT.jar
2、運行
拷貝demo-0.0.1-SNAPSHOT.jar到指定目錄,運行
java -jar demo-0.0.1-SNAPSHOT.jar
訪問:http://192.168.1.100:8081/hello