java定时任务无法用@Autowired注入BEAN,需要用implements ApplicationContextAware 的工具类获取spring上下文 的数据
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
@Component
@Lazy(false)
public class SpringContextUtil implements ApplicationContextAware, DisposableBean {
private static final Logger log = LoggerFactory.getLogger(SpringContextUtil.class);
private static ApplicationContext applicationContext = null;
public SpringContextUtil() {
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextUtil.applicationContext = applicationContext;
}
public static <T> T getBean(String name) {
return applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> requiredType) {
return applicationContext.getBean(requiredType);
}
public static void clearHolder() {
if (log.isDebugEnabled()) {
log.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
}
applicationContext = null;
}
public static void publishEvent(ApplicationEvent event) {
if (applicationContext != null) {
applicationContext.publishEvent(event);
}
}
public void destroy() {
try {
clearHolder();
} catch (Throwable var2) {
throw var2;
}
}
}