如何从属性文件中获取所有密钥?

时间:2016-04-11 18:19:35

标签: spring-mvc spring-boot

我正在开发spring boot应用程序,我有一个属性文件,我正在阅读下面的属性文件

     @Configuration
     @ConfigurationProperties(locations = "classpath:mail.properties", prefix = "mail")
     public class MailConfiguration {

       public static class Smtp {
        private boolean auth;
         private boolean starttlsEnable;

           // ... getters and setters
         }

      @NotBlank
     private String host;
     private int port;  
     private String from;
     private String username;
      ..............
  }

邮件.properites

mail.host=localhost
mail.port=25
mail.smtp.auth=false
mail.smtp.starttls-enable=false
mail.from=me@localhost

这很好用,但是我没有逐个读取属性,而是想从属性文件中获取所有属性键,我怎么能得到它。

1 个答案:

答案 0 :(得分:0)

使用Map。像:(它的“伪代码” - 可能包含拼写错误或其他东西,只是为了向你展示这个想法)

@Configuration
@ConfigurationProperties(locations = "classpath:mail.properties", prefix = "mail")
public static class MailConfiguration {

    private Map<String, Object> mail = new HashMap<String, Object>();

    public Map<String, Object> getMail() {
        return this.mail;
    }
}

应该做的工作。

此致

相关问题