Spring 无法在实现 Quartz Job 的类中注入 Bean [英] Spring cannot inject Bean in class that implements Quartz Job
问题描述
我正在尝试从实现 Quartz Job 的类发送电子邮件,为此我必须在类中 @Autowire IEmailService.
I am trying to send an email from a class that implements Quartz Job, in order to do that I have to @Autowire the IEmailService inside the class.
这是我用来创建 Quartz 作业的方法:
Here is the method I use to create a Quartz Job:
@Override
public Boolean sendInfoEmail(ManifestationProp manifProp, ServletRequest request) throws SchedulerException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String token = httpRequest.getHeader("jwt_token");
if(token == null) {
System.out.println("(ManifestationPropDaoImp) - TOKEN NULL");
return false;
}
String email = tokenUtils.getUsernameFromToken(token);
User user = userDao.findUserByEmail(email);
if(update(manifProp) != null) {
Scheduler sc = StdSchedulerFactory.getDefaultScheduler();
sc.start();
JobKey jobKey = new JobKey("Job_"+manifProp.getId(),"group1");
if(!sc.checkExists(jobKey)) { System.out.println("-----------------------------------------------");
System.out.println("Scheduling a Job for Manifestation Prop with ID - "+ manifProp.getId());
System.out.println("Current time - " + new Date());
System.out.println("Scheduled time - NOW" );
System.out.println("User - "+ user.getEmail());
System.out.println("Manifestation Prop - "+manifProp.getName());
JobDataMap jdm = new JobDataMap();
jdm.put("manifProp",manifProp);
jdm.put("user", user);
JobDetail jobDetail = JobBuilder.newJob(QuartzInformUser.class)
.withIdentity(jobKey)
.usingJobData(jdm)
.build();
Trigger t = TriggerBuilder.newTrigger().withIdentity("SimpleTrigger_"+manifProp.getId()).startNow().build();
sc.scheduleJob(jobDetail, t);
System.out.println("-----------------------------------------------");
}else {
System.out.println(" *** Job_"+manifProp.getId()+" already exists! *** ");
}
return true;
}else {
System.out.println("Could not update manifestation prop!");
}
return false;
}
下面是实现Job接口的类的代码:
Here is the code of the class which implements Job interface:
@Service
public class QuartzInformUser implements Job{
@Autowired
IEmailService emailService;
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
try {
JobDataMap dataMap = arg0.getJobDetail().getJobDataMap();
User user = (User)dataMap.get("user");
ManifestationProp manifProp = (ManifestationProp)dataMap.get("manifProp");
System.out.println("USER: "+user);
System.out.println("MANIFESTATION PROP: "+manifProp);
emailService.informUser(user,manifProp);
}catch(Exception e){
e.printStackTrace();
}
}
}
Quartz Job 创建得非常好,问题出在 QuartzInformUser
类中.Spring 不会将 IEmailService
注入到类中,因此字段 emailService
是 null
并且我得到以下异常:
Quartz Job gets created perfectly fine, the problem is in the class QuartzInformUser
. Spring does not inject IEmailService
into the class, therefore the field emailService
is null
and i get the following exception:
如果有人知道如何解决此问题,我将非常感谢您的帮助!提前致谢!
If anyone has any idea how can i fix this issue I would really appreciate the help! Thank you in advance!
推荐答案
您将需要使用各种 Quartz 组件的 spring 助手/实现,以便您创建的作业将由 spring 管理.
You will want to use the spring helpers/implementations of the various Quartz components, so that the jobs you create will be managed by spring.
- https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#scheduling-quartz
- http://www.baeldung.com/spring-quartz-schedule
... 是两个很好的起点(尽管您需要确保您查看的文档适合您正在使用的 spring 版本;例如 spring-boot 有一个用于调度/石英的启动器)
... are two good starting points (although you'll want to make sure the documentation you look at is appropriate for the version of spring you're using; spring-boot for example has a starter for scheduling/quartz)
这篇关于Spring 无法在实现 Quartz Job 的类中注入 Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!