运行 Spring 应用程序时出现以下异常:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of
type [org.quartz.JobDetail] is defined: expected single matching bean but found 2:
[quartzSchedulerRuntime, jobDetail]
这是我在root-context.xml
.
根上下文.xml
<bean name="quartzSchedulerRuntime" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.task.QuartzScheduler" />
<property name="jobDataAsMap">
<map>
<entry key="runtimeReportServiceImpl" value-ref="RuntimeReportService" />
</map>
</property>
</bean>
<bean id="cronTriggerRuntime"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="quartzSchedulerRuntime" />
<property name="cronExpression" value="0 30 12 ? * MON *" />
</bean>
<bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
<property name="schedulerContextAsMap">
<map>
<entry key="reportSchedulerServiceImpl" value-ref="reportSchedulerServiceImpl"></entry>
</map>
</property>
<property name="jobDetails">
<list>
<ref bean="quartzSchedulerRuntime" />
<ref bean="jobDetail"/>
</list>
</property>
<property name="triggers">
<list>
<ref bean="cronTriggerRuntime" />
<ref bean="jobTrigger"/>
</list>
</property>
</bean>
<bean id="jobDetailFactory"
class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean">
<property name="targetBeanName">
<idref local="jobDetail" />
</property>
</bean>
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean"
scope="prototype">
<property name="jobClass" value="com.scheduler.SMTPMailJob " />
<property name="jobDataAsMap">
<map>
<entry key="reportSchedulerServiceImpl" value-ref="reportSchedulerServiceImpl" />
<entry key="filterUtil" value-ref="filterUtil" />
<entry key="reportService" value-ref="reportService" />
<entry key="fusionChartHtmlToImage" value-ref="fusionChartHtmlToImage"/>
<entry key="fcproperties" value-ref="fcproperties"/>
</map>
</property>
</bean>
<bean id="jobTriggerFactory"
class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean">
<property name="targetBeanName">
<idref local="jobTrigger" />
</property>
</bean>
<bean id="jobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean" scope="prototype">
<property name="jobDetail" ref="jobDetail" />
<property name="cronExpression" value="0 0 9 1/1 * ? *" />
</bean>
编辑:我已将问题缩小到这个范围。我创建了一个名为 RScheduler 的类,它应该安排作业。在该课程中,我宣布:
Scheduler scheduler;
ObjectFactory<JobDetail> jobDetailFactory;
ObjectFactory<CronTrigger> jobTriggerFactory;
@Autowired
public ReportScheduler(Scheduler scheduler,ObjectFactory<JobDetail> jobDetailFactory,ObjectFactory<CronTrigger> jobTriggerFactory)
throws SchedulerException
{
Assert.notNull(scheduler, "scheduler cannot be null");
Assert.notNull(jobDetailFactory, "jobDetailFactory cannot be null");
Assert.notNull(jobTriggerFactory, "jobTriggerFactory cannot be null");
this.scheduler = scheduler;
this.jobDetailFactory=jobDetailFactory;
this.jobTriggerFactory=jobTriggerFactory;
this.scheduler.getContext().put("reportSchedulerServ", reportSchedulerServ);
}
我用另一种方法调用这项工作,例如:
JobDetail jobDetail = jobDetailFactory.getObject();
这是给我错误的行。
编辑2:我猜问题是我需要触发许多工作。所以我创建了
ObjectFactory<JobDetail> jobDetailFactory
通过jobDetailFactory
bean 捕获对象。我想,线
JobDetail jobDetail = jobDetailFactory.getObject()
无法确定要获取哪个 JobDetail bean,因为它有两个作业详细信息。我不明白@Qualifier
符号将如何帮助确定这一点。我可以解决此错误的任何特定方式..?