package com.li.utils;
/*
* 用于提供日志的工具类,抽取公共代码
* */
public class Logger {
/**
* @Description: 打印日志,在切入点方法执行之前进行
* @Param:
* @return:
* @Date: 2020/5/14
*/
public void printLog(){
System.out.println("开始记录日志了");
}
}
package com.li.aop;
public class AccountServiceImpl implements AccountService {
public void safeCount() {
System.out.println("账户存储");
}
public void createCount(int i) {
System.out.println("新建账户");
}
public int deleteCount() {
System.out.println("删除账户");
return 0;
}
}
<bean id="accountService" class="com.li.aop.AccountServiceImpl">bean>
<bean id="logger" class="com.li.utils.Logger">bean>
<bean id="logger" class="com.li.utils.Logger">bean>
<aop:config>
<aop:aspect id="logAdvice" ref="logger">
<aop:before method="printLog" pointcut="execution(public void com.li.aop.AccountServiceImpl.safeCount())">aop:before>
aop:aspect>
aop:config>
package com.li.utils;
/*
* 用于提供日志的工具类,抽取公共代码
* */
public class Logger {
/**
* @Description: 打印日志,在切入点方法执行之前进行
* @Param:
* @return:
* @Author: Mr.Wang
* @Date: 2020/5/14
*/
//前置通知
public void beforePrintLog(){
System.out.println("前置通知开始记录日志了");
}
//后置通知
public void afterReturningPrintLog(){
System.out.println("后置通知记录日志了");
}
//异常通知
public void afterThrowingPrintLog(){
System.out.println("异常通知开始记录日志了");
}
//最后通知
public void afterPrintLog(){
System.out.println("最后通知开始记录日志了");
}
}
<bean id="logger" class="com.li.utils.Logger">bean>
<aop:config>
<aop:aspect id="logAdvice" ref="logger">
<aop:before method="beforePrintLog" pointcut-ref="service">aop:before>
<aop:after-returning method="afterReturningPrintLog" pointcut-ref="service">aop:after-returning>
<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="service">aop:after-throwing>
<aop:after method="afterPrintLog" pointcut-ref="service">aop:after>
<aop:pointcut id="service" expression="execution(* com.li.aop.*.*(..))">aop:pointcut>
aop:aspect>
aop:config>
如果环绕通知方法仅如下编写,则运行时不会运行切入点。
public void arroundPrintLog(){
System.out.println("环绕通知开始记录日志了");
}
<aop:around method="arroundPrintLog" pointcut-ref="service">aop:around>
public Object arroundPrintLog(ProceedingJoinPoint point){
Object rtvalue = null;
try {
Object[] args = point.getArgs();//得到方法执行所需的参数
System.out.println("前置通知开始记录日志了");
rtvalue = point.proceed(args);
System.out.println("后置通知记录日志了");
return rtvalue;
} catch (Throwable throwable) {
System.out.println("异常通知开始记录日志了");
throw new RuntimeException(throwable);
}finally {
System.out.println("最终通知开始记录日志了");
}
}
package com.li.utils;
import com.li.aop.AccountServiceImpl;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
/*
* 用于提供日志的工具类,抽取公共代码
* */
@Configuration
@Import(AccountServiceImpl.class)
@EnableAspectJAutoProxy
@Component("logger")
@Aspect
public class Logger {
@Pointcut("execution(* com.li.aop.*.*(..))")
private void serviceMethod(){};
/* //前置通知
@Before("serviceMethod()")
public void beforePrintLog(){
System.out.println("前置通知开始记录日志了");
}
//后置通知
@AfterReturning("serviceMethod()")
public void afterReturningPrintLog(){
System.out.println("后置通知记录日志了");
}
//异常通知
@AfterThrowing("serviceMethod()")
public void afterThrowingPrintLog(){
System.out.println("异常通知开始记录日志了");
}
//最后通知
@After("serviceMethod()")
public void afterPrintLog(){
System.out.println("最后通知开始记录日志了");
}*/
//环绕通知
@Around("serviceMethod()")
public Object arroundPrintLog(ProceedingJoinPoint point){
Object rtvalue = null;
try {
Object[] args = point.getArgs();//得到方法执行所需的参数
System.out.println("前置通知开始记录日志了");
rtvalue = point.proceed(args);
System.out.println("后置通知记录日志了");
return rtvalue;
} catch (Throwable throwable) {
System.out.println("异常通知开始记录日志了");
throw new RuntimeException(throwable);
}finally {
System.out.println("最终通知开始记录日志了");
}
}
}