@Value
注解直接注入到你的bean中,经过Spring的Environment
抽象访问,或者经过@ConfigurationProperties
绑定到结构化对象。那么如何进行Spring Boot 注入外部配置到应用内部的静态变量呢?操做以下:StaticProperties.class
@Component
public class StaticProperties {
public static String CUSTOM_NAME;
@Value("${custom.name}")
public void setCustomName(String customName) {
CUSTOM_NAME = customName;
}
}复制代码
resources/META-INF/spring-configuration-metadata.json
{
"properties": [
{
"name": "custom.name",
"type": "java.lang.String",
"sourceType": "com.anoyi.xxx.config.StaticProperties"
}
]
}复制代码
application.properties
custom.name=anoyi
复制代码
至此,便可在 Spring Boot 全局任意引用 StaticProperties.CUSTOM_NAME
java
© 著做权归做者全部,转载或内容合做请联系做者面试
● 将 HTML 转化为 PDF新姿式spring
● Java 使用 UnixSocket 调用 Docker APIjson
● Fastjson致命缺陷app
● Service Mesh - gRPC 本地联调远程服务框架
● Fastjson致命缺陷命令行
● Spring Boot 2 集成log4j2日志框架3d
● Spring Security 实战干货:如何保护用户密码
● Spring Boot RabbitMQ - 优先级队列
本文由博客一文多发平台 OpenWrite 发布!