我不明白为什么Java注释中没有继承,就像Java类一样.我认为这将非常有用.
例如:我想知道给定的注释是否是验证器.通过继承,我可以反复地浏览超类,以了解此注释是否扩展了ValidatorAnnotation.否则,我该怎样才能做到这一点?
那么,任何人都可以给我这个设计决定的理由吗?
在我的项目中,我使用预定义的注释@With:
@With(Secure.class)
public class Test { //....
Run Code Online (Sandbox Code Playgroud)
源代码@With:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface With {
Class<?>[] value() default {};
}
Run Code Online (Sandbox Code Playgroud)
我想编写自定义注释@Secure,其效果与之相同@With(Secure.class).怎么做?
如果我这样喜欢怎么办?它会起作用吗?
@With(Secure.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Secure {
}
Run Code Online (Sandbox Code Playgroud) 我在使用多个注释时遇到问题,这些注释或多或少都说相同的事情,但针对不同的框架,我想将它们全部归为一个自定义注释。目前它看起来像这样:
@Column(name = "bank_account_holder_name")
@XmlElement(name = "bank_account_holder_name")
@SerializedName("bank_account_holder_name")
@ApiModelProperty(name = "bank_account_holder_name", value = "The name of the recipient bank account holder")
public String bankAccountHolderName;
Run Code Online (Sandbox Code Playgroud)
如您所见,它们都重复相同的字符串,我想将它们组合起来,但我还没有找到这样做的方法。
是否有可能做到这一点,或者我是否必须继续这样做或更改/创建一个新框架?
我有两个来自框架的注释。我经常在同一个字段上使用这两个注释。因此,我试图创建一个包含两者的“组合”注释。
但我不知道这是否可能:
现有的注释(我无法控制):
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiParam {
String name() default "";
}
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiModelProperty {
String name() default "";
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建的自定义注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
@ApiParam
@ApiModelProperty
public @interface SwaggerParam {
String name() default "";
}
Run Code Online (Sandbox Code Playgroud)
结果:注解不适用于注解类型。
问:有机会吗?
是否可以使用与另一个注释执行相同工作的自定义注释?
例如,如果我写@MyCustomAnnotation就像我写@override一样!
在使用Java注释时,我不太明白这一点.这是一个例子:
我创建一个@Log注释并添加一些功能(每个使用@Log注释的方法在执行方法之前运行一些日志).
现在我正在创建一个像这样的新@SuperLog注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Log
public @interface SuperLog {
............
}
Run Code Online (Sandbox Code Playgroud)
这个@SuperLog必须提供@Log所做的所有内容,以及一些特定于@SuperLog的额外内容.
不幸的是,当我执行一些使用@SuperLog注释的方法时,特定于@Log的日志不会执行.
我不明白为什么:@SuperLog用@Log注释的事实并不意味着它从@Log"继承"属性?不应该@SuperLog做@Log应该做的每一件事吗?
谢谢
如您所知,注释驱动的编程越来越多地融入我们现在使用的大多数框架中(即 Spring、Lombok 等)。
此外,我们有时需要创建自定义注释。(ig 使用方面 - 记录给定类的所有公共方法的进入/退出跟踪@LogAroundMethods)
因此,给定的类可以包含大量注释。
@LogAroundMethod // My custom annotation
@Slf4j // Lombok annotation
@Component // Spring annotation
public class ClientNotificationProxy {
//Code
}
@LogAroundMethod // My custom annotation
@Configuration // Spring annotation
@ConditionalOnClass(NotificationSender.class) // Spring annotation
@EnableConfigurationProperties(MessagingProperties.class) // Spring annotation
@Import({ MongoConfiguration.class, SpringRetryConfiguration.class }) // Spring annotation
public class StarterClientAutoConfiguration {
// Code
}
Run Code Online (Sandbox Code Playgroud)
在我的 Spring 应用程序中,我有带有两个注释的控制器:
@Controller - Spring注解
@AdminPanelController - 我的注释
是否可以更改我的注释,以便我可以使用 if 而无需另外放置 @Controller ?
我希望 Spring 将我的注释处理为 @Controller 注释。
java ×8
annotations ×7
spring ×4
spring-boot ×2
frameworks ×1
inheritance ×1
java-ee ×1
lombok ×1