Skip to content
Advertisement

Composite annotations to reuse

we have couple of annotations, for springboot controller method. and these are mostly reused in multiple methods.

@Annotation1
@Annotation2
@Annotation3
public void someMethod()

combine as

@CompositeAnnotation
public void someMethod()

Is there a way we can create a composite annotation ? I know we might as well add a new one which encapsulates these, but at times this is not possible.

Just trying to reuse these bunch as single or reduced to fewer ones.

Any help is appreciated

Advertisement

Answer

It’s possible to create a new annotation that will have all yours.

You can take a look at @SpringBootApplication realization – it contains @SpringBootConfiguration, @EnableAutoConfiguration, @ComponentScan annotations on self, so all of them are applied when @SpringBootApplication is used.

Or @RestController which combines @Controller and @ResponseBody.

In your case:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Annotation1
@Annotation2
@Annotation3
public @interface CompositeAnnotation

Here is some tutorial: https://chrysanthium.com/spring-annotation-composition

Advertisement