1、定义我们的注解:@InterfaceCheck
import java.lang.annotation.*;
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD})
@Documented
public @interface InterfaceCheck {
}
2、实现我们的切面类
package com.app.business.openapi.aopcheck;
import com.app.common.util.Assert;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
@Aspect
@Component
public class GreetingAspect {
@Around("@annotation(InterfaceCheck)")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
//request.getHeader("")来获取请求头
//判断
if (...){
//进行断言操作
}
Object result = pjp.proceed();
return result;
}
}
3、使用
@Service
@Transactional
public class ServiceImpl implements Service{
@Override
@InterfaceCheck
public void test1() {
System.out.println("test1");
}
}