springboot获取配置文件中的内容


代码:

GrilApplication.java
1 @SpringBootApplication
2 public class GrilApplication {
3 
4     public static void main(String[] args) {
5         SpringApplication.run(GrilApplication.class, args);
6     }
7 }

application.yml

1 server:
2   port: 8089
3 gril:
4   cupSize: B
5   age: 18
GrilProperties.java
 1 @Component
 2 @ConfigurationProperties(prefix = "gril")
 3 public class GrilProperties {
 4     private String cupSize;
 5     private Integer age;
 6 
 7     public String getCupSize() {
 8         return cupSize;
 9     }
10 
11     public void setCupSize(String cupSize) {
12         this.cupSize = cupSize;
13     }
14 
15     public Integer getAge() {
16         return age;
17     }
18 
19     public void setAge(Integer age) {
20         this.age = age;
21     }
22 }
HelloController.java

@Controller
public class HelloController {

    @Autowired
    private GrilProperties grilProperties;
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public  String say(){

        return grilProperties.getCupSize();
    }
}

访问地址http://127.0.0.1:8089/hello

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM