带有数组元素的Java自定义注释

use*_*872 5 java spring java-8

我有一个自定义注释如下.

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnApiVersionConditional.class)
public @interface ConditionalOnApiVersion {

    int[] value() default 5;

    String property();
}
Run Code Online (Sandbox Code Playgroud)

OnApiVersionConditional是,

public class OnApiVersionConditional implements Condition {

  @Override
    public boolean matches(final ConditionContext context, final AnnotatedTypeMetadata metadata) {
        final MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(ConditionalOnApiVersion.class.getName());

       attributes.get("value");


        final String inputVersion = context.getEnvironment().getProperty("userInputVersion");


    }
Run Code Online (Sandbox Code Playgroud)

在我的Bean注释中,

  @Bean
  @ConditionalOnApiVersion(value = {6, 7}, property = "userInputVersion")
Run Code Online (Sandbox Code Playgroud)

还有像单一版本匹配的bean

@Bean
@ConditionalOnApiVersion(value = 8, property = "userInputVersion")
Run Code Online (Sandbox Code Playgroud)

我想验证属性文件中的userInput版本到可用的Beans支持版本.不确定,如何获取值,迭代并与userInoutVersion进行比较.该值可以是8或{6,7}作为int数组.不确定,如何迭代该值以检查是否有任何值与输入版本匹配.

final List apiVersions = attributes.get("value").stream().collect(Collectors.toList());

题:

如何迭代attributes.get("value")并与userInputVersion进行比较?

attributes.get("value")返回一个Object列表.

我试过下面的代码,

final List<Object> apiVersions = attributes.get("value").stream().collect(Collectors.toList());

boolean result = apiVersions.stream().anyMatch(version -> (int)version == Integer.parseInt(userInputVersion));
Run Code Online (Sandbox Code Playgroud)

但得到以下错误在第二行anyMatch,

java.lang.ClassCastException:[我无法转换为java.lang.Integer

谢谢

Ian*_* Mc 3

您已经定义了一个很好的自定义注释,它使用 Spring 的 @Conditional,并且只有在数组value中存在名为“ property ”的属性时,此过程才会创建 bean 。

该注释定义了这两个变量,如下所示:

  1. value : int[] (一个或多个支持的版本)
  2. property : String (要比较的属性的名称)

当您使用元数据检索这些值时,Spring 将它们作为对象返回。将这些对象转换为您定义的类型是关键步骤。不需要流,因为迭代 int[] 数组很简单。

我测试了您的注释的各种形式(值= 5,值= {6,7}等),并发现以下代码运行良好(仅在版本正确时才创建@Conditional beans)。

public class OnApiVersionConditional implements Condition {

@Override
public boolean matches(final ConditionContext context, final AnnotatedTypeMetadata metadata) {

    final MultiValueMap<String, Object> attributes = metadata
            .getAllAnnotationAttributes(ConditionalOnApiVersion.class.getName());

    // 1.  Retrieve the property name from the annotation (i.e. userInputVersion)
    List<Object> propertyObject = attributes.get("property");
    // 2.  Cast it to a String
    String propertyName = (String)propertyObject.get(0);

    // 3.  Retrieve the value 
    List<Object> list = attributes.get("value");
    // 4.  Cast it to int[]
    int[] values = (int[])list.get(0);

    // 5.  Look up the actual version (based on the property name in the annotation)
    final String inputVersion = context.getEnvironment().getProperty(propertyName);
    // Assume it is an integer? 
    int version = Integer.valueOf(inputVersion).intValue();

    // 6.  Scan the supported version; look to match against actual version
    for (int i : values)
        if (i == version)
            return true;

    // The version is not supported
    return false;

}

}
Run Code Online (Sandbox Code Playgroud)

可以通过验证属性和价值来改进该方法;如果其中任何一个包含错误/空值等,则返回 false。