使用依赖于配置文件中的变量的不同 bean 实现

ip696

我有 2 个豆子:

@Component("FierstClient")
public class FierstClient implements CryptoClient {

@Component("SecondClient")
public class SecondClient implements CryptoClient {

我有服务:

    @Component
    public class CryptoServiceImpl implements CryptoService {

        private final Marshaler marshaler;
        private final CryptoClient cryptoClient;

        public CryptoServiceImpl(Marshaler marshaler, 
                                 @Qualifier("FirstClient") CryptoClient cryptoClient) {
            this.marshaler = marshaler;
            this.cryptoClient = cryptoClient;
        }

现在我有一个任务 - 控制这个 bean 配置文件。我知道一些解决方案,但它们对我来说似乎很幼稚:

  1. 创建配置default-server: first // or secondCryptoServiceImpl注入 2 个 bean:

    @Qualifier("FirstClient") CryptoClient cryptoClientFirst @Qualifier("SecondsClient") CryptoClient cryptoClientSecond

当我使用它时写:

if(default-server equals first)...else...
  1. 创建Profile. 但是我会不会有其他配置,比如 DB 等等。回答我会有很多配置文件,比如:

FirstClientAndPosgresqlProfile FirstClientAndOracleProfile SecondClientAndPosgresqlProfile SecondClientAndOracleProfile

...

如果我有更多可更改的参数,我会有新的配置文件吗?

可能存在依赖于配置文件中的变量使用不同 bean 实现的明确解决方案?

用户10367961

你可以使用这样的东西

@Configuration
public class ClientConfig {

    @Bean(name="criptoClient")
    @ConditionalOnProperty(
      name = "enabled.client", 
      havingValue = "first")
    public CryptoClient firstClient() {
        // return first client
    }

    @Bean(name="criptoClient")
    @ConditionalOnProperty(
      name = "enabled.client",
      havingValue = "second")
    public CryptoClient secondClient() {
        // return second client
    }

    @Bean(name="criptoClient")
    @ConditionalOnProperty(
      name = "enabled.client", 
      matchIfMissing = true)
    public CryptoClient defaultClient() {
        // return default client
    }
}

您需要将该enable.client属性设置为firstsecond如果该属性不存在,则会DefaultClient被实例化。

另一种方法是将 移动到@ConditionalOnProperty您的@Component定义之上在这种情况下,您将不再需要上述@Configuration.

@Component("criptoClient")
@ConditionalOnProperty(
      name = "enabled.client", 
      havingValue = "first")
public class FierstClient implements CryptoClient {
}

@Component("criptoClient")
@ConditionalOnProperty(
      name = "enabled.client",
      havingValue = "second")
public class SecondClient implements CryptoClient {
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在BeanPostProcessor实现中获取bean的注释

如何在Spring中实现弹性bean?

使用bean.xml链接存储库以实现许多实现

xcodebuild不同的配置文件以实现目标依赖

Spring Fallback bean实现

Spring bean的Singleton实现

如何根据春季轮廓调用Bean的不同实现

Spring Boot:加载所有在测试中实现接口的bean?

Spring Framework中的原型bean作用域实现

获取所有在Spring中实现通用接口的bean

更改打包在 Jar 中的 Bean 实现的 Bean 名称 - Spring Boot

Spring Retry @Recover 方法实现prototype-bean 的Runnable 不使用prototype-bean 的字段

如何为依赖于Rust中泛型类型参数的结构的关联函数定义不同的实现?

在Grails中如何覆盖外部配置文件中的配置变量,以便依赖于该变量的变量也被更新?

在Spring javaconfig中,如何初始化依赖于@Service的@Bean

添加接口实现后对容器管理的无状态 bean 的依赖不满意

如何在带有xml配置的Spring MVC中提供默认的bean实现?

React如何实现钩子,使其依赖于调用顺序

设计模式以实现依赖于其他插件的插件

Eigen的实现是否依赖于标准容器?

依赖于haxe中泛型实现的功能的可选类代码生成

如何对依赖于属性的UML中的接口实现建模?

在Spring JavaConfig中,bean返回类型应该是接口还是实现?

我们可以在spring boot中基于rest api的路径参数实现@Conditional Bean吗?

如何在 Spring 中通过 bean 名称自动装配接口的特定实现

在一个类字段中处理一个Spring Bean /接口的几种实现

导出延迟初始化的bean(实现SelfNaming,并使用ManagedResource批注进行批注)会产生IllegalStateException

如何确定使用lambda实现通用FunctionalInterface的Bean的类型参数?

使用@Inject实例获取实现特定接口的EJB和CDI bean