Spring 弹簧&x2B;Quartz:@PostConstruct方法被调用两次

Spring 弹簧&x2B;Quartz:@PostConstruct方法被调用两次,spring,scheduled-tasks,quartz-scheduler,Spring,Scheduled Tasks,Quartz Scheduler,我已经研究了这个问题,以寻求解决我的问题的方法,但没有任何方法可以帮助我解决这个问题,或者可能我不完全理解这个问题的解决方法 我正在执行多个Quartz计划的电子邮件任务,这些任务应该在应用程序启动时运行,以及在运行时动态计划、重新计划和未计划。下面是我的配置类 @Configuration @ComponentScan(basePackages="de.it2media.dps.statistics") public class AppConfig{ @Autowired priva

我已经研究了这个问题,以寻求解决我的问题的方法,但没有任何方法可以帮助我解决这个问题,或者可能我不完全理解这个问题的解决方法

我正在执行多个Quartz计划的电子邮件任务,这些任务应该在应用程序启动时运行,以及在运行时动态计划、重新计划和未计划。下面是我的配置类

@Configuration
@ComponentScan(basePackages="de.it2media.dps.statistics")
public class AppConfig{

    @Autowired private AutowireCapableBeanFactory autowireCapableBeanFactory;

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(){
         SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
         schedulerFactoryBean.setJobFactory(autowiringSpringBeanFactory());

         return schedulerFactoryBean;
    }

    @Bean
    public SpringBeanJobFactory autowiringSpringBeanFactory(){      
        return new SpringBeanJobFactory(){
            @Override
            protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
                 Object job = super.createJobInstance(bundle);
                 autowireCapableBeanFactory.autowireBean(job);

                 return job;
            }
        };
    }

    @Bean
    public Scheduler scheduler() throws SchedulerException{
        Scheduler scheduler = schedulerFactoryBean().getScheduler();

        return scheduler;
    }
}
我的
@Controller
类中有一个
@PostConstruct
方法,如下所示

@Controller
@RequestMapping("/task")
public class TaskController extends BaseRemoteService implements TaskService {

@Autowired Scheduler scheduler;

@PostConstruct
private void afterPropertiesSet() throws Exception {
    List<EmailTask> taskList;

    taskList = getSavedTasks();

    for (EmailTask emailTask : taskList) {
        if (!scheduler.checkExists(new JobKey(emailTask.getTaskKey()))) {
        JobDetail jobDetail = JobBuilder.newJob(EmailCronJob.class)
                .withIdentity(emailTask.getTaskKey(), "emailTaskGroup").storeDurably(false).build();

        jobDetail.getJobDataMap().put("emailTask", emailTask);

        CronTrigger cronTrigger = TriggerBuilder.newTrigger().forJob(jobDetail)
                .withIdentity(emailTask.getTaskKey(), "emailTaskGroup")
                .withSchedule(CronScheduleBuilder.cronSchedule(emailTask.getCronExpression())).build();

        scheduler.scheduleJob(jobDetail, cronTrigger);
    }
    }

}
}
我有另一个从Spring导入MVC配置的配置文件,但到目前为止,我还不需要在其中添加DelareBeans。我不知道这是否真的很重要,但事情是这样的

@Configuration
@ComponentScan(basePackages="de.it2media.dps.statistics.server.controller")
@EnableWebMvc
public class MVCConfig {}
因此,正如标题中提到的,问题在于
@PostConstruct
方法被调用了两次。同样的Quartz作业都尝试创建两次,我得到以下异常

[INFO] 2016/10/06 13:42:27.505 WARN [main] AnnotationConfigWebApplicationContext:549 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'taskController': Invocation of init method failed; nested exception is org.quartz.ObjectAlreadyExistsException: Unable to store Job : 'emailTaskGroup.423946369', because one already exists with this identification.
    [INFO] 2016/10/06 13:42:27.510 ERROR [main] DispatcherServlet:502 - Context initialization failed
    [INFO] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'taskController': Invocation of init method failed; nested exception is org.quartz.ObjectAlreadyExistsException: Unable to store Job : 'emailTaskGroup.423946369', because one already exists with this identification.
    [INFO]  at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    [INFO]  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    [INFO]  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1575) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    [INFO]  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    [INFO]  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    [INFO]  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    [INFO]  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    [INFO]  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    [INFO]  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    [INFO]  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    [INFO]  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    [INFO]  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    [INFO]  at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    [INFO]  at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    [INFO]  at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
我认为这个问题与
AutowireCapableBeanFactory
有关,我需要为我的Quartz调度程序类提供应用程序上下文,但我不确定


我完全被难住了。

@m.戴纳姆是对的。我所做的唯一一件事就是从AppConfig.java中的
@ComponentScan
中排除控制器包,它就可以工作了

@ComponentScan(basePackages="de.it2media.dps.statistics", 
    excludeFilters = { @Filter(Configuration.class), @Filter(Controller.class) })

@迪纳姆先生是对的。我所做的唯一一件事就是从AppConfig.java中的
@ComponentScan
中排除控制器包,它就可以工作了

@ComponentScan(basePackages="de.it2media.dps.statistics", 
    excludeFilters = { @Filter(Configuration.class), @Filter(Controller.class) })

@ComponentScan(basePackages=“de.it2media.dps.statistics”)
还包括
@ComponentScan(basePackages=“de.it2media.dps.statistics.server.controller”)
。是的,但是我能做什么呢?您的根上下文应该扫描除
@controller
之外的所有内容。在其上放置一个排除筛选器。
@ComponentScan(basePackages=“de.it2media.dps.statistics”)
还包括
@ComponentScan(basePackages=“de.it2media.dps.statistics.server.controller”)
。是的,但是我能做什么呢?您的根上下文应该扫描除
@controller
之外的所有内容。在其上放置排除筛选器。