java - 是否可以创建我自己的自定义注释来充当 Spring @Controller 注释?

标签 java spring spring-boot

在我的 Spring 应用程序中,我有带有两个注释的 Controller :

@Controller - Spring 注释

@AdminPanelController - 我的注释

是否可以更改我的注释,以便我可以使用 if 而无需另外放置 @Controller?

我希望 Spring 将我的注释处理为 @Controller 注释。

最佳答案

您的问题缺少一些关于您的需求的详细解释,但我的假设是您不想将注释和 @Controller 都放在管理面板 Controller 上。你想要Spring-MVC,你明白任何@AdminPanelController都是@Controller。
这正是 Spring 4.0 中的 @RestController 注释,任何 @RestController 都是 @Controller (请参阅源代码 code )。 所以你的 @AdminPanelController 注释应该类似于下面的注释

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

因此,尽管 java 注释不支持我们期望的继承(参见 Why is not possible to extend annotations in Java? ),但由于 meta-annotation ,这将适用于 spring-mvc (我用 4.1 测试过)。

关于java - 是否可以创建我自己的自定义注释来充当 Spring @Controller 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28015498/

相关文章:

java - Spring Boot属性到对象

java - 使用 Kotlin 的 RSocket retrieveFlux()

java - 使用 KeyEvent(KeyPressed、KeyTyped 等)将字符附加到 JTextArea

java - 在不同平台上从 Java 代码运行 adb shell 命令

java - 如何从 Java 中的 html 代码中删除/替换以下内联 css 样式

java - 使用Spring插入大量分层数据

java - 访问 Thymeleaf 中的嵌入对象属性

java - 如何使用 Spring Boot 查询和检索约 100 个客户数据库的结果?

spring-boot - IntelliJ 不获取自己的 spring 配置元数据

Java和AppStore收据验证