您的位置:首页 > 编程语言 > Java开发

Spring 如何注入值到静态变量

2016-03-31 20:16 309 查看


Spring 如何注入值到静态变量


Spring不允许直接将值注册到静态变量中,可以间接完成


在任意类中注入配置文件中的常量

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
public class StrongTest implements InitializingBean {

public static String STRONG_TEST;

@Value("${strong.url.strongTest}")
private String strongTest;

@Override
public void afterPropertiesSet() throws Exception {
STRONG_TEST= strongTest;
}
}

配置文件
strong.url.strongTest= xxx

执行属性为
Constructor > @PostConstruct > InitializingBean > init-method
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: