SpringBoot的配置文件有默認的application.properties
還可以使用YAML
區別:
application.properties示例:
server.port=8090
server.session-timeout=30
server.tomcat.max-threads=0
server.tomcat.uri-encoding=UTF-8
application.yml示例:
server:
port: 8090
session-timeout: 30
tomcat.max-threads: 0
tomcat.uri-encoding: UTF-8
兩種方式都優於SSM的XML配置形式,個人更偏向於使用properties文件
SpringBoot默認的配置項:
這里不多寫了,可以去Spring官網查找
也可以參考一位大佬的博客:
https://www.cnblogs.com/SimpleWu/p/10025314.html
如何在代碼中讀取配置文件中的自定義信息呢?
比如:
file.path=D:\\temp\\images\\
想要在代碼中獲取這個值的話:
@Value("${file.path}") private String FILE_PATH;
將配置文件映射到實體類:
第一種方式:手動給實體類屬性注入
配置文件:
test.name=springboot
test.domain=www.dreamtech.org
實體類:
package org.dreamtech.springboot.domain; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ServerSettings { @Value("${test.name}") private String name; @Value("${test.domain}") private String domain; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDomain() { return domain; } public void setDomain(String domain) { this.domain = domain; } }
Controller:
@Autowired private ServerSettings serverSettings; @RequestMapping("/test") @ResponseBody private ServerSettings test() { return serverSettings; }
第二種方式:根據前綴自動注入
實體類:
package org.dreamtech.springboot.domain; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "test") public class ServerSettings { private String name; private String domain; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDomain() { return domain; } public void setDomain(String domain) { this.domain = domain; } }
注意:如果使用了前綴的方式,那么不能再使用@Value注解了!
下面進行SpringBoot測試學習:
普通的單元測試:
首先導入依賴:通常SpringBoot新項目帶有這個依賴,如果沒有,自行導入即可
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
在test目錄下新建測試類:
@Test用於測試;@Before測試前運行;@After測試后運行
package org.dreamtech.springboot; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import junit.framework.TestCase; @RunWith(SpringRunner.class) @SpringBootTest(classes = { SpringbootApplication.class }) public class SpringBootTestDemo { @Test public void testOne() { System.out.println("hello world!"); TestCase.assertEquals(1, 1); } @Before public void testBefore() { System.out.println("Before"); } @After public void testAfter() { System.out.println("After"); } }
對API接口進行測試:使用MockMVC
MockMVC相當於就是一個HTTP客戶端,模擬用戶使用瀏覽器進行操作
寫一個接口進行測試:
@RequestMapping("/test") @ResponseBody private String test() { return "test"; }
測試類:
package org.dreamtech.springboot; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import junit.framework.TestCase; @RunWith(SpringRunner.class) @SpringBootTest(classes = { SpringbootApplication.class }) @AutoConfigureMockMvc public class MockMvcTest { @Autowired private MockMvc mockMvc; @Test public void apiTest() throws Exception { // 以GET方式訪問/test路徑,如果返回是200狀態碼說明調用成功 MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/test")) .andExpect(MockMvcResultMatchers.status().isOk()).andReturn(); TestCase.assertEquals(mvcResult.getResponse().getStatus(), 200); } }
最后記錄一點有趣的東西:
啟動的時候顯示SpringBoot多單調啊,不如找一些好看的!
在classpath下面寫一個banner.txt:
//////////////////////////////////////////////////////////////////// // _ooOoo_ // // o8888888o // // 88" . "88 // // (| ^_^ |) // // O\ = /O // // ____/`---'\____ // // .' \\| |// `. // // / \\||| : |||// \ // // / _||||| -:- |||||- \ // // | | \\\ - /// | | // // | \_| ''\---/'' | | // // \ .-\__ `-` ___/-. / // // ___`. .' /--.--\ `. . ___ // // ."" '< `.___\_<|>_/___.' >'"". // // | | : `- \`.;`\ _ /`;.`/ - ` : | | // // \ \ `-. \_ __\ /__ _/ .-` / / // // ========`-.____`-.___\_____/___.-`____.-'======== // // `=---=' // // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // // 佛祖保佑 永不宕機 永無BUG // //////////////////////////////////////////////////////////////////// Spring Boot Version: ${spring-boot.version}