@Autowired自动注入普通service很方便,如:
@Component
public class WarningMatterUtil {
//报警表
@Autowired
private IDScypWarningMatterService warningMatterService;
public void dScypWarningMatterFZ(){
}
}
但是如果注入static修饰的service则注入不了,如:
@Component
public class WarningMatterUtil {
//报警表
@Autowired
private static IDScypWarningMatterService warningMatterService;
public static void dScypWarningMatterFZ(){
}
}
后来网上百度了一下,看到了一个方法,修改后代码如下:
@Component
public class WarningMatterUtil {
//报警表
@Autowired
private IDScypWarningMatterService warningMatterService;
private static WarningMatterUtil warningMatterUtil;
@PostConstruct
public void init() {
warningMatterUtil = this;
warningMatterUtil.warningMatterService = this.warningMatterService;
}
/**
* 赋值方法
*
*/
public static void dScypWarningMatterFZ(){
}
}
标签解释
@Autowired:作用于构建器、属性、方法。按byType自动注入。(@Resource默认按 byName自动注入)
@PostConstruct:被@PostConstruct修饰的方法会在服务器加载Servle的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。(PreDestroy()方法在destroy()方法执行执行之后执行)百度解释