1.配置注解 (作用于方法上,相当于要告诉aop对哪些方法做切面植入)
import javax.jdo.annotations.Element;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AspectPointCutTag {
String name();
}
2.做一个aop切面(就是知道对哪些方法做植入,切面就是植入的内容或者要做的事情方法)
@Aspect
@Component
public class LogAspectWave {
@Pointcut("@annotation(com.service.example.common.annotation.AspectPointCutTag)")
public void annotaionPointCut(){
PaasLogger.info("开启切面日志记录");
}
@Before("annotaionPointCut()")
public void bofore(JoinPoint joinPoint){
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
AspectPointCutTag aspectPointCutTag = methodSignature.getMethod().getAnnotation(AspectPointCutTag.class);
PaasLogger.info("前置通知:"+aspectPointCutTag.name());
}
@Before("annotaionPointCut()")
public void after(){
}
}
3.给方法配置切入点
@Controller
@EnableAspectJAutoProxy
public class ExampleApp {
@Autowired
private ExampleService service;
/**
* 保存客户订单
* @param req
* @return
*/
@PostMapping("/insert")
@AspectPointCutTag(name="insetCustomerOrder")
public void insert(){
//PaasLogger.info("入参报文:"+ req.toString());
//TODO 将改为切面日志
service.createCustomerOrder(req.getBody());
SaveCustomerOrderRspDTO rspDTO = new SaveCustomerOrderRspDTO();
//PaasLogger.info("出参报文:"+ rspDTO.toString());
}
}
标签:info,void,AspectPointCutTag,切面,AOP,SpringAOP,public,PaasLogger From: https://blog.51cto.com/ratelcloud/7453287