要对静态变量进行注入
@Resource
private static ITecharchService techarchService;//注入为空,spring注解注入不支持静态变量注入
查资料找到可以这样注入
@Component //必须是组件,才可以用@PostConstruct
public class DDUtils{
@Resource
private ITecharchService techarchService;
private static DDUtils dd;
@PostConstruct
public void init() {
dd = this;
dd.techarchService= this.techarchService;
}
public void getA(){
//调用
dd.techarchService.getA();
}
}
本文介绍了一种通过使用@Component和@PostConstruct注解实现静态变量注入的方法。这种方法绕过了Spring框架直接支持静态变量注入的限制,并提供了示例代码来说明如何在实际应用中实施。
992

被折叠的 条评论
为什么被折叠?



