您当前的位置: 首页 >  spring

梁云亮

暂无认证

  • 2浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringBoot 自定义注解

梁云亮 发布时间:2020-05-31 20:16:32 ,浏览量:2

参考博客:基于反射自定义注解 代码下载地址:代码

基于AOP实现 第一步:创建SpringBoot项目,添加依赖

    org.springframework.boot
    spring-boot-starter


    org.springframework.boot
    spring-boot-starter-aop


    org.springframework.boot
    spring-boot-starter-web

第二步:注解类
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {
    String value() default "";
}
第三步:注解功能实现类
@Component
@Aspect
public class MyAnnotationAspectImpl {
    @Pointcut("@annotation(com.hc.demo.MyAnnotation)")
    private void pointCut() {
        System.out.println("pointCut");
    }
    @Around("pointCut()")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("around 1");
        try {
            joinPoint.proceed();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("around 2");
    }
    @Before("pointCut()")
    public void before() {
        System.out.println("before");
    }
    @After("pointCut()")
    public void after() {
        System.out.println("after");
    }
}
第三步: 测试Controller
@RestController
public class DemoController {
    @MyAnnotation
    @GetMapping(value = "/fun1")
    public void fun() {
        // 自己的业务实现
        System.out.println("fun1");
    }
}

在网页中请求fun1,结果: 在这里插入图片描述

基于Interceptor实现 第一步:创建SpringBoot项目,添加依赖

    org.springframework.boot
    spring-boot-starter


    org.springframework.boot
    spring-boot-starter-aop


    org.springframework.boot
    spring-boot-starter-web

第二步: 自定义注解类
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    boolean required() default true;
}
第三步:注解拦截器
@Slf4j
public class MyAnnotationInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
        if (!(object instanceof HandlerMethod)) {  // 如果不是映射到方法直接通过
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) object;
        Method method = handlerMethod.getMethod();
        //检查是否有自定义注释,有则定义相应功能
        if (method.isAnnotationPresent(MyAnnotation.class)) {
            MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
            if (myAnnotation.required()) {
                System.out.println("MyAnnotation注解被调用……");
                return true;
            }
        }
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}
第四步:拦截器配置类
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor())
                .addPathPatterns("/**");    //用于设置拦截器的过滤路径规则
    }
    @Bean
    public MyAnnotationInterceptor authenticationInterceptor() {
        return new MyAnnotationInterceptor();
    }
}
第五步: 测试Controller
@RestController
public class DemoController {
    @MyAnnotation
    @GetMapping(value = "/fun2")
    public void fun() {
        // 自己的业务实现
        System.out.println("fun2");
    }
}

在网页中请求fun2,结果: 在这里插入图片描述

关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.0434s