第一种:
application.yml配置中的参数:
zip: Hello Springboot
方法读取:
@RestController public class ControllerTest { //在这里读取配置文件 @Value("${zip}") private String zip; @GetMapping(value = "hello") public String hello(){ return zip; } }
第一种比较麻烦,推荐第二种:
首先配置文件application.yml:
girl: cupSize: D age: 18
配置类:Testconfig.java
package org.kaiyuan.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; //获取前缀为“girl”的配置 @Component @ConfigurationProperties(prefix = "girl") public class Testconfig { private String cupSize; private String age; public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
最后使用:
@RestController public class ControllerTest { //在这里读取配置文件 @Autowired private Testconfig testconfig; @GetMapping("/hello") public String hello(){ return testconfig.getCupSize() + testconfig.getAge(); } }
原文地址:https://www.cnblogs.com/suiyisuixing/p/8485429.html
时间: 2024-10-08 09:28:40