开发者

creating spring bean

开发者 https://www.devze.com 2022-12-09 15:43 出处:网络
Is there a way to write a Spring bean in XML so that it uses constructor which doesn\'t need an argument.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消