参考博客:基于反射自定义注解 代码下载地址:代码
基于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,结果:
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,结果: