package com.sxtt.controller.Beans; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; /*** * @Author Laugh" * * 可以通过@Value + SPEL 直接绑定SpringBoot配置文件 .yml里的值 * @Value() 固定写法,“${XXXX.XXXX}” 配置文件里面的值 * 案例1: * @Value("${users.username}") * private String username; * * 案例2: * @ConfigurationProperties(prefix="users") * 常用于bean属性和.yml配置文件 * prefix属性 可以指定配置文件中的某节点,该节点中的子节点将自动和属性进行配对绑定 * 注意:ConfigurationProperties 这个注解 比较简单,应该说是比较容易/松散,不限定特殊写法,例如:大写 / 驼峰 / 下划线 / 中划线 / 小写 都会被识别 */ @Component public class Users { @Value("${users.username}") private String username; @Value("${users.age}") private Integer age; private List<String> hobby; private Date birthday; private Map<Integer,String> girlfriend; private Address address; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public List<String> getHobby() { return hobby; } public void setHobby(List<String> hobby) { this.hobby = hobby; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Map<Integer, String> getGirlfriend() { return girlfriend; } public void setGirlfriend(Map<Integer, String> girlfriend) { this.girlfriend = girlfriend; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "Users{" + "username='" + username + '\'' + ", age=" + age + ", hobby=" + hobby + ", birthday=" + birthday + ", girlfriend=" + girlfriend + ", address=" + address + '}'; } }
server: port: 8080 servlet: context-path: /laugh users: username: Laugh" age: 25
package com.sxtt; import com.sxtt.controller.Beans.Users; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class App { @Autowired private Users user; @Test void contextLoads(){ System.out.println(user); } }
package com.sxtt.controller.Beans; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; /*** * @Author Laugh" * * 可以通过@Value + SPEL 直接绑定SpringBoot配置文件 .yml里的值 * @Value() 固定写法,“${XXXX.XXXX}” 配置文件里面的值 * 案例1: * @Value("${users.username}") * private String username; * * 案例2: * @ConfigurationProperties(prefix="users") * 常用于bean属性和.yml配置文件 * prefix属性 可以指定配置文件中的某节点,该节点中的子节点将自动和属性进行配对绑定 * 注意:ConfigurationProperties 这个注解 比较简单,应该说是比较容易/松散,不限定特殊写法,例如:大写 / 驼峰 / 下划线 / 中划线 / 小写 都会被识别 */ @Component @ConfigurationProperties(prefix="users") public class Users { private String username; private Integer age; private List<String> hobby; private Date birthday; private Map<Integer,String> girlfriend; private Address address; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public List<String> getHobby() { return hobby; } public void setHobby(List<String> hobby) { this.hobby = hobby; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Map<Integer, String> getGirlfriend() { return girlfriend; } public void setGirlfriend(Map<Integer, String> girlfriend) { this.girlfriend = girlfriend; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "Users{" + "username='" + username + '\'' + ", age=" + age + ", hobby=" + hobby + ", birthday=" + birthday + ", girlfriend=" + girlfriend + ", address=" + address + '}'; } }
users: username: Laugh" age: 25
users: USERNAME: Laugh" AGE: 25
users: userName: Laugh" age: 25
users: user-Name: Laugh" age: 25
users: user_Name: Laugh" age: 25