This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring quatrz dynamic cron expression

francis varkey
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I am trying to implement a scheduler using quartz in spring3. The requirement is need to take value of cron expression should read from database. While trying this I am getting following exception.

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cronTrigger' defined in class path resource [Spring-Quartz.xml]: Cannot resolve reference to bean 'constructCronExpression' while setting bean property 'cronExpression'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'constructCronExpression' is defined


1) Spring-Quartz.xml

<beans xmlns=""

<bean id="runMeTask" class="test.RunMeTask" />
<!-- <bean id="constructCronExpression" class="test.CustomCronExpression" /> -->

<!-- Spring Quartz -->
<bean name="runMeJob" class="org.springframework.scheduling.quartz.JobDetailBean">

<property name="jobClass" value="test.RunMeJob" />

<property name="jobDataAsMap">
<entry key="runMeTask" value-ref="runMeTask" />


<bean id="runMeJob"
<property name="targetObject" ref="runMeTask" />
<property name="targetMethod" value="printMe" />

<!-- Simple Trigger, run every 5 seconds -->
<bean id="simpleTrigger"

<property name="jobDetail" ref="runMeJob" />
<property name="repeatInterval" value="5000" />
<property name="startDelay" value="1000" />


<!-- Cron Trigger, run every 5 seconds -->
<bean id="cronTrigger"

<property name="jobDetail" ref="runMeJob" />
<!-- <property name="cronExpression" value="0/5 * * * * ?" /> -->
<property name="cronExpression" ref="constructCronExpression"/>


<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<ref bean="runMeJob" />

<property name="triggers">
<ref bean="simpleTrigger" />


package test;

public class RunMeTask {
public void printMe() {
System.out.println("Spring 3 + Quartz 1.8.6 ~");

package test;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class RunMeJob extends QuartzJobBean {
private RunMeTask runMeTask;

public void setRunMeTask(RunMeTask runMeTask) {
this.runMeTask = runMeTask;

protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {



package test;


public class App
public static void main( String[] args ) throws Exception
new ClassPathXmlApplicationContext("Spring-Quartz.xml");

package test;
import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

public class CustomCronExpression{

public String constructCronExpression() {
String cronExpression = "0/5 * * * * ?"; //Contruct Cron Expression from the request
return cronExpression ;


Thank You

  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic