Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.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
带有数组元素的Java自定义注释_Java_Spring_Java 8 - Fatal编程技术网

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

带有数组元素的Java自定义注释,java,spring,java-8,Java,Spring,Java 8,我有一个自定义注释,如下所示 @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.METHOD }) @Documented @Conditional(OnApiVersionConditional.class) public @interface ConditionalOnApiVersion { int[] value() default 5; String propert

我有一个自定义注释,如下所示

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

    int[] value() default 5;

    String property();
}
一个版本的条件是

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");


    }
也有单版本匹配的bean,比如

@Bean
@ConditionalOnApiVersion(value = 8, property = "userInputVersion")
我想验证从属性文件到可用的Beans支持版本的userInput版本。不确定,如何获取值、迭代并与userInoutVersion进行比较。作为int数组,该值可以是8或{6,7}。不确定,如何迭代该值以检查是否有任何值与输入版本匹配

最终列表apiVersions=attributes.get(“value”).stream().collect(Collectors.toList())

问题:

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

get(“value”)返回对象的列表

我试过下面的代码

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

boolean result = apiVersions.stream().anyMatch(version -> (int)version == Integer.parseInt(userInputVersion));
final List apiVersions=attributes.get(“value”).stream().collect(Collectors.toList());
布尔结果=apiVersions.stream().anyMatch(version->(int)version==Integer.parseInt(userInputVersion));
但是在第二行的任意匹配中得到下面的错误

java.lang.ClassCastException:[我不能转换为java.lang.Integer


谢谢

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

注释对这两个变量的定义如下:

  • 值:int[](一个或多个受支持的版本)
  • 属性:字符串(要比较的属性的名称)
  • 当您使用元数据检索这些值时,Spring将它们作为对象返回。将这些对象转换为您定义为的类型是一个关键步骤。不需要流,因为迭代int[]数组非常简单

    我测试了各种形式的注释(value=5、value={6,7},等等),发现下面的代码工作得很好(只有在版本正确时才创建@conditionalbeans)

    PIVersionConditional实现条件上的公共类{ @凌驾 公共布尔匹配(最终条件上下文、最终注释类型元数据){ 最终多值映射属性=元数据 .getAllAnnotationAttributes(conditionalOnAversion.class.getName()); //1.从注释中检索属性名称(即userInputVersion) List propertyObject=attributes.get(“属性”); //2.把它扔到绳子上 String propertyName=(String)propertyObject.get(0); //3.检索值 列表=attributes.get(“值”); //4.将其转换为int[] int[]值=(int[])list.get(0); //5.查找实际版本(基于批注中的属性名称) 最终字符串inputVersion=context.getEnvironment().getProperty(propertyName); //假设它是一个整数? int version=Integer.valueOf(inputVersion).intValue(); //6.扫描支持的版本;查看是否与实际版本匹配 for(int i:值) 如果(i==版本) 返回true; //不支持该版本 返回false; } }
    可以通过验证属性和值来改进此方法;如果其中任何一个包含坏值/空值,则返回false,等等。

    “如果我将inputversion传递为5.0,则此方法不起作用”--定义“不起作用”。“上述结果将导致类强制转换异常”--您的问题,并包括完整的堆栈跟踪(请将格式设置为
    code
    ,而不是blockquote),并指出源代码中的哪一行引发异常。更新了我的问题输入版本不能是双精度的,因为它需要
    int[]
    。如果您希望灵活,您希望传递
    字符串[]
    。你也没有得到一个
    列表,你得到的是一个数组。这看起来很好。我怎么能有一个bean来支持大于6的版本?有条件吗?你是在问你是否可以将注释更改为“minSupportedVersion”而不是int数组?然后将其与属性中的值进行比较erty文件?如果是,是。是查找支持的次要版本1)而不是定义int[]value(),您将定义int minValue()默认值5。2)在条件内部,您将强制转换为整数(而不是int[])。3)然后只需检查(minValue>=version)返回true。是否希望编辑后显示?谢谢。我非常喜欢你的方法;非常好地使用Spring工具。做得好。
    
    final List<Object> apiVersions = attributes.get("value").stream().collect(Collectors.toList());
    
    boolean result = apiVersions.stream().anyMatch(version -> (int)version == Integer.parseInt(userInputVersion));
    
    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;
    
    }
    
    }