SpringBoot项目给静态变量static注入@Value

 

 

@value不能直接注入值给静态属性,spring 不允许/不支持把值注入到静态变量中;spring支持set方法注入,我们可以利用非静态setter 方法注入静态变量

 

 

@Component
public class SendMessageUtil {
   

    public static String accessKeyId ;



    @Value("${accessKeyId}")
    public  void setAccessKeyId(String accessKeyId) {
        SendMessageUtil.accessKeyId = accessKeyId;
    }


    public static String accessKeySecret ;

    @Value("${accessKeySecret}")
    public  void setAccessKeySecret(String accessKeySecret) {
        SendMessageUtil.accessKeySecret = accessKeySecret;
    }

    


}

 

posted @ 2021-03-17 11:39  yvioo  阅读(2383)  评论(0编辑  收藏  举报