标签:SpringBoot 示例 void joinPoint Pointcut result AOP public ###
AOP主要注解:
@Aspect,作用在类上,说明这是一个Aspect切面类。
@Pointcut,用来描述,你需要在哪些类的哪些方法中植入你的代码。
@Adive,与Pointcut配合使用,主要说明在Pointcut标记方法的什么时机执行,执行之前?执行之后?
@Pointcut express(切面表达式)
designators指示器:
匹配方法:execution() 通过什么方式去匹配哪些类的哪些方法(重点掌握)。
匹配注解:@annotation() @args() @within() @target()
匹配包/类型:within()
匹配对象:this() bean() target()
匹配参数:args()
wildcards通配符:
* 匹配任意数量的字符
.. 匹配指定类及其子类
+ 匹配任意数的子包或参数
operators运算符:&&与 ||或 !非
5中Advice
@Before
@After
@AfterReturning
@AfterThrowing
@Around
示例代码
@Aspect
@Component
public class HttpAspect {
private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);
@Pointcut("execution(public * com.imooc.controller.GirlController.*(..))")
public void log(){
}
/**
* 在截取的方法前执行
* @param joinPoint
*/
@Before("log()")
public void doBefore(JoinPoint joinPoint){
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// url
logger.info("url={}", request.getRequestURL());
// method
logger.info("method={}", request.getMethod());
// ip
logger.info("ip={}", request.getRemoteAddr());
// 类方法
logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
// 参数
logger.info("args={}", joinPoint.getArgs());
}
/**
* 获取方法返回参数
* @param object
*/
@AfterReturning(returning = "object", pointcut = "log()")
public void doAfterReturning(Object object){
logger.info("response={}", object);
}
}
Advice示例
@Aspect
@Component
public class AdviceAspectConfig {
/********PointCut********/
@Pointcut("@annotation(com.vmware.AopExecutionDemo.annotation.AdminOnlyMethod)")
public void matchAnnotation(){}
@Pointcut("execution(* com.vmware.AopExecutionDemo.service.*.find*(int))")
public void matchIntArgs(){}
@Pointcut("execution(public * com.vmware.AopExecutionDemo.service..*.*(..) throws java.lang.IllegalAccessException)")
public void matchExecption(){}
@Pointcut("execution(int com.vmware.AopExecutionDemo.service.*.*(..))")
public void matchReturn(){}
/********Advice********/
/**
* 获取方法输入的参数
* @param productId
*/
@Before("matchIntArgs() && args(productId)")
public void before(int productId){
System.out.println("### before ### productId = [" + productId + "]");
}
/**
* 获取方法返回参数
* @param result
*/
@AfterReturning(value = "matchReturn()", returning = "result")
public void after(int result){
System.out.println("### after ###:" + result);
}
/**
* Before + After + AfterReturning
* @param joinPoint
* @return
* @throws Throwable
*/
@Around("matchReturn()")
public Object after(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("### before ###");
Object result;
result = joinPoint.proceed(joinPoint.getArgs());
System.out.println("### after ###" + result);
return result;
}
}
@annotation示例
@Aspect
@Component
public class AnnotationAspectConfig {
/**
* 匹配带有 AdminOnlyMethod Annotation的方法
*/
@Pointcut("@annotation(com.vmware.AopExecutionDemo.annotation.AdminOnlyMethod)")
public void matchAnnotation(){}
@Before("matchAnnotation()")
public void before(){
System.out.println("###matchAnnotation && Before");
}
}
@Aspect
@Component
public class AnnotationClassAspectConfig {
/**
* 匹配带有 AdminOnlyClass Annotation的类
*/
@Pointcut("@within(com.vmware.AopExecutionDemo.annotation.AdminOnlyClass)")
public void annotationClassCondition(){}
@Before("annotationClassCondition()")
public void before(){
System.out.println("###annotationClass && before");
}
}
标签:SpringBoot,示例,void,joinPoint,Pointcut,result,AOP,public,### 来源: https://www.cnblogs.com/vincenshen/p/10427872.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。