Gandalf StormCrow
Gandalf StormCrow

Reputation: 26192

creating spring bean

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>

Upvotes: 0

Views: 301

Answers (1)

pedromarce
pedromarce

Reputation: 5669

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.

Upvotes: 2

Related Questions