Spring在多个@Configuration文件中配置相同的bean

时间:2016-12-14 13:58:45

标签: java spring spring-boot quartz-scheduler

当我需要在我的核心模块@Configuration文件中配置一些bean,而不是在顶级模块@Configuration文件中完成核心模块bean的配置时,我遇到了这种情况。 (请参阅下面的代码

如何在facade的@Configuration文件中完成核心模块的SchedulerFactoryBean的设置?

核心模块调度程序配置:

@Configuration
public class BaseSchedulerConfig {

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource,
                                                     JobFactory jobFactory,
                                                     @Qualifier("baseReusableJob") JobDetail baseReusableJob) {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setJobDetails(baseReusableJob);
        return factory;
    }
}

Facade模块调度程序配置:

@Configuration
@Import(BaseSchedulerConfig.class)
public class FacadeSchedulerConfig {

    @Autowire
    public SchedulerFactoryBean schedulerFactoryBean;

    // Question: which annotation use to inject module (facade) specific jobs into SchedulerFactoryBean
    public void lookup(@Qualifier("facadeSpecificJob") JobDetail facadeSpecificJob) {
        schedulerFactoryBean.setJobDetails(facadeSpecificJob);
    }
}

1 个答案:

答案 0 :(得分:1)

您正在寻找@PostConstruct

  

JSR-250 @PostConstruct和@PreDestroy注释通常被认为是在现代Spring应用程序中接收生命周期回调的最佳实践。

按预期使用@Configuration。

  

What is the expected behaviour of @PostConstruct in @Configuration classes?