我有一个Dropwizard应用程序,需要为配置列表中的每个配置生成十几个豆子。比如像健康检查,石英调度等Spring - 以编程方式生成一组bean
事情是这样的:
@Component
class MyModule {
@Inject
private MyConfiguration configuration;
@Bean
@Lazy
public QuartzModule quartzModule() {
return new QuartzModule(quartzConfiguration());
}
@Bean
@Lazy
public QuartzConfiguration quartzConfiguration() {
return this.configuration.getQuartzConfiguration();
}
@Bean
@Lazy
public HealthCheck healthCheck() throws SchedulerException {
return this.quartzModule().quartzHealthCheck();
}
}
我MyConfiguration的多个实例,所有的豆类需要这样。 现在我必须复制并粘贴这些定义,并为每个新配置重命名它们。
我可以以某种方式迭代我的配置类并为每个配置类生成一组bean定义?
我会很好的一个子类解决方案或类型安全的任何东西,而不会让我复制和粘贴相同的代码,并重新命名方法有史以来我必须添加一个新的服务。
编辑:我要补充一点,我必须依赖于这些豆类其他组件(它们注入Collection<HealthCheck>
例如)
要么你需要用'BeanDefinitionRegistryPostProcessor'注册bean定义或者做一些上下文结构魔术(独立的环境中为用户的模块,其中的依赖关系可以在父上下文中自行注册),或只是做一个服务查找,而不是春天让以注入依赖关系(即'ApplicationContext#getBeansOfType')。 – 2015-02-12 22:33:10