Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
@具有@ConditionalOnProperty的自动连线Spring@组件_Spring_Autowired_Featuretoggle - Fatal编程技术网

@具有@ConditionalOnProperty的自动连线Spring@组件

@具有@ConditionalOnProperty的自动连线Spring@组件,spring,autowired,featuretoggle,Spring,Autowired,Featuretoggle,我可以使用@AutowiredSpring 4.x@Components和@ConditionalOnProperty根据featuretoggles.properties文件选择功能的实现吗 使用src/main/resources/featuretoggles.properties文件: (切换“b”的名称和类“b”的名称匹配是巧合;我的目标不是让它们相等,切换可以有任何名称。) 这无法自动连接控制器中的功能,出现未满足的PendencyException,表示“没有可用的'feature'

我可以使用
@Autowired
Spring 4.x
@Component
s和
@ConditionalOnProperty
根据featuretoggles.properties文件选择功能的实现吗

使用src/main/resources/featuretoggles.properties文件:

(切换“b”的名称和类“b”的名称匹配是巧合;我的目标不是让它们相等,切换可以有任何名称。)

这无法自动连接
控制器中的
功能
,出现
未满足的PendencyException
,表示“没有可用的'feature'类型的合格bean:至少需要1个符合autowire候选条件的bean”

我知道我可以通过一个
@Configuration
类来实现这一点,该类根据属性选择
@Bean
。但当我这样做时,每次添加功能切换时,我都必须添加一个新的配置类,这些配置类将非常相似:

@Configuration
@PropertySource("classpath:featuretoggles.properties")
public class FeatureConfig {

    @Bean
    @ConditionalOnProperty(name = "b", havingValue = "on")
    public Feature useB() {
        return new B();
    }

    @Bean
    @ConditionalOnProperty(name = "b", havingValue = "off")
    public Feature useA() {
        return new A();
    }

}

我做了你想做的事。第一步是编写一个
条件
。。。


您可以将
featuretoggles.properties
添加到带有注释的环境中。

a
@PropertySource
仅对
@配置文件有用。现在它将永远不会被加载,并且您的条件将永远不会匹配。也就是说,我仍然会选择配置类,因为它没有那么神奇,在你看来,它比所有那些带注释的组件都要神奇。你完全正确。尝试了一下,并用我更改的内容更新了示例。不幸的是,添加一个配置类并不能改善情况。上面的样品仍然无法接线。到目前为止,我还没有发现
@Component
是否与
@ConditionalOnProperty
一起工作,或者我是否仍然有一个无法加载属性文件的bug。遗憾的是,Spring并没有为此提供有意义的日志输出。起初我们以为我们彼此误解了。但是再尝试一下,这实际上解决了这个问题。谢谢
b = on
@Configuration
@PropertySource("classpath:featuretoggles.properties")
public class FeatureConfig {

    @Bean
    @ConditionalOnProperty(name = "b", havingValue = "on")
    public Feature useB() {
        return new B();
    }

    @Bean
    @ConditionalOnProperty(name = "b", havingValue = "off")
    public Feature useA() {
        return new A();
    }

}
public class OnEnvironmentPropertyCondition implements Condition
{
  @Override
  public boolean matches(ConditionContext ctx, AnnotatedTypeMetadata meta)
  {
    Environment env = ctx.getEnvironment();
    Map<String, Object> attr = meta.getAnnotationAttributes(
                                 ConditionalOnEnvProperty.class.getName());

    boolean shouldPropExist = (Boolean)attr.get("exists");
    String prop = (String)attr.get("value");

    boolean doesPropExist = env.getProperty(prop) != null;

    // doesPropExist    shouldPropExist    result
    //    true             true             true
    //    true             false            false
    //    false            true             false
    //    true             false            true
    return doesPropExist == shouldPropExist;
  }
}
/*
 * Condition returns true if myprop exists:
 * @ConditionalOnEnvProperty("myprop")
 *
 * Condition returns true if myprop does not exist
 * @ConditionalOnEnvProperty(value="myprop", exists=false)
 */
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Conditional(OnEnvironmentPropertyCondition.class)
public @interface ConditionalOnEnvProperty
{
  public String value();
  public boolean exists() default true;
}