[英]Custom Annotations in Spring overwriting parameters
Is it possible to overwrite the parameter values of an annotation used in a custom annotation?是否可以覆盖自定义注释中使用的注释的参数值?
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SpringBootTest(classes = [HOW_TO_CHANGE_THESE])
@ComponentScan(basePackages = {[HOW_TO_CHANGE_THESE]})
@ActiveProfiles("testing")
public @interface IntegrationTest {
}
My intention was to reduce the number of annotations in my tests, while setting default values to all test of this type.我的目的是减少测试中的注释数量,同时为所有这种类型的测试设置默认值。 I would like to use it like this:
我想这样使用它:
@IntegrationTest(classes="...", basePackages="...")
class AbcTest {
}
Thanks in advance!提前致谢!
Yes, it is possible if you are using the Spring framework.是的,如果您使用的是 Spring 框架,这是可能的。 It has a special @AliasFor annotation for this purpose:
为此,它有一个特殊的@AliasFor注释:
@AliasFor is an annotation that is used to declare aliases for annotation attributes.
@AliasFor 是一个注解,用于声明注解属性的别名。
You can find more examples of its usage in the official documentation here or in this tutorial .您可以在此处的官方文档或本教程中找到更多使用示例。
Also, there are details of how to use meta-annotations in the Spring Test Context in the official documentation here .此外, 这里的官方文档中的 Spring 测试上下文中详细介绍了如何使用元注释。
Your example with the usage of this annotation will look like this:您使用此注释的示例将如下所示:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SpringBootTest
@ComponentScan
@ActiveProfiles("testing")
public @interface IntegrationTest {
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] basePackages() default {};
@AliasFor(annotation = SpringBootTest.class, attribute = "classes")
Class<?>[] classes() default {};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.