springboot 之 注入资源给静态字段

37 阅读1分钟

springboot 注入资源给静态字段

非静态字段可以这样子注入

    @Value("${weChat.token}")
private  String token;
    @Value("${weChat.appid}")
private  String appid;
    @Value("${weChat.scope}")
private  String scope;
    @Value("${weChat.appsecret}")
private  String appsecret;

静态字段采用以上注入值会为null

sringboot支持下面注入setter注入

@Component
public class ParamConfig {

    private static String token;

    private static String appid;

    private static String scope;

    private static String appsecret;


    @Value("${weChat.token}")
    public void setToken(String token) {
        this.token = token;
    }


    @Value("${weChat.appid}")
    public void setAppid(String appid) {
        this.appid = appid;
    }


    @Value("${weChat.scope}")
    public void setScope(String scope) {
        this.scope = scope;
    }


    @Value("${weChat.appsecret}")
    public void setAppsecret(String appsecret) {
        this.appsecret = appsecret;
    }
	/* get省略
}