飞道的博客

Spring Boot读取 properties或者application.yml 配置文件中的数据

400人阅读  评论(0)

Spring Boot读取配置的 5 种方式

读取application文件

在application.yml或者properties文件中添加:

  • 
        
    1. user.address=china
    2. user.company=demo
    3. user.name=让我康康

     

 1、使用@Value注解读取

     直接 代码如下:


  
  1. package im.homeapi.controller;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.omg.CORBA.PUBLIC_MEMBER;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. @RestController
  8. @RequestMapping(value= "/api")
  9. public class HomeController {
  10. @Value( "${user.address}")
  11. private String address;
  12. @Value( "${user.company}")
  13. private String company;
  14. @Value( "${user.name}")
  15. private String name;
  16. //value 指定访问地址,method 指定请求类型
  17. @RequestMapping(value = "/home",method = RequestMethod.GET)
  18. public String Home()
  19. {
  20. return "Hello Word";
  21. }
  22. @RequestMapping(value = "/getConfig")
  23. public String getConfig() {
  24. return "获取的配置信息 :" +
  25. " name=" + name +
  26. " address=" + address +
  27. " , company=" + company;
  28. }
  29. }

 放到单独的配置类中读取:


  
  1. package im.homeapi.entity;
  2. import org.springframework.beans.factory. annotation.Value;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class UserConfig {
  6. @Value("${user.address}")
  7. private String address;
  8. @Value("${user.company}")
  9. private String company;
  10. public String getAddress() {
  11. return address;
  12. }
  13. public void setAddress(String address) {
  14. this.address = address;
  15. }
  16. public String getCompany() {
  17. return company;
  18. }
  19. public void setCompany(String company) {
  20. this.company = company;
  21. }
  22. public String getName() {
  23. return name;
  24. }
  25. public void setName(String name) {
  26. this.name = name;
  27. }
  28. @Value("${user.name}")
  29. private String name;
  30. }

调用如下:


  
  1. @Autowired
  2. private UserConfig userConfig;
  3. //读取配置类
  4. @RequestMapping(value = "/getConfigEntity")
  5. public String getConfigEntity() {
  6. return "获取的配置信息 :" +
  7. " name=" + userConfig.getName() +
  8. " address=" + userConfig.getAddress() +
  9. " , company=" + userConfig.getCompany();
  10. }

   运行结果如下:

 2、使用@ConfigurationProperties注解读取方式

代码如下:


  
  1. package im.homeapi.entity;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. @ConfigurationProperties(prefix = "user")
  6. public class UserConfig1 {
  7. private String address;
  8. private String company;
  9. private String name;
  10. public String getAddress() {
  11. return address;
  12. }
  13. public void setAddress(String address) {
  14. this.address = address;
  15. }
  16. public String getCompany() {
  17. return company;
  18. }
  19. public void setCompany(String company) {
  20. this.company = company;
  21. }
  22. public String getName() {
  23. return name;
  24. }
  25. public void setName(String name) {
  26. this.name = name;
  27. }
  28. }

调用:


  
  1. package im.homeapi.controller;
  2. import im.homeapi.entity.UserConfig;
  3. import im.homeapi.entity.UserConfig1;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.omg.CORBA.PUBLIC_MEMBER;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. @RestController
  11. @RequestMapping(value= "/api")
  12. public class ConfigController {
  13. @Autowired
  14. private UserConfig1 userConfig;
  15. //读取配置类 ConfigurationProperties注解读取方式
  16. @RequestMapping(value = "/getConfigEntity1")
  17. public String getConfigEntity() {
  18. return "获取的配置信息 :" +
  19. " name=" + userConfig.getName() +
  20. " address=" + userConfig.getAddress() +
  21. " , company=" + userConfig.getCompany();
  22. }
  23. }

运行结果:

3、读取指定文件

     3.1、@PropertySource+@Value注解读取方式

     在resources下新建配置config/db-config.properties 

     注意:@PropertySource不支持yml文件读取。


  
  1. db.username=root
  2. db.password= 123456

如图:

代码:


  
  1. package im.homeapi.entity;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.context.annotation.PropertySource;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. @PropertySource(value = { "config/db-config.properties" })
  7. public class DBConfig {
  8. @Value( "${db.username}")
  9. private String username;
  10. @Value( "${db.password}")
  11. private String password;
  12. public String getUsername() {
  13. return username;
  14. }
  15. public void setUsername(String username) {
  16. this.username = username;
  17. }
  18. public String getPassword() {
  19. return password;
  20. }
  21. public void setPassword(String password) {
  22. this.password = password;
  23. }
  24. }

调用代码:


  
  1. package im.homeapi.controller;
  2. import im.homeapi.entity.DBConfig;
  3. import im.homeapi.entity.UserConfig;
  4. import im.homeapi.entity.UserConfig1;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.omg.CORBA.PUBLIC_MEMBER;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. @RestController
  12. @RequestMapping(value= "/api")
  13. public class DbController {
  14. @Autowired
  15. private DBConfig dbConfig;
  16. //读取配置类 PropertySource+@Value注解读取方式
  17. @RequestMapping(value = "/getConfigdb")
  18. public String getConfigdb() {
  19. return "获取的配置信息 :" +
  20. " name=" + dbConfig.getUsername() +
  21. " , password=" + dbConfig.getPassword();
  22. }
  23. }

运行结果:

3.2、@PropertySource+@ConfigurationProperties注解读取方式

   代码:


  
  1. package im.homeapi.entity;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.context.annotation.PropertySource;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. @ConfigurationProperties(prefix = "db")
  7. @PropertySource(value = { "config/db-config.properties" })
  8. public class DBconfig1 {
  9. private String username;
  10. private String password;
  11. public String getUsername() {
  12. return username;
  13. }
  14. public void setUsername(String username) {
  15. this.username = username;
  16. }
  17. public String getPassword() {
  18. return password;
  19. }
  20. public void setPassword(String password) {
  21. this.password = password;
  22. }
  23. }

调用代码:


  
  1. @Autowired
  2. private DBconfig1 dbConfig1;
  3. //读取配置类 @PropertySource+@ConfigurationProperties注解读取方式
  4. @RequestMapping(value = "/getConfigdb1")
  5. public String getConfigdb1() {
  6. return "获取的配置信息 :" +
  7. " name=" + dbConfig1.getUsername() +
  8. " , password=" + dbConfig1.getPassword();
  9. }

运行结果:

@Component 表示将该类标识为Bean

@ConfigurationProperties(prefix = "db")用于绑定属性,其中prefix表示所绑定的属性的前缀。

@PropertySource(value = "config/db-config.properties")表示配置文件路径。

4、使用Environment读取

代码:


  
  1. @Autowired
  2. private Environment environment;
  3. //读取配置类 CEnvironment读取方式
  4. @RequestMapping(value = "/getConfigenv")
  5. public String getConfigenv() {
  6. return "获取的配置信息 :" +
  7. " name=" + environment.getProperty( "user.name") +
  8. " address=" + environment.getProperty( "user.address") +
  9. " , company=" + environment.getProperty( "user.company");
  10. }

运行结果:

总结

从以上示例来看,Spring Boot可以通过@PropertySource,@Value,@Environment,@ConfigurationProperties来绑定变量。


转载:https://blog.csdn.net/qq_27462223/article/details/106763419
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场