运行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
中配置石英的部分。root-context.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
表示法如何帮助确定这一点。有什么我可以解决此错误的特殊方式。 最佳答案
如您所见,您定义了两个具有相同类型的bean:
jobDetail和quartzSchedulerRuntime是类型
org.springframework.scheduling.quartz.JobDetailBean
您可以通过它们的名称(quartzSchedulerRuntime / jobDetail)来区分它们,但是如果您通过Type(... JobDetailBean)将它们自动连线,这将是模棱两可的。