springboot高级功能(三)自定义注解实现方式全解析

简介: springboot高级功能(三)自定义注解实现方式全解析


源注解

@Retention

1.  //注解只会存在源代码中,将会被编译器丢弃
2.     SOURCE,
3. //注解将会保留到class文件阶段,但是在加载如vm的时候会被抛弃
4. CLASS,
5. //注解不单会被保留到class文件阶段,而且也会被vm加载进虚拟机的时候保留
6.     RUNTIME

@Target

1.     用于描述类、接口(包括注解类型) 或enum声明 Class, interface (including annotation type), or enum declaration 
2.     TYPE,
3. 
4.     用于描述域 Field declaration (includes enum constants)
5.     FIELD,
6. 
7.     用于描述方法 Method declaration
8. METHOD,
9. 
10.     用于描述参数 Formal parameter declaration 
11. PARAMETER,
12. 
13.     用于描述构造器 Constructor declaration
14.     CONSTRUCTOR,
15. 
16.     用于描述局部变量 Local variable declaration
17.     LOCAL_VARIABLE,
18. 
19.     Annotation type declaration
20.     ANNOTATION_TYPE,
21. 
22.     用于描述包 Package declaration
23.     PACKAGE,
24. 
25.     用来标注类型参数 Type parameter declaration
26.     TYPE_PARAMETER,
27. 
28. *能标注任何类型名称 Use of a type
29.     TYPE_USE

依赖于@Conditional

原理是是否满足@Conditional中绑定类的条件,如果满足,就将使用注解的类注入进factory,不满足就不注入,只能在类中使用或者配合@bean使用

代码

首先定义一个注解类

定义的变量为参数

@Conditional(CustomOnPropertyCondition.class)意思为CustomOnPropertyCondition类中返回为true才会使用注解

1. package com.airboot.bootdemo.config;
2. import org.springframework.context.annotation.Conditional;
3. import java.lang.annotation.*;
4. 
5. @Retention(RetentionPolicy.RUNTIME)
6. @Target({ElementType.TYPE, ElementType.METHOD})
7. @Documented
8. @Conditional(CustomPropertyCondition.class)
9. public @interface CustomProperty {
10. 
11. //参数
12.     String name() default "";
13. /**
14.          * havingValue数组,支持or匹配
15.          */
16. 
17. //参数
18.     String[] havingValue() default {};
19. 
20. }

CustomOnPropertyCondition类

实现condition接口 以下是实现接口后的代码 然后在其中填补

1. public class CustomPropertyCondition implements Condition {
2. @Override
3. public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
4. return false;
5.     }
6. }
1. 
2. import org.springframework.context.annotation.Condition;
3. import org.springframework.context.annotation.ConditionContext;
4. import org.springframework.core.type.AnnotatedTypeMetadata;
5. import org.springframework.web.bind.annotation.RequestMapping;
6. 
7. import java.util.Map;
8. 
9. 
10. public class CustomPropertyCondition implements Condition {
11. @Override
12. public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
13. //获取注解上的参数和配置值
14. Map<String, Object> annotationAttributes = annotatedTypeMetadata.getAnnotationAttributes(RequestMapping.class.getName());
15. //获取具体的参数值
16. String propertyName = (String) annotationAttributes.get("name");
17. String[] values = (String[]) annotationAttributes.get("havingValue");
18. if (0 == values.length) {
19. return false;
20.         }
21. //从application.properties中获取配置
22. String propertyValue = conditionContext.getEnvironment().getProperty(propertyName);
23. // 有一个匹配上就ok
24. for (String havingValue : values) {
25. if (propertyValue.equalsIgnoreCase(havingValue)) {
26. return true;
27.             }
28.         }
29. return false;
30.     }
31. }

Map annotationAttributes = annotatedTypeMetadata.getAnnotationAttributes(RequestMapping.class.getName()); 获取类中注解上的参数

String propertyValue = conditionContext.getEnvironment().getProperty(propertyName);获取application.properties上的配置

使用

