Java Quartz + Spring WEB无法加载DataSource

时间:2016-12-27 04:31:43

标签: java spring-mvc datasource quartz

当我在localhost中运行我的应用程序时,我遇到了问题。我的应用程序是基于tomcat的应用程序,它使用spring web MVC构建并使用@Autowired DataSource作为数据库的连接路径。除此之外,我还有一个应用程序功能来使用Java Quartz。

DataJdbc1.class:

@Repository
public class DataJdbc1 implements Data {

  @Autowired
  public DataSource dataSource1;
  public JdbcTemplate jdbc1;

  @Autowired
  public DataSource dataSource2;
  public JdbcTemplate jdbc2;

  public void setDataSource1(DataSource dataSource1) {
     this.dataSource1 = dataSource1;
  }

  public void setDataSource2(DataSource dataSource2) {
     this.dataSource2 = dataSource2;
  }
}

application-context.xml:

<bean id="dataSource1"  class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/xxx" />
</bean>
<bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource1" />
</bean>

<bean id="dataSource2" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/xxx2" />
</bean>
<bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource2" />
</bean>

CopyTableCustomerHandleJob.class

public class CopyTableCustomerHandleJob extends QuartzJobBean{

@Autowired
QuartzServicesFactoryDao quartzServicesFactoryDao;

@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
    // TODO Auto-generated method stub
    try {
        quartzServicesFactoryDao = new QuartzServicesFactoryDaoImpl();
        quartzServicesFactoryDao.getCopyTableCustomerHandle();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

quartz-context.xml:

<!-- job -->
<bean name="copyTableCustomerHandleJob"  class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="crm.spring.main.quartz.CopyTableCustomerHandleJob"/>
<property name="jobDataMap">
    <map>
        <entry key="quartzServicesFactoryDao" value-ref="quartzServicesFactoryDao"/>
    </map>
</property>
<property name="durability" value="true" />

<!-- trigger -->
 <bean id="copyTableCustomerHandleJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="copyTableCustomerHandleJob"/>
<property name="cronExpression" value="0 0/1 * * * ?"/>
</bean>

<!-- SchedulerFactoryBean -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
   <list>
        <ref bean="copyTableCustomerHandleJob" />
   </list>
</property>
<property name="triggers">
<list>
    <ref bean="copyTableCustomerHandleJobTrigger" />
</list>
</property>
</bean>

之后,我决定在Spring WEB MVC中使用Job类,Trigger类,schedulefactory和创建quartz-context.xml来构建那个石英应用程序,遗憾的是,运行这个石英应用程序时我有问题,我无法每次初始化DataSource属性我运行石英应用程序的时间。但奇怪的是,当我试图用spring webMVC RequestMapping DataSource成功初始化时调用石英应用程序方法。

是不是有任何stackoverflow成员遇到过与我类似的问题?我必须采取什么样的解决方案来解决我的问题。非常感谢你。

1 个答案:

答案 0 :(得分:0)

Quartz作业是由Quartz创建的,而不是Spring。因此,您可以使用SpringBeanJobFactory使用spring自动自动装配石英作业。

以下网址对您有所帮助。请检查一下。

https://stackoverflow.com/a/15211030/7119031