SpringBoot自學匯總
啟動彩蛋修改:
項目resources目錄下建立banner.txt文件就可替換原來的菜單
字符畫生成的網站http://www.network-science.de/ascii/ http://patorjk.com/software/taag/
切換不同環境配置
在idea 啟動配置program arguments加上–spring.profiles.active={profile},或在dos行加上–spring.profiles.active={profile};
或配置文件spring.profiles.active={profile}
各個環境公共的配置寫在application.properties中
各個模塊獨有的配置配置在自己的application-{xxx}.properties文件中
程序讀取的時候優先讀取application.properties中選中的profile的配置,若讀不到才會從application.properties去讀
讀取配置
[email protected] [email protected]("${cusvar}"
@Value("${app.name}")
private String cusvar ; 將${app.name}值賦予cusvar
name= HowieLi
age= 18
content= "name: ${name}, age: ${age}"
代碼中直接調用content就可以了,訪問啟動的應用顯示name: HowieLi, age: 18。
@RestController該註解是Spring4之後新加的註解,[email protected]@ResponseBody的組合。
@RequestMapping(value = "/hello", method = RequestMethod.GET)== @GetMapping("/hello")
@RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET)訪問/hello和/hi是一樣的效果
@GetMapping(value = "/say/{id}")
public String helloGet(@PathVariable("id") int id, @RequestParam("name") String name) {return "id: " + id + ",name:" + name;}訪問http://localhost:8080/say/5?name=howieli
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import lombok.Data; /** * Created by [email protected] on 2017/07/01. */ @Data @Component //將Person類交由Spring容器管理 @ConfigurationProperties(prefix = "person") //填寫配置文件中的前綴 public class Person { private String name; private int age; // // public String getName() { // return name; // } // public void setName(String name) { // this.name = name; // } // public int getAge() { // return age; // } // public void setAge(int age) { // this.age = age; // } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } } @Autowired private Person person; @RequestMapping("/hellTask") public String hellTask(){ logger.info("訪問hellTask"); return person.toString(); }
獲得配置文件值
本文出自 “藍色的天空” 博客,請務必保留此出處http://shurk.blog.51cto.com/1134443/1943959
SpringBoot自學匯總