tags:

views:

52

answers:

1

Is there a way to write a Spring bean in XML so that it uses constructor which doesn't need an argument. For instance:

public class CronSchedule {
    public CronSchedule() throws Exception {
     SchedulerFactory sf = new StdSchedulerFactory();
     Scheduler sched = sf.getScheduler();
     JobDetail jd = new JobDetail("job1", "group1", CronJob.class);
     CronTrigger ct = new CronTrigger("cronTrigger", "group2", "0 * * * * ?");
     sched.scheduleJob(jd, ct);
     sched.start();
    }
}

Should I use <constructor-arg /> or I should write just bean tags without it ?

<bean name="cronSchedule" class="com.lastogat.CronSchedule">
     <constructor-arg />
    </bean>
+2  A: 

You won't need to define the constructor-arg it will pick up the constructor as there is no other.

But I would suggest to inject those dependencies you create in the constructor defining them as beans in spring rather than creating them as new instances.

pedromarce
@pedromarce, I hope you don't mind me editing your answer. I just threw in the XML that you are referring to. Feel free to rollback if you want.
Adam Paynter
@Adam, No problem at all.
pedromarce