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:
开发者_JS百科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>
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.
精彩评论