问题
I have a problem with using multiple annotations that all say more or less the same thing but to different frameworks and I would like to group them all into one custom annotation. Currently it looks like this:
@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;
As you can see they all repeat the same String and I would like to combine them, but I have yet to find a way of doing so.
Is it at all possible to do this, or do I have to either continue doing this or change/create a new framework?
回答1:
The answer is: probably no, this is not possible (using "standard" java).
You see, there is no inheritance for annotations, yet alone "multiple" inheritance which would allow you to express:
public @interface MultiAnnotiation extends Column, XmlElement, ...
And most likely, those annotations work like this: at runtime the corresponding frameworks uses reflection to check if a certain object has that annotation. And if it doesn't find "its" annotation, nothing happens.
So you would need a way to "magically" insert those annotations into your class file.
Boiling down to: when you would write your own compiler, and invent something on top of java, then you could do something like that.
Things might be different when dealing with annotations that come with compiler-plugins (meaning: an annotation that is processed at compile time). Maybe you could write your own custom annotation then that triggers the same compile-time operations.
Long story short: all of this sounds interesting, but rather advanced, and most likely: not resulting in robust, production-worthy code!
回答2:
It is possible to merge some annotations to only one annotation. Spring for example do it in the SpringBootApplication-Annotation:
/**
* Indicates a {@link Configuration configuration} class that declares one or more
* {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration
* auto-configuration} and {@link ComponentScan component scanning}. This is a convenience
* annotation that is equivalent to declaring {@code @Configuration},
* {@code @EnableAutoConfiguration} and {@code @ComponentScan}.
*
* @author Phillip Webb
* @since 1.2.0
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {
/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
Class<?>[] exclude() default {};
}
But i don't know if it is possible to set a value to the inner annotations from the merged annotation.
来源:https://stackoverflow.com/questions/43679586/merge-multiple-annotations-with-parameters