首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有多个相同类型的bean的自定义自动配置

具有多个相同类型的bean的自定义自动配置
EN

Stack Overflow用户
提问于 2019-06-14 22:36:16
回答 1查看 733关注 0票数 1

使用spring-boot-2,我想创建一个自定义的自动配置,它定义了相同类型的动态数量的bean。

例如,我使用以下ConfigurationProperties来定义AmazonS3客户端

代码语言:javascript
复制
@Setter
@Getter
@ConfigurationProperties("aws")
public class S3ClientProperties {
    private Map<String, S3Config> s3 = new HashMap<>();

    @Setter
    @Getter
    public static class S3Config {
        private String accessKey;
        private String secretKey;
        private String bucketName;
        private String region;
        // ...
    }
}

如果我有一个这样的yaml:

代码语言:javascript
复制
aws.s3:
  primary:
    bucketName: bucket1
    region: eu-west-1
  secondary:
    bucketName: bucket2
    region: eu-east-1

如果有两个AmazonS3类型的Bean注册到ApplicationContext上,一个名为primary,另一个名为secondary,那就太好了。

除了在我的@Configuration上有一个@PostContstruct,自动装配上下文并手动添加这些bean之外,还有什么更方便/更好的方法来做到这一点吗?

EN

回答 1

Stack Overflow用户

发布于 2019-06-15 02:46:38

您可以使用ConditionalOnProperty或多个条件,简单的示例(对于您的yaml):

代码语言:javascript
复制
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);

        Map<String, SomeService> someServiceBeans = context.getBeansOfType(SomeService.class);

        someServiceBeans.forEach((key, value) ->
                System.out.println("bean name: " + key + " ,bean class: " + value.getClass().getSimpleName()));
    }

    // some service
    public interface SomeService {

    }

    // service if `aws.s3.primary` property exists
    @Service("primaryService")
    @ConditionalOnProperty("aws.s3.primary.bucketName")
    public static class SomePrimaryService implements SomeService {

    }

    // service if `aws.s3.secondary` property exists
    @Service("secondaryService")
    @ConditionalOnProperty("aws.s3.secondary.bucketName")
    public static class SomeSecondaryService implements SomeService {

    }

    // service if `SomeService` bean missing ( `aws.s3.primary` & `aws.s3.secondary` not exists )
    @Service("defaultService")
    @ConditionalOnMissingBean(SomeService.class)
    public static class SomeDefaultService implements SomeService {

    }

    // service with `custom` condition and `class` condition (multiple conditions)
    @Service("customService")
    @ConditionalOnCustom
    @ConditionalOnClass(SomeDefaultService.class)
    public static class SomeCustomService implements SomeService {

    }

    // annotation for custom condition
    @Target({ElementType.TYPE, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Conditional(CustomCondition.class)
    public @interface ConditionalOnCustom {

    }

    // any custom condition
    public static class CustomCondition implements Condition {

        @Override
        public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
            // do something
            return true;
        }

    }

}

应用程序输出:

代码语言:javascript
复制
bean name: customService ,bean class: SomeCustomService
bean name: primaryService ,bean class: SomePrimaryService
bean name: secondaryService ,bean class: SomeSecondaryService

还可以看看Creating Your Own Auto-configuration

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56600323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档