简体   繁体   English

是否可以使用Java中的一组注释创建自定义注释?

[英]Is it possible to create a custom annotation with a group of annotations in Java?

I use lombok and JPA in my system. 我在我的系统中使用了lombok和JPA。 So for the entity classes, all of them looks like: 所以对于实体类,它们看起来像:

@Getter
@Setter
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class XxxEntity {
    ...
}

So my question can I create a custom annotation to grouping these all these annotations? 所以我的问题是,我可以创建一个自定义注释来分组这些所有这些注释吗?

so it may look like: 所以它可能看起来像:

@CustomAnnotation
public class XxxEntity {
    ...
}

And when I use the @CustomAnnotation, it will apply all the above annotation to that class. 当我使用@CustomAnnotation时,它会将所有上述注释应用于该类。

Is this possible? 这可能吗?

Thanks. 谢谢。

No, it's not possible. 不,这是不可能的。 You can lump your annotations together using a new annotation, but the annotation processor won't understand it. 您可以使用新注释将注释混合在一起,但注释处理器将无法理解它。 The @Caching annotation from Spring is just an example of an annotation known by the annotation processor . Spring中的@Caching注释只是注释处理器已知注释的一个示例。 If you create an own annotation by copying @Caching , I'd bet you'll see that it does not work. 如果你通过复制@Caching创建一个自己的注释,我敢打赌你会发现它不起作用。

In theory, it could work as an annotation processor can read any annotation and look inside. 从理论上讲,它可以作为注释处理器读取任何注释并查看内部。 However, it's totally unclear how an unknown annotation should be processed. 但是,完全不清楚如何处理未知的注释。 Putting other annotations inside has no standardized meaning. 在其中放入其他注释没有标准化的含义。

Especially, this does not work for Lombok. 特别是,这对龙目岛不起作用。 There are a few issues requesting this, eg, here , but it's pretty complicated. 有一些问题需要这个,例如, 这里 ,但它非常复杂。

The ultimate solution would be an annotation pre-processor understanding compound annotations and replacing them by their constituents before ordinary annotation processors run. 最终的解决方案是注释预处理器理解复合注释,并在普通注释处理器运行之前用它们的成分替换它们。 But there's no such thing and AFAIK no plans for it. 但是没有这样的事情和AFAIK没有计划。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可以在Java中创建自定义的JPA批注 - Is is possible to create a custom jpa annotation in java 是否可以在 java 中创建注释包(即应用其他注释的注释)? - Is it possible to create an annotation bundle in java (i.e. an annotation that applies other annotations)? 如何创建一组杰克逊注释的注释? - How to create an annotation that is a group of Jackson annotations? Java自定义Annotation聚合多个注释 - Java custom Annotation aggregate multiple annotations Java自定义注释需要另一个注释 - Java custom annotations takes another annotation 在 java 中编写自定义注释 - TimeCounter 注释示例 - Writing custom annotations in java - TimeCounter annotation example 是否可以创建组合多个字段注释的组合注释? - Is is possible to create a combo-annotation which combines multiple field annotations? 如何在java中创建自定义注释? - How to create custom annotation in java? 有没有办法在 Scala 中创建自定义注释并编写自定义注释处理器来验证注释? - Is there a way to create custom annotations in Scala and write a custom annotation processor to verify the annotations? 是否可以创建自己的充当Spring @Controller注释的自定义注释? - Is it possible to create my own Custom Annotation that act as Spring @Controller annotation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM