当前位置:   article > 正文

Springboot注解@Value读取配置文件参数_spring value获取参数

spring value获取参数

1、读取application.ym文件配置直接在类属性上@Value("${application.yml里的字段}");

2、读取自定义Properties文件的配置:

    在类上使用@PropertySource(可同时读取多个properties文件);

例子:

自定义两个properties文件:

wkx.properties:

wkx2.properties:

Prop实体类:

  1. @Component
  2. @PropertySource(value={"classpath:wkx.properties","classpath:wkx2.properties"})
  3. //@PropertySource("classpath:wkx.properties")
  4. public class Prop {
  5. @Value("${my.wkx.name}")
  6. private String name;
  7. @Value("${demo.password}")
  8. private String password;
  9. @Value("${my.wkx2.address}")
  10. private String address;
  11. public String getAddress() {
  12. return address;
  13. }
  14. public void setAddress(String address) {
  15. this.address = address;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23. public String getPassword() {
  24. return password;
  25. }
  26. public void setPassword(String password) {
  27. this.password = password;
  28. }
  29. }

测试接口:

  1. @RestController
  2. @RequestMapping("/wkx1")
  3. public class PropController {
  4. @Autowired
  5. private Prop prop;
  6. @GetMapping("/test")
  7. public String show() {
  8. return "username" +":"+ prop.getName() +"--"+ "password" +":"+ prop.getPassword()+"--"+
  9. "address"+":"+prop.getAddress();
  10. }
  11. }

测试结果:

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号