代码之家  ›  专栏  ›  技术社区  ›  Zed

用两种可能的实现定义bean

  •  3
  • Zed  · 技术社区  · 7 年前

    到目前为止,我有一个非常简单的bean定义,如下所示:

    @Bean
    @Conditional(value=ConditionClass.class)
    SomeInterface myMethodImpl(){
        return new ImplementationOne();
    }
    

    不过,我现在遇到了添加了其他实现类的情况,我们称之为 ImplementationTwo ,需要使用 ImplementationOne 在配置文件中启用该选项时。

    所以我需要的是这样的东西:

    @Bean
    @Conditional(value=ConditionClass.class)
    SomeInterface myMethodImpl(){
        return context.getEnvironment().getProperty("optionEnabled") ? new 
       ImplementationOne() : new ImplementationTwo();
    }
    

    基本上是一种基于配置值在bean定义时实例化正确实现的方法。这有可能吗?有人能举个例子吗?谢谢

    2 回复  |  直到 7 年前
        1
  •  2
  •   sirain    7 年前

    可以不使用 @Conditional .

    假设你有一个接口 SomeInterface ImplOne ImplTwo

    SomeInterface.java

    public interface SomeInterface {
        void someMethod();
    }
    

    ImplOne.java

    public class ImplOne implements SomeInterface{
        @Override
        public void someMethod() {
           // do something
        }
    }
    

    ImplTwo.java

    public class ImplTwo implements SomeInterface{
        @Override
        public void someMethod() {
           // do something else
        }
    }
    

    然后可以控制在如下配置类中使用的实现:

    MyConfig.java

    @Configuration
    public class MyConfig {
    
        @Autowired
        private ApplicationContext context;
    
        @Bean
        public SomeInterface someInterface() {
            if (this.context.getEnvironment().getProperty("implementation") != null) {
                return new ImplementationOne();
            } else {
                return new ImplementationTwo();
            }
        }
    }
    

    确保spring的组件扫描找到 MyConfig . 那你可以用 @Autowired 在代码中的任何其他地方注入正确的实现。

        2
  •  1
  •   sirain    7 年前

    @Conditional() 在实现上,而不是在接口上。

    您将在代码中使用的接口。

    public interface MyInterface {
       void myMethod();
    }
    

    第一次实施:

    MyInterfaceImplOne.java文件

    @Bean
    @Conditional(MyInterfaceImplOneCondition.class)
    public class MyInterfaceImplOne implements MyInterface {
       void myMethod(){
         // dosmthg
        }
    }
    

    MyInterfaceImplOneCondition.java接口

    public class MyInterfaceImplOneCondition implements Condition {
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
           return context.getEnvironment().getProperty("optionEnabled")
        }
    }
    

    对于第二个实现:

    MyInterfaceImplTwo.java文件

    @Bean
    @Conditional(MyInterfaceImplTwoCondition.class)
    public class MyInterfaceImplTwo implements MyInterface {
       void myMethod(){
         // dosmthg 2
        }
    }
    

    MyInterfaceImplTwoCondition.java接口

    public class MyInterfaceImplTwoCondition implements Condition {
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
           return !context.getEnvironment().getProperty("optionEnabled")
        }
    }
    

    在这种情况下,您现在只需调用接口,Spring将注入对应于正确条件的bean。

    希望这就是你要找的,我已经很清楚了!

    推荐文章