Spring中如何自定义一个类似Controller注解呢?
今天接到一个特殊的需求,我们自定义一个类似@Controller注解,那么如何实现这一需求呢?下文笔者将一一道来,如下所示
自定义类似@Controller注解的实现思路
1.定义新注解 2.创建注解处理器 3.注册自定义handler 4.使用自定义注解例:
1.定义自定义注解
定义`@CustomController`注解 import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.TYPE) // 仅适用于类 @Retention(RetentionPolicy.Runtime) // 保留到运行时 public @interface CustomController { String value() default ""; // 可选属性 }
2.创建注解处理器
创建 `CustomRequestMappingHandlerMapping` import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import javax.annotation.PostConstruct; import java.util.Map; @Configuration public class CustomRequestMappingHandlerMapping extends RequestMappingHandlerMapping { @Autowired private Map<String, Object> beanMap; @PostConstruct protected void initHandlerMethods() { super.initHandlerMethods(); for (Map.Entry<String, Object> entry : beanMap.entrySet()) { Object bean = entry.getValue(); if (bean.getClass().isAnnotationPresent(CustomController.class)) { detectHandlerMethods(bean); } } } }
3.注册自定义`HandlerMapping`
确保自定义`HandlerMapping` 被Spring容器识别和注册 配置类 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class WebConfig { @Bean public CustomRequestMappingHandlerMapping customRequestMappingHandlerMapping() { return new CustomRequestMappingHandlerMapping(); } }
4.使用自定义注解
使用`@CustomController`注解 import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; @CustomController public class MyCustomController { @GetMapping("/custom") @ResponseBody public String customEndpoint() { return "Hello from Custom Controller!"; } }
完整示例
自定义注解 `CustomController` import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.TYPE) // 仅适用于类 @Retention(RetentionPolicy.RUNTIME) // 保留到运行时 public @interface CustomController { String value() default ""; // 可选属性 } 自定义`HandlerMapping` import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import javax.annotation.PostConstruct; import java.util.Map; @Configuration public class CustomRequestMappingHandlerMapping extends RequestMappingHandlerMapping { @Autowired private Map<String, Object> beanMap; @PostConstruct protected void initHandlerMethods() { super.initHandlerMethods(); for (Map.Entry<String, Object> entry : beanMap.entrySet()) { Object bean = entry.getValue(); if (bean.getClass().isAnnotationPresent(CustomController.class)) { detectHandlerMethods(bean); } } } } 配置类 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class WebConfig { @Bean public CustomRequestMappingHandlerMapping customRequestMappingHandlerMapping() { return new CustomRequestMappingHandlerMapping(); } } 使用自定义注解的控制器 import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; @CustomController public class MyCustomController { @GetMapping("/custom") @ResponseBody public String customEndpoint() { return "Hello from Custom Controller!"; } }
完整示例运行及测试
pring Boot 应用程序 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class CustomControllerApplication { public static void main(String[] args) { SpringApplication.run(CustomControllerApplication.class, args); } } 启动应用程序后 访问 `http://localhost:8080/custom` 浏览器将会返回 `Hello from Custom Controller!`
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。