问题:
上述内容运行时,service=null
原因
spring容器加载优先级问题,aotuwired优先级没有这些高,还没开始注入,这些资源就开始执行
解决
创建CustomApplicationContext去实现ApplicationContextAware
@Service
public class CustomApplicationContext implements ApplicationContextAware {
private static ApplicationContext applicationContextStatic;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
applicationContextStatic = applicationContext;
}
public static <T>T getBean(Class<T> clazz) {
return applicationContextStatic.getBean(clazz);
}
}
使用
// @Autowired
// private SysTestService sysTestService; autowired优先级低于定时任务,无法注入
private SysTestService sysTestService= CustomApplicationContext.getBean(SysTestService .class);
完成!!!简单吧? 喜欢点个赞