在 JobDetailBean 中注入对作业 class 的 bean 引用

To Inject bean reference to a job class in JobDetailBean

我正在使用 spring 批处理和 quartz 调度程序每 5 秒触发一次作业。

<bean name="JobA" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="com.batch.test.FirstJob" />
</bean>

这里不是传递 class 本身,是否可以传递 bean 的引用?

<bean id="firstJob" class="com.batch.test.FirstJob">
        <property name="sessionFactory" ref="sessionFactory"> </property>
    </bean>
<bean name="JobA" class="org.springframework.scheduling.quartz.JobDetailBean">
            <property name="jobClass" ref="firstJob" />
    </bean>

这可能吗?

是的。这完全取决于您的用例。

考虑下面的 class :

  public ClassA{

      private Class clazz;
      private FirstJob firstJob;

      public Class getClazz() {
          return clazz;
      }

      public void setClazz(Class clazz) {
          this.clazz = clazz;
      }

      public FirstJob getFirstJob() {
          return firstJob;
      }

      public void setFirstJob(FirstJob firstJob) {
         this.firstJob = firstJob;
      }
}

依赖注入:

 <bean id="firstJob" class="com.batch.test.FirstJob">
    <property name="sessionFactory" ref="sessionFactory"> </property>
</bean>

<bean name="JobA" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="clazz" value="com.batch.test.FirstJob" />
        <property name="firstJob" ref="firstJob" />
</bean>

终于知道要做什么了。

<bean name="JobA" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass">
            <bean factory-bean="firstJob" factory-method="getClass"></bean>
        </property>
</bean>

希望有一天它会对某人有用:)