spring 静态注入

时间:2022-11-27 10:44:18

1、静态注入

在setter 方法修改为非 static , 然后在上面注入即可

@Component
public class WeixinConfig {
// token
public static String token = "WeiXinEnterprises";
// 随机戳
public static String encodingAESKey = "tw8ColhrtDzU6XTHJ2shUeTyuaPQkSzdvD8jYtnTIYf";
// 你的企业号ID
public static String corpId = "wx54fab583808b52f1";
// 管理组的凭证密钥,每个secret代表了对应用、通讯录、接口的不同权限;不同的管理组拥有不同的secret
public static String secret = "X9fj3D82g83D9Fl7m0gNOl5O0lP5CSjBz6wQggiwsuhZcJx76Wu58tAMUZNCh1k6";
// OAuth2 回调地址
// URLEncoder.encode("http://rainbow.idreamsky.com/", "utf-8")
public static String redirectUri = "http://m.rainbow.idreamsky.com/qyweixin/oauth2/callback"; public void setToken(String token) {
WeixinConfig.token = token;
}
public void setEncodingAESKey(String encodingAESKey) {
WeixinConfig.encodingAESKey = encodingAESKey;
} @Value("${corp_id}")
public void setCorpId(String corpId) {
if(corpId != null && !corpId.equals(""))
WeixinConfig.corpId = corpId;
} @Value("${corp_secret}")
public void setSecret(String secret) {
if(secret != null && !secret.equals(""))
WeixinConfig.secret = secret;
} @Value("${redirect_uri}")
public void setRedirectUri(String redirectUri) {
if(redirectUri != null && !redirectUri.equals(""))
WeixinConfig.redirectUri = redirectUri;
}
}

2、 利用postConstruct 保存实例

@Component
public class Constants {
public static final Integer CODE_FAILURE_VALUE = -1;
public static final Integer CODE_SUCCESS_VALUE = 0; @Resource
private LotteryConfigService lotteryConfigService; private static Constants constants; @PostConstruct
public void init(){
constants = this;
constants.lotteryConfigService = this.lotteryConfigService;
}
}

参考:

1、 《记录问题》spring注解如何读取.properties文件数据,以及spring静态变量读取.properties文件错误