自定义注解
package com.test.interceptor.config.annotion;
import com.test.interceptor.customannotation.Season;
import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AnnotationTst {
String model() default "";
String value() default "";
String[] code() default "";
TimeUnit timeUnit() default TimeUnit.SECONDS;
Season season() default Season.SUMMER;
Season[] enm() default {Season.SUMMER,Season.SPRING};
}
自定义拦截器
package com.test.interceptor.interceptor;
import com.test.interceptor.config.annotion.AnnotationTst;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Objects;
@Component
public class ProjectInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle");
// 判断 handler是否是HandlerMethod
if(handler instanceof HandlerMethod) {
HandlerMethod hm = (HandlerMethod)handler;
Method method = hm.getMethod();
// 判断要执行的方法是否包含自定义注解
if(hm.hasMethodAnnotation(AnnotationTst.class)) {
AnnotationTst annotationTst = method.getAnnotation(AnnotationTst.class);
if(Objects.nonNull(annotationTst)) {
System.out.println(annotationTst);
}
}
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
// boolean isAnnotation = clazz.isAnnotationPresent(SysLog.class);
// if (isAnnotation) {
// SysLog sysLog = clazz.getAnnotation((SysLog.class));
// String model = sysLog.model();
// String name = sysLog.name();
// }
// // 方法
// Method[] methods = clazz.getMethods();
// for (Method method : methods) {
// boolean isAnnotation = method.isAnnotationPresent(SysLog.class);
// if (isAnnotation) {
// System.out.println("方法:" + method.getName() + "存在自定义注解");
// SysLog sysLog = method.getAnnotation((SysLog.class));
// String model = sysLog.model();
// String name = sysLog.name();
// }
// }
// for (Field field : fields) {
// boolean isAnnotation = field.isAnnotationPresent(SysLog.class);
// if (isAnnotation) {
// System.out.println("字段:" + field.getName() + "存在自定义注解");
// SysLog sysLog = field.getAnnotation((SysLog.class));
// String model = sysLog.model();
// String name = sysLog.name();
// }
// }
// Signature signature = proceedingJoinPoint.getSignature();
// MethodSignature methodSignature = (MethodSignature) signature;
// Method method = methodSignature.getMethod();
// if (method != null) {
// SysLog sysLog = method.getAnnotation(SysLog.class);
// // 获取注解的属性值
// String[] code = sysLog.code();
// String name = sysLog.name();
// String model = sysLog.model();
// System.out.println("code:" + Arrays.toString(code));
// System.out.println("name:" + name);
// System.out.println("model:" + model);
// }
// return proceedingJoinPoint.proceed();
}
加载拦截器
package com.test.interceptor.config;
import com.test.interceptor.interceptor.ProjectInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class InterceptorConfigure implements WebMvcConfigurer {
@Autowired
private ProjectInterceptor projectInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(projectInterceptor).addPathPatterns("/student","/student/*");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
}
}
枚举类
package com.test.interceptor.customannotation;
public enum Season {
SPRING,SUMMER,AUTUMN,WINTER
}
MVC层
/**
* 查询所有数据
*
* @return 实例对象集合
*/
@GetMapping
@AnnotationTst(value="aa",model = "bb",code = {"1","2"},timeUnit = TimeUnit.DAYS,season = Season.SPRING,enm = {Season.AUTUMN,Season.WINTER})
public ResponseEntity<List> queryAll() {
return ResponseEntity.ok(this.studentService.queryAll());
}