标签:扫描 表示 inf base system 切面 oid 问题 编译
方法设计:
// spring提供了ProceedingJoinPoint接口用来让我们获取切入点的信息
public Object aroundLogging(ProceedingJoinPoint pjp) {
try {
System.out.println("前置 ...");
Object ret = pjp.proceed(pjp.getArgs());
System.out.println("后置 ...");
return ret;
} catch (Throwable throwable) {
System.out.println("异常 ...");
throw new RuntimeException(throwable);
} finally {
System.out.println("最终 ...");
}
}
配置切入点
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut id="logPointcut" expression="execution(* cn.ann.service..*.*(..))"/>
</aop:config>
aop配置代码
<!-- 配置AOP -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut id="logPointcut" expression="execution(* cn.ann.service..*.*(..))"/>
<!-- 配置切面 -->
<aop:aspect id="logAdvice" ref="logger">
<!-- 前置通知
<aop:before method="beforeLogging" pointcut-ref="logPointcut"/> -->
<!-- 后置通知
<aop:after-returning method="afterReturningLogging" pointcut-ref="logPointcut"/> -->
<!-- 异常通知
<aop:after-throwing method="afterThrowingLogging" pointcut-ref="logPointcut"/> -->
<!-- 最终通知
<aop:after method="afterLogging" pointcut-ref="logPointcut"/> -->
<!-- 环绕通知 -->
<aop:around method="aroundLogging" pointcut-ref="logPointcut"/>
</aop:aspect>
</aop:config>
<context:component-scan base-package="cn.ann"/>
<aop:aspectj-autoproxy/>
配置切入点:
@Pointcut("execution(* cn.ann.service..*.*(..))")
private void logPointcut(){}
配置通知
@Before("logPointcut()")
public void beforeLogging(){
System.out.println("before logging run...");
}
@AfterReturning("logPointcut()")
public void afterReturningLogging(){
System.out.println("afterReturning logging run...");
}
@AfterThrowing("logPointcut()")
public void afterThrowingLogging(){
System.out.println("afterThrowing logging run...");
}
@After("logPointcut()")
public void afterLogging(){
System.out.println("after logging run...");
}
@Around("logPointcut()")
public Object aroundLogging(ProceedingJoinPoint pjp) {
try {
System.out.println("前置 ...");
Object ret = pjp.proceed();
System.out.println("后置 ...");
return ret;
} catch (Throwable throwable) {
System.out.println("异常 ...");
throw new RuntimeException(throwable);
} finally {
System.out.println("最终 ...");
}
}
本文代码: 此处 的 spring03-aop
标签:扫描 表示 inf base system 切面 oid 问题 编译
原文地址:https://www.cnblogs.com/ann-zhgy/p/11783644.html