美文网首页springboot程序员
Spring Boot 注入外部配置到应用内部的静态变量

Spring Boot 注入外部配置到应用内部的静态变量

作者: Anoyi | 来源:发表于2018-07-11 17:50 被阅读0次

    属性配置类 StaticProperties.class

    @Component
    public class StaticProperties {
    
        public static String CUSTOM_NAME;
    
        @Value("${custom.name}")
        public void setCustomName(String customName) {
            CUSTOM_NAME = customName;
        }
    
    }
    

    Spring Boot 配置提示 resources/META-INF/spring-configuration-metadata.json

    {
      "properties": [
        {
          "name": "custom.name",
          "type": "java.lang.String",
          "sourceType": "com.anoyi.xxx.config.StaticProperties"
        }
      ]
    }
    

    Spring Boot 配置 application.properties

    custom.name=anoyi
    

    至此,即可在 Spring Boot 全局任意引用 StaticProperties.CUSTOM_NAME

    相关文章

      网友评论

        本文标题:Spring Boot 注入外部配置到应用内部的静态变量

        本文链接:https://www.haomeiwen.com/subject/baxkpftx.html