Spring AOP
AOP的原理,就是生成对象的代理,然后通过在代理的执行中,添加一些钩子来扩展功能。
@Aspect
public class MyAspect {
@Pointcut("execution(* cn.kbug.code.service.impl.*.*(..))")
public void pointCut(){
}
@Around("pointCut()")
public void around(ProceedingJoinPoint pjp) throws Throwable {
pjp.proceed();
System.out.println("around");
}
@Before("pointCut()")
public void before(){
System.out.println("before .. ");
}
@After("pointCut()")
public void after(){
System.out.println(" after ..");
}
@AfterReturning("pointCut()")
public void afterReturning(){
System.out.println(" after returning ..");
}
@AfterThrowing("pointCut()")
public void afterThrowing(){
System.out.println(" afterThrowing ..");
}
}
本文详细介绍了Spring AOP的原理和实践,通过@Aspect注解定义切面,使用@Pointcut定义切入点表达式,结合@Before、@After、@AfterReturning和@AfterThrowing实现方法执行前、后及异常处理的增强操作。示例代码展示了如何在方法调用的各个阶段插入自定义逻辑。
503

被折叠的 条评论
为什么被折叠?