1. @RequestMapping(value ="/Demo")
2. @Controller
3. @ResponseBody
4. @Api("demo测试类")
5. @CustomProperty(name = "condition",havingValue = {"2"})
6. public class DemoController {
7. 
8. 
9. }

注解在spring boot启动时加载。

使用HandlerMethodArgumentResolver

注意 1 只能接受get请求

定义注解

1. package com.airboot.bootdemo.config;
2. 
3. import java.lang.annotation.*;
4. 
5. 
6. @Target({ElementType.PARAMETER, ElementType.METHOD})
7. @Retention(RetentionPolicy.RUNTIME)
8. @Documented
9. public @interface UserCheck {
10. 
11. //当前用户在request中的名字
12.     String value() default "userid";
13. }

定义类实现HandlerMethodArgumentResolver

其中resolveArgument中返回的是controllor的参数,也就是这个方法里面可以组装入参

1. package com.airboot.bootdemo.config;
2. 
3. import org.springframework.core.MethodParameter;
4. import org.springframework.web.bind.support.WebDataBinderFactory;
5. import org.springframework.web.context.request.NativeWebRequest;
6. import org.springframework.web.method.support.HandlerMethodArgumentResolver;
7. import org.springframework.web.method.support.ModelAndViewContainer;
8. 
9. public class UserCheckMethodArgumentResolver implements HandlerMethodArgumentResolver {
10. @Override
11. public boolean supportsParameter(MethodParameter methodParameter) {
12. return false;
13.     }
14. 
15. @Override
16. public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
17. return null;
18.     }
19. }
1. import com.airboot.bootdemo.entity.DemoVO;
2. import org.springframework.core.MethodParameter;
3. import org.springframework.web.bind.support.WebDataBinderFactory;
4. import org.springframework.web.context.request.NativeWebRequest;
5. import org.springframework.web.method.support.HandlerMethodArgumentResolver;
6. import org.springframework.web.method.support.ModelAndViewContainer;
7. 
8. public class UserCheckMethodArgumentResolver implements HandlerMethodArgumentResolver {
9. @Override
10. public boolean supportsParameter(MethodParameter parameter) {
11. if (parameter.getParameterType().isAssignableFrom(DemoVO.class) && parameter.hasParameterAnnotation(UserCheck.class)) {
12. return true;
13.         }
14. return false;
15.     }
16. 
17. @Override
18. public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
19. UserCheck currentUserAnnotation = parameter.getParameterAnnotation(UserCheck.class);
20. //获取head中的
21. String userId = webRequest.getHeader("userId");
22. //组装入参 return后的参数 类型需要和controllor中的相同 
23. DemoVO demoVO = new DemoVO();
24.         demoVO.setId(Long.valueOf(1));
25. return demoVO;
26.     }
27. }

controllor

1. @RequestMapping(value = "/selectDemoByVo")
2. public List<DemoVO> selectDemoByVo(@RequestBody @UserCheck DemoVO demoVO) {
3. return demoService.selectDemoVO(demoVO);
4.     }

基于aop

1. 
2. 1.JoinPoint  
3. * java.lang.Object[] getArgs():获取连接点方法运行时的入参列表; 
4. * Signature getSignature() :获取连接点的方法签名对象; 
5. * java.lang.Object getTarget() :获取连接点所在的目标对象; 
6. * java.lang.Object getThis() :获取代理对象本身; 
7. 
8. 2.ProceedingJoinPoint  
9. * ProceedingJoinPoint继承JoinPoint子接口,它新增了两个用于执行连接点方法的方法: 
10. * java.lang.Object proceed() throws java.lang.Throwable:通过反射执行目标对象的连接点处的方法; 
11. * java.lang.Object proceed(java.lang.Object[] args) throws java.lang.Throwable:通过反射执行目标对象连接点处的方法,不过使用新的入参替换原来的入参。

注解类

1. import java.lang.annotation.ElementType;
2. import java.lang.annotation.Retention;
3. import java.lang.annotation.RetentionPolicy;
4. import java.lang.annotation.Target;
5. 
6. @Target({ ElementType.METHOD })
7. @Retention(RetentionPolicy.RUNTIME)
8. public @interface TestAnnotation {
9. 
10. int p0() default 0;
11.     String p1() default "";
12.     Class<?> clazz();
13. 
14. }

切面配置

其中切点要切到注解类的路径

1. import org.aspectj.lang.ProceedingJoinPoint;
2. import org.aspectj.lang.annotation.*;
3. import org.springframework.stereotype.Component;
4. import lombok.extern.slf4j.Slf4j;
5. 
6. @Slf4j
7. @Aspect
8. @Component
9. public class TestAspect {
10. 
11. // 切入点签名
12. @Pointcut("@annotation(com.airboot.bootdemo.config.TestAnnotation)")
13. private void cut() {
14.     }
15. 
16. // 前置通知
17. @Before("cut()")
18. public void BeforeCall() {
19.         log.info("====前置通知start");
20. 
21.         log.info("====前置通知end");
22.     }
23. 
24. // 环绕通知
25. @Around(value = "cut()")
26. public Object AroundCall(ProceedingJoinPoint joinPoint) throws Throwable {
27.         log.info("====环绕通知start");
28. 
29. // 注解所切的方法所在类的全类名
30. String typeName = joinPoint.getTarget().getClass().getName();
31.         log.info("目标对象:[{}]", typeName);
32. 
33. // 注解所切的方法名
34. String methodName = joinPoint.getSignature().getName();
35.         log.info("所切方法名:[{}]", methodName);
36. 
37. StringBuilder sb = new StringBuilder();
38. // 获取参数
39.         Object[] arguments = joinPoint.getArgs();
40. for (Object argument : arguments) {
41.             sb.append(argument.toString());
42.         }
43.         log.info("所切方法入参:[{}]", sb.toString());
44. 
45. // 统计方法执行时间
46. long start = System.currentTimeMillis();
47. 
48. //执行目标方法,并获得对应方法的返回值
49. Object result = joinPoint.proceed();
50.         log.info("返回结果:[{}]", result);
51. 
52. long end = System.currentTimeMillis();
53.         log.info("====执行方法共用时:[{}]", (end - start));
54. 
55.         log.info("====环绕通知之结束");
56. return result;
57.     }
58. 
59. // 后置通知
60. @After("cut()")
61. public void AfterCall() {
62.         log.info("====后置通知start");
63. 
64.         log.info("====后置通知end");
65.     }
66. 
67. // 最终通知
68. @AfterReturning("cut()")
69. public void AfterReturningCall() {
70.         log.info("====最终通知start");
71. 
72.         log.info("====最终通知end");
73.     }
74. 
75. // 异常通知
76. @AfterThrowing(value = "cut()", throwing = "ex")
77. public void afterThrowing(Throwable ex) {
78. throw new RuntimeException(ex);
79.     }
80. 
81. }

controllor

1. @RequestMapping(value = "/selectDemoByVo")
2. @TestAnnotation(p0 = 123, p1 = "qaws",clazz = DemoVO.class)
3. public List<DemoVO> selectDemoByVo(@RequestBody DemoVO demoVO) {
4. return demoService.selectDemoVO(demoVO);
5.     }

拦截器

自定义注解类

1. import java.lang.annotation.*;
2. 
3. @Target(ElementType.METHOD)
4. @Retention(RetentionPolicy.RUNTIME)
5. @Documented
6. public @interface InterceptorAnnotation {
7. 
8. 
9.     String[] value() default {};
10. 
11.     String[] authorities() default {};
12. 
13.     String[] roles() default {};
14. }

拦截器

1. import org.springframework.web.method.HandlerMethod;
2. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
3. 
4. import javax.servlet.http.HttpServletRequest;
5. import javax.servlet.http.HttpServletResponse;
6. import java.lang.reflect.Method;
7. 
8. public class TestAnnotationInterceptor extends HandlerInterceptorAdapter {
9. // 在调用方法之前执行拦截
10. @Override
11.     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
12. // 将handler强转为HandlerMethod, 前面已经证实这个handler就是HandlerMethod
13. HandlerMethod handlerMethod = (HandlerMethod) handler;
14. // 从方法处理器中获取出要调用的方法
15. Method method = handlerMethod.getMethod();
16. // 获取出方法上的自定义注解
17. InterceptorAnnotation access = method.getAnnotation(InterceptorAnnotation.class);
18. if (access == null) {
19. // 如果注解为null,没有注解 不拦截
20. return true;
21.         }
22. //获取注解值
23. if (access.authorities().length > 0) {
24. // 如果权限配置不为空, 则取出配置值
25. String[] authorities = access.authorities();
26.         }
27. // 拦截之后应该返回公共结果, 这里没做处理
28. return true;
29.     }
30. }

主要原理就是拦截所有的请求 然后判断方法上是否有自定的注解 如果有注解 执行注解的操作

注册拦截器

1. package com.airboot.bootdemo.config;
2. 
3. 
4. import org.springframework.context.annotation.Configuration;
5. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
7. 
8. @Configuration
9. public class InterceptorConfig extends WebMvcConfigurerAdapter {
10. @Override
11.     public void addInterceptors(InterceptorRegistry registry) {
12.         registry.addInterceptor(new TestAnnotationInterceptor()).addPathPatterns("/**");
13.     }
14. }

调用

1. @RequestMapping(value = "/selectDemoByVo")
2. @InterceptorAnnotation(authorities = {"admin"})
3. public List<DemoVO> selectDemoByVo(@RequestBody DemoVO demoVO) {
4. return demoService.selectDemoVO(demoVO);
5.     }

ConstraintValidator注解实现验证

验证入参格式使用

注解类

其中message是返回值,groups()和payload() 是必须有的

@Constraint(validatedBy = TestConstraintValidator.class) 是处理注解逻辑的类

1. import javax.validation.Constraint;
2. import javax.validation.Payload;
3. import java.lang.annotation.*;
4. 
5. @Target({ElementType.METHOD, ElementType.FIELD})
6. @Retention(RetentionPolicy.RUNTIME)
7. @Documented
8. @Constraint(validatedBy = TestConstraintValidator.class)
9. public @interface TestConstraintAnnotation {
10. 
11.     String message() default "入参大小不合适";
12. 
13. long min();
14. 
15. long max();
16. 
17. boolean required() default true;
18. 
19.     Class<?>[] groups() default {};
20. 
21.     Class<? extends Payload>[] payload() default {};
22. 
23. }

逻辑类

需要实现ConstraintValidator,第一个参数是注解类,第二个参数是入参类型

只有第一次调用时才会调用initialize ,如果满足isValid逻辑,那么就正常执行,不满足会有message提示

1. public class TestConstraintValidator implements ConstraintValidator<TestConstraintAnnotation, Object> {
2. private long max = 1;
3. private long min = 1;
4. 
5. @Override
6. public void initialize(TestConstraintAnnotation constraintAnnotation) {
7.         max = constraintAnnotation.max();
8.         min = constraintAnnotation.min();
9.     }
10. 
11. @Override
12. public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
13. if(o == null){
14. return true;
15.         }
16. if(Long.valueOf(o.toString())>=min && Long.valueOf(o.toString())<=max){
17. return true;
18.         }
19. return false;
20.     }
21. }

使用

vo中在需要验证的参数上加上自定义的注解,在方法接收参数前加入@Valid 说明本方法需要验证

1. @RequestMapping(value = "/selectDemoByVo")
2. public List<DemoVO> selectDemoByVo(@Valid @RequestBody DemoVO demoVO) {
3. return demoService.selectDemoVO(demoVO);
4.     }
5. 
6. 
7. @TestConstraintAnnotation(min = 1, max = 10)
8. private Long id;

参考:https://www.licoy.cn/3238.html

       https://www.jianshu.com/p/8cbfff715581

       http://www.360doc.com/content/17/1122/16/16915_706175138.shtml

       https://blog.csdn.net/m0_37819279/article/details/80455165

       https://www.jianshu.com/p/d7842927340f

       https://www.jianshu.com/p/e04eeae86cf9


目录
打赏
0
0
0
0
27
分享
相关文章
SpringBoot高级并发实践:自定义线程池与@Async异步调用深度解析
SpringBoot高级并发实践:自定义线程池与@Async异步调用深度解析
561 0
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— logback.xml 配置文件解析
本文解析了 `logback.xml` 配置文件的详细内容,包括日志输出格式、存储路径、控制台输出及日志级别等关键配置。通过定义 `LOG_PATTERN` 和 `FILE_PATH`,设置日志格式与存储路径;利用 `&lt;appender&gt;` 节点配置控制台和文件输出,支持日志滚动策略(如文件大小限制和保存时长);最后通过 `&lt;logger&gt;` 和 `&lt;root&gt;` 定义日志级别与输出方式。此配置适用于精细化管理日志输出,满足不同场景需求。
357 1
SpringBoot + 通义千问 + 自定义React组件:支持EventStream数据解析的技术实践
【10月更文挑战第7天】在现代Web开发中,集成多种技术栈以实现复杂的功能需求已成为常态。本文将详细介绍如何使用SpringBoot作为后端框架,结合阿里巴巴的通义千问(一个强大的自然语言处理服务),并通过自定义React组件来支持服务器发送事件(SSE, Server-Sent Events)的EventStream数据解析。这一组合不仅能够实现高效的实时通信,还能利用AI技术提升用户体验。
660 2
React音频播放器样式自定义全解析:从入门到避坑指南
在React中使用HTML5原生&lt;audio&gt;标签时,开发者常面临视觉一致性缺失、样式定制局限和交互体验割裂等问题。通过隐藏原生控件并构建自定义UI层,可以实现完全可控的播放器视觉风格,避免状态不同步等典型问题。结合事件监听、进度条拖拽、浏览器兼容性处理及性能优化技巧,可构建高性能、可维护的音频组件,满足跨平台需求。建议优先使用成熟音频库(如react-player),仅在深度定制需求时采用原生方案。
148 12
详细介绍SpringBoot启动流程及配置类解析原理
通过对 Spring Boot 启动流程及配置类解析原理的深入分析,我们可以看到 Spring Boot 在启动时的灵活性和可扩展性。理解这些机制不仅有助于开发者更好地使用 Spring Boot 进行应用开发,还能够在面对问题时,迅速定位和解决问题。希望本文能为您在 Spring Boot 开发过程中提供有效的指导和帮助。
216 12
【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
本文详细介绍了Spring框架的核心功能,并通过手写自定义Spring框架的方式,深入理解了Spring的IOC(控制反转)和DI(依赖注入)功能,并且学会实际运用设计模式到真实开发中。
【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
Spring MVC中的请求映射:@RequestMapping注解深度解析
在Spring MVC框架中,`@RequestMapping`注解是实现请求映射的关键,它将HTTP请求映射到相应的处理器方法上。本文将深入探讨`@RequestMapping`注解的工作原理、使用方法以及最佳实践,为开发者提供一份详尽的技术干货。
659 2
探索Spring MVC:@Controller注解的全面解析
在Spring MVC框架中,`@Controller`注解是构建Web应用程序的基石之一。它不仅简化了控制器的定义,还提供了一种优雅的方式来处理HTTP请求。本文将全面解析`@Controller`注解,包括其定义、用法、以及在Spring MVC中的作用。
196 2
深入解析:如何用 Spring Boot 实现分页和排序
深入解析:如何用 Spring Boot 实现分页和排序
485 2
Spring MVC中的控制器:@Controller注解全解析
在Spring MVC框架中,`@Controller`注解是构建Web应用程序控制层的核心。它不仅简化了控制器的定义,还提供了灵活的请求映射和处理机制。本文将深入探讨`@Controller`注解的用法、特点以及在实际开发中的应用。
513 0

推荐镜像

更多
  • DNS
  • AI助理

    你好,我是AI助理

    可以解答问题、推荐解决方案等

    登录插画

    登录以查看您的控制台资源

    管理云资源
    状态一览
    快捷访问