- 项目结构(proxy文件夹没用)
- bean.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="accountService" class="com.itheima.service.Impl.AccountServiceImpl"/>
<bean id="logger" class="com.itheima.utils.Logger"/>
<aop:config>
<aop:aspect id="logAdvice" ref="logger">
<aop:pointcut id="pt1" expression="execution(* com.itheima.service.Impl.*.*(..))"/>
<aop:before method="beforePrintLog" pointcut="execution(* com.itheima.service.Impl.*.*(..))"/>
<aop:after-returning method="afterReturningPrintLog" pointcut="execution(* com.itheima.service.Impl.*.*(..))"/>
<aop:after-throwing method="afterThrowingPringLog" pointcut="execution(* com.itheima.service.Impl.*.*(..))"/>
<aop:after method="afterPrintLog" pointcut="execution(* com.itheima.service.Impl.*.*(..))"/>
<aop:around method="aroundPrintLog" pointcut="execution(* com.itheima.service.Impl.*.*(..))"/>
<aop:before method="beforePrintLog" pointcut-ref="pt1"/>
<aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"/>
<aop:after-throwing method="afterThrowingPringLog" pointcut-ref="pt1"/>
<aop:after method="afterPrintLog" pointcut-ref="pt1"/>
<aop:around method="aroundPrintLog" pointcut-ref="pt1"/>
</aop:aspect>
</aop:config>
</beans>
- Logger.java
package com.itheima.utils;
import org.aspectj.lang.ProceedingJoinPoint;
public class Logger {
public void beforePrintLog(){
System.out.println("前置通知:Logger类中的printLog方法开始记录日志了");
}
public void afterReturningPrintLog(){
System.out.println("后置通知:Logger类中的printLog方法开始记录日志了");
}
public void afterThrowingPringLog(){
System.out.println("异常通知:Logger类中的printLog方法开始记录日志了");
}
public void afterPrintLog(){
System.out.println("最终通知:Logger类中的printLog方法开始记录日志了");
}
public Object aroundPrintLog(ProceedingJoinPoint pjp) throws Throwable {
Object returnValue = null;
try{
Object [] args = pjp.getArgs();
System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。前置");
returnValue = pjp.proceed(args);
System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。后置");
return returnValue;
}catch (Exception e) {
System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。异常");
throw new RuntimeException(e);
}finally {
System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。最终");
}
}
}
- IAccountService接口
package com.itheima.service;
public interface IAccountService {
void saveAccount();
void updateAccount(int i);
int deleteAccount();
}
- AccountServiceImpl.java
package com.itheima.service.Impl;
import com.itheima.service.IAccountService;
public class AccountServiceImpl implements IAccountService {
public void saveAccount() {
System.out.println("执行了保存");
}
public void updateAccount(int i) {
System.out.println("执行了更新" + i);
}
public int deleteAccount() {
System.out.println("执行了删除");
return 0;
}
}