开发过程中可以需要注入静态变量,但是如果使用常规方式,直接在静态变量上面使用@Autowired注解注入是不成功的,使用时报空指针异常,如下:
常用的两种解决方式:
一、使用构造方法注入(注:spring中这种方式可能出现循环依赖错误)
@Component
public class TestBean {
public TestBean(){}
// 静态变量
private static DictService dictService;
// 构造方法注入静态变量
@Autowired
public TestBean(DictService dictService){
TestBean.dictService = dictService;
}
public static void getDict(String type) throws Exception {
// 注入成功后,在静态方法里面使用静态变量
dictService.getDict(type);
}
}
二、set方法注入(推荐使用)
@Component
public class TestBean {
// 静态变量
private static DictService dictService;
// 构造方法注入静态变量
@Autowired
public void setDictService(DictService dictService){
TestBean.dictService = dictService;
}
public static void getDict(String type) throws Exception {
// 注入成功后,在静态方法里面使用静态变量
dictService.getDict(type);
}
}