spring 注解和xml 两版本实现spring-aop
xml
通知类
package com.zho.utils;
import org.aspectj.lang.ProceedingJoinPoint;
public class Logger {
public void beforePrintLog() {
System.out.println("Logger.beforePrintLog");
}
public void afterReturningPrintLog() {
System.out.println("Logger.afterReturningPrintLog");
}
public void afterThrowingPrintLog() {
System.out.println("Logger.afterThrowingPrintLog");
}
public void afterPrintLog() {
System.out.println("Logger.afterPrintLog");
}
public Object aroundPrintLog(ProceedingJoinPoint point){
Object proceed = null;
try {
System.out.println("Logger.aroundPrintLog---前置");
Object[] args = point.getArgs();
proceed = point.proceed(args);
System.out.println("Logger.aroundPrintLog---after_returning");
return proceed;
} catch (Throwable throwable) {
System.out.println("Logger.aroundPrintLog---异常");
throw new RuntimeException(throwable);
} finally {
System.out.println("Logger.aroundPrintLog --- return");
}
}
}
xml环绕通知
方法需要一个参数ProceedingJoinPoint point
一个Object[]数组接受point中的args
point.proceed(args);执行方法,此方法就相当于明确调用切入点方法.
public Object aroundPrintLog(ProceedingJoinPoint point){
Object proceed = null;
try {
System.out.println("Logger.aroundPrintLog---前置");
Object[] args = point.getArgs();
proceed = point.proceed(args);
System.out.println("Logger.aroundPrintLog---after_returning");
return proceed;
} catch (Throwable throwable) {
System.out.println("Logger.aroundPrintLog---异常");
throw new RuntimeException(throwable);
} finally {
System.out.println("Logger.aroundPrintLog --- return");
}
}
bean配置 aop:config
aop:config 中一个pointcut 切入点表达式
一个aspect 配置被监听的类,实现aop
<!-- spring中居于xml的aop配置步骤-->
<bean id="logger" class="com.zho.utils.Logger">
</bean>
<aop:config>
<aop:pointcut id="tx" expression="execution(* com.zho.service.impl.*.*(..) )"/>
<aop:aspect id="logAdvice" ref="logger">
<!-- <aop:before method="beforePrintLog" pointcut-ref="tx"/>-->
<!-- <aop:after-returning method="afterReturningPrintLog" pointcut-ref="tx"/>-->
<!-- <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="tx"/>-->
<!-- <aop:after method="afterPrintLog" pointcut-ref="tx"/>-->
<aop:around method="aroundPrintLog" pointcut-ref="tx" />
</aop:aspect>
</aop:config>
anno
logger配置 需要有Componect注解,加入ioc中
开启Aspect注解 如不在通知类上使用@Aspect则可以在bean中加入
<aop:aspectj-autoproxy/>
开启注解使用实现aop通知开启
anno需要有一个切入点方法
实现通知的方式,在方法上使用注解注明通知方式
@Pointcut("execution(* com.zho.service.impl.*.*(..))")
private void tx() {
}
@Component("logger")
@Aspect
public class Logger {
@Pointcut("execution(* com.zho.service.impl.*.*(..))")
private void tx() {
}
// @Before("tx()")
public void beforePrintLog() {
System.out.println("Logger.beforePrintLog");
}
// @AfterReturning("tx()")
public void afterReturningPrintLog() {
System.out.println("Logger.afterReturningPrintLog");
}
// @AfterThrowing("tx()")
public void afterThrowingPrintLog() {
System.out.println("Logger.afterThrowingPrintLog");
}
// @After("tx()")
public void afterPrintLog() {
System.out.println("Logger.afterPrintLog");
}
@Around("tx()")
public Object aroundPrintLog(ProceedingJoinPoint point) {
Object proceed = null;
try {
System.out.println("Logger.aroundPrintLog---前置");
Object[] args = point.getArgs();
proceed = point.proceed(args);
System.out.println("Logger.aroundPrintLog---after_returning");
return proceed;
} catch (Throwable throwable) {
System.out.println("Logger.aroundPrintLog---异常");
throw new RuntimeException(throwable);
} finally {
System.out.println("Logger.aroundPrintLog --- return");
}
}
}
配置类
开启spring注解 EnableAspectJAutoProxy
@Configuration
@ComponentScan("com.zho")
@EnableAspectJAutoProxy
public class SpringConfiguration {
}