带有数组元素的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 注释对这两个变量的定义如下:
可以通过验证属性和值来改进此方法;如果其中任何一个包含坏值/空值,则返回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;
}
}