在static变量上使用@Value和@Autowired注解
之前在使用@Value和@Autowired注解为static变量注入值时运行总是报空指针,经过在网上查找后找到解决办法
解决办法:
将注解加在变量setter方法上,注意要将setter方法的static去掉
private static String username;
@Value("${username}")
public void setUsername(String username) {
this.username = username;
}
private static HDFSAgent hdfsAgent;
@Autowired
public void setHdfsAgent(HDFSAgent hdfsAgent) {
this.hdfsAgent = hdfsAgent;
}