1

In my Spring application I have controllers with two annotations:

@Controller - Spring annotation

@AdminPanelController - my annotation

Is it possible change my annotation so I can used if with out the need to place @Controller in addition?

I want that Spring will process my annotation as @Controller annotation.

Haim Raman
  • 11,508
  • 6
  • 44
  • 70
Arthur
  • 3,253
  • 7
  • 43
  • 75

2 Answers2

5

Your question is missing some detailed explanation on your needs, but my assumption is that you do not want to bother putting both your annotation and @Controller on your admin panel controllers. You want Spring-MVC you understand that any @AdminPanelController is-a @Controller.
This is exactly what @RestController annotation in Spring 4.0 any @RestController is-a @Controller (See the source code). So your @AdminPanelController annotation should be similar to the one below

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
public @interface AdminPanelController {
    String value() default "";
}

So although java annotation does not support inheritance in the sense we expect (see Why is not possible to extend annotations in Java?) this will work for spring-mvc thanks to meta-annotation (I tested this with 4.1).

Community
  • 1
  • 1
Haim Raman
  • 11,508
  • 6
  • 44
  • 70
3

I'm reasonably sure that all you need is to annotate the AdminPanelController with @Controller

Lev Kuznetsov
  • 3,520
  • 5
  • 20
  • 33