gpt4 book ai didi

java - org.springframework.beans.factory.NoSuchBeanDefinitionException : No unique bean of type [org.quartz.JobDetail]已定义

转载 作者:行者123 更新时间:2023-12-01 04:19:04 26 4
gpt4 key购买 nike

运行 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中配置quartz的部分.

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 的类,用于安排作业。在该类(class)中我已经声明:

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)来区分它们,但如果您通过类型(...JobDetailBean) Autowiring 它们,那就很模糊了。

关于java - org.springframework.beans.factory.NoSuchBeanDefinitionException : No unique bean of type [org.quartz.JobDetail]已定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19155922/

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