gpt4 book ai didi

java - Spring Quartz 使用父类构造函数设置基于构造函数的注入(inject)

转载 作者:行者123 更新时间:2023-11-29 08:31:36 28 4
gpt4 key购买 nike

我有一个 spring boot 项目,我想使用 quartz 在特定时间运行特定作业。我有这个类布局:

abstract public class AbstractFoo {
protected final FooB fooB;

public AbstractFoo(FooB fooB) {
this.fooB = fooB;
}
}

@Service
public class SomeJob extends AbstractFoo implements Job {
public SomeJob(FooB fooB) {
super(fooB);
}

@Override
public void execute(final JobExecutionContext context) throws JobExecutionException {
//do stuff
}
}

但是,当我运行这个作业时,出现以下错误:

2017-12-06 14:18:01,383  ERROR --- [quartz-jobs] org.quartz.core.ErrorLogger                                                                : An error occured instantiating job to be executed. job= 'jobGroup.someJob' 
org.quartz.SchedulerException: Job instantiation failed
at org.springframework.scheduling.quartz.AdaptableJobFactory.newJob(AdaptableJobFactory.java:45)
at org.quartz.core.JobRunShell.initialize(JobRunShell.java:127)
at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:375)
Caused by: java.lang.InstantiationException: com.jobs.SomeJob
at java.lang.Class.newInstance(Class.java:427)
at org.springframework.scheduling.quartz.AdaptableJobFactory.createJobInstance(AdaptableJobFactory.java:58)
at org.springframework.scheduling.quartz.SpringBeanJobFactory.createJobInstance(SpringBeanJobFactory.java:74)
at com.config.AutowiringSpringBeanJobFactory.createJobInstance(AutowiringSpringBeanJobFactory.java:27)
at org.springframework.scheduling.quartz.AdaptableJobFactory.newJob(AdaptableJobFactory.java:41)
... 2 common frames omitted
Caused by: java.lang.NoSuchMethodException: com.jobs.SomeJob.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 6 common frames omitted

我已经有一个像这样的 Autowiring 工厂了:

public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements
ApplicationContextAware {

private transient AutowireCapableBeanFactory beanFactory;

@Override
public void setApplicationContext(final ApplicationContext context) {
beanFactory = context.getAutowireCapableBeanFactory();
}

@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
final Object job = super.createJobInstance(bundle);
beanFactory.autowireBean(job);
return job;
}
}

这是我的 quartz 配置:

@Configuration
public class QuartzConfig {
@Autowired
private DataSource dataSource;
@Autowired
private PlatformTransactionManager transactionManager;
@Autowired
private ApplicationContext applicationContext;

@Bean
public SchedulerFactoryBean quartzScheduler() {
SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();

quartzScheduler.setDataSource(dataSource);
quartzScheduler.setTransactionManager(transactionManager);
quartzScheduler.setOverwriteExistingJobs(true);
quartzScheduler.setSchedulerName("quartz-jobs");

AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
quartzScheduler.setJobFactory(jobFactory);

quartzScheduler.setQuartzProperties(quartzProperties());

Trigger[] triggers = {someJobTrigger().getObject();
quartzScheduler.setTriggers(triggers);

return quartzScheduler;
}

@Bean
public CronTriggerFactoryBean someJobTrigger() {
CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
cronTriggerFactoryBean.setJobDetail(someJob().getObject());
cronTriggerFactoryBean.setCronExpression(cronExp);
cronTriggerFactoryBean.setGroup(someGroup);
return cronTriggerFactoryBean;
}

@Bean
public JobDetailFactoryBean someJob() {
JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
jobDetailFactory.setJobClass(SomeJob.class);
jobDetailFactory.setGroup(someGroup);
jobDetailFactory.setDurability(true);
return jobDetailFactory;
}

@Bean
public Properties quartzProperties() {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
Properties properties = null;
try {
propertiesFactoryBean.afterPropertiesSet();
properties = propertiesFactoryBean.getObject();
} catch (IOException e) {
}

return properties;
}
}

如何让 Quartz 通过构造函数连接适当的依赖项?

最佳答案

docs 中所述:

One of the ramifications of this behavior is the fact that jobs must have a no-argument constructor (when using the default JobFactory implementation).

您基本上使用默认的 JobFactory 添加了 Autowiring 功能。我从个人测试中发现, Autowiring 不适用于构造函数注入(inject)。此外,不要使用 Spring 注释(组件、服务、e.c.t)标记您的作业,因为它没有任何效果。

要解决您的问题,请重构您的作业,使其具有默认构造函数和 Autowiring 依赖项以及字段注入(inject)(也许 setter 注入(inject)也可以)。

abstract public class AbstractFoo {
@Autowired
protected FooB fooB;
}

public class SomeJob extends AbstractFoo implements Job {

@Override
public void execute(final JobExecutionContext context) throws JobExecutionException {
//do stuff
}
}

关于java - Spring Quartz 使用父类构造函数设置基于构造函数的注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47683071/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com