当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Boot 注入外部配置到应用内部的静态变量

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

2018年07月13日  | 移动技术网IT编程  | 我要评论

陈云的子女,heatseek,屈玥个人资料

属性配置类 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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网