HM-SpringBoot1.3【SpringBoot配置】
1、配置文件分类
1-1 properties
1-2 yml
1-3 yaml
2、yaml
1 server: 2 port: 8082 3 4 5 name: wangwu 6 7 #对象 8 person: 9 # name: zhansgan 10 name: ${name} #参数引用-->wangwu 11 age: 20 12 #对象的行内写法(键值对,键唯一,不能同时存在两个person) 13 person2: {name: lisi, age: 19} 14 15 #数组 16 address: 17 - nanjing 18 - hangzhou 19 #数组的行内写法 20 address2: [shanghai, shenzhen] 21 22 #纯量 23 msg1: "hello \n world" #会识别转义字符,换行输出 24 msg2: 'hello \n world' #不会识别转义字符,会原样输出
3、读取配置文件内容
application.properties
1 server.port=8081 2 # 程序自动识别的内容 3 4 name: wangwu 5 # 自定义内容,不会被自定识别,需要在程序中手动加载读取
application.yml
1 server: 2 port: 8082 3 4 5 name: zhaoliu 6 7 #对象 8 person: 9 # name: zhansgan 10 name: ${name} #参数引用-->当前文件中zhaoliu,从application.properties(优先级高于本文件)中wangwu 11 age: 20 12 address: 13 - nanjing 14 - hangzhou 15 #对象的行内写法(键值对,键唯一,不能同时存在两个person) 16 person2: {name: lisi, age: 19} 17 18 #数组 19 address: 20 - nanjing 21 - hangzhou 22 #数组的行内写法 23 address2: [shanghai, shenzhen] 24 25 #纯量 26 msg1: "hello \n world" #会识别转义字符,换行输出 27 msg2: 'hello \n world' #不会识别转义字符,会原样输出
application.yaml
1 server: 2 port: 8083
1 package com.haifei.springboot2init; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 import org.springframework.stereotype.Component; 5 6 import java.util.Arrays; 7 8 @Component //标志这个类是一个bean,被spring所识别 9 @ConfigurationProperties(prefix = "person") //指定前缀,即前缀/键为person下的name和age才会被注入 10 public class Person { 11 12 private String name; 13 private int age; 14 private String[] address; 15 16 @Override 17 public String toString() { 18 return "Person{" + 19 "name='" + name + '\'' + 20 ", age=" + age + 21 ", address=" + Arrays.toString(address) + 22 '}'; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public int getAge() { 34 return age; 35 } 36 37 public void setAge(int age) { 38 this.age = age; 39 } 40 41 public String[] getAddress() { 42 return address; 43 } 44 45 public void setAddress(String[] address) { 46 this.address = address; 47 } 48 }
1 package com.haifei.springboot2init; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.beans.factory.annotation.Value; 5 import org.springframework.core.env.Environment; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RestController; 8 9 @RestController 10 public class HelloController { 11 12 @RequestMapping("/hello") 13 public String hello(){ 14 return "hi springboot"; 15 } 16 17 18 @Value("${name}") 19 private String name1; 20 21 @Value("${person.name}") 22 private String name2; 23 24 @Value("${person2.name}") 25 private String name3; 26 27 @Value("${person2.age}") 28 private int age; 29 30 @Value("${address[0]}") 31 private String address1; 32 33 @Value("${msg1}") 34 private String msg1; 35 36 @Value("${msg2}") 37 private String msg2; 38 39 40 //http://localhost:8081/hello2 41 @RequestMapping("/hello2") 42 public void hello2(){ 43 System.out.println(name1); //wangwu 44 System.out.println(name2); //wangwu 45 System.out.println(name3); //lisi 46 System.out.println(age); //19 47 System.out.println(address1); //nanjing 48 System.out.println(msg1); 49 /* 50 hello 51 world 52 */ 53 System.out.println(msg2); 54 /* 55 hello \n world 56 */ 57 } 58 59 60 @Autowired 61 private Environment env; 62 63 //http://localhost:8081/hello3 64 @RequestMapping("/hello3") 65 public void hello3(){ 66 System.out.println(env.getProperty("person.name")); //wangwu 67 System.out.println(env.getProperty("person.age")); //20 68 System.out.println(env.getProperty("address[1]")); //hangzhou 69 } 70 71 72 @Autowired 73 private Person person; 74 75 //http://localhost:8081/hello4 76 @RequestMapping("/hello4") 77 public void hello4(){ 78 System.out.println(person); //Person{name='wangwu', age=20, address=[nanjing, hangzhou]} 79 String[] address = person.getAddress(); 80 for (String s : address) { 81 System.out.println(s); 82 } 83 /* 84 nanjing 85 hangzhou 86 */ 87 } 88 89 }
4、profile
1
2
3 激活-虚拟机参数 
-Dspring.profiles.active=test
4 激活-命令行参数
--spring.profiles.active=dev-yml
5 激活-命令行参数2
5、内部配置加载顺序
4
3
2
1
其他
1 package com.haifei.springboot4config; 2 3 import org.springframework.web.bind.annotation.RequestMapping; 4 import org.springframework.web.bind.annotation.RestController; 5 6 @RestController 7 public class HelloController { 8 9 @RequestMapping("/hi") //配置资源访问路径 10 public String hi(){ 11 return "hi config"; 12 } 13 14 }
6、外部配置加载顺序
1
课程录制时
学习课程时
2
1 <plugin> 2 <groupId>org.apache.maven.plugins</groupId> 3 <artifactId>maven-resources-plugin</artifactId> 4 <version>3.1.0</version> 5 </plugin>
3
4