springboot 之 注入资源给静态字段

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38361347/article/details/85255511

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省略
}

猜你喜欢

转载自blog.csdn.net/weixin_38361347/article/details/85255511
今日推荐