普通的变量通过@Value("${app.path}")的方式就可以注入
但是对应静态方法需要用静态变量则不支持上述注入
springboot不允许上述方式注入,返回的值为null
需要通过set的方式
/** * 钉钉应用APP_KEY */ public static String APP_KEY;
@Value("${ding.appkey}")
public void setAppKey(String appkey) {
APP_KEY = appkey;
}
/**
* 获取钉钉token
* @return
* @throws RuntimeException
*/
public static String getToken() throws RuntimeException {
try {
DefaultDingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_GET_TOKKEN);
OapiGettokenRequest request = new OapiGettokenRequest();
request.setAppkey(APP_KEY);
request.setAppsecret(APP_SECRET);
request.setHttpMethod("GET");
OapiGettokenResponse response = client.execute(request);
return response.getAccessToken();
} catch (ApiException e) {
bizLogger.error("getAccessToken failed", e);
throw new RuntimeException();
}
}

通过上述方式就可以直接使用,但是需要特别注意的是,该类需要使用
@Component 将该类加载到spring容器中 否则拿不到值