两种实现方式
公共jar包
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
2.方式一:配置bean切入
通过配置文件标签,对特定的类进行特定方法的切入:
2.1创建日志切面类
public class LogManager {
//自定义日志方法
public void before(){
System.out.println("方法开始执行-----------");
}
//after 后置通知
public void after(){
System.out.println("----------after-----------");
}
public void afterReturning(){
System.out.println("----------afterReturning-----------");
}
public void afterThrowing(){
System.out.println("----------afterThrowing-----------");
}
/*
环绕通知
*/
public void around(ProceedingJoinPoint point){
//前置通知
System.out.println("-----around before---------");
try {
point.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
//后置通知
System.out.println("-----around after---------");
}
}
2.2 配置日志切面类
日志切面类
<bean id="logManager" class="com.lanou.util.LogManager"></bean>
配置切入点
<aop:config>
<aop:pointcut id="p1" expression="execution(* com.lanou.service.impl.*.*(..))"></aop:pointcut>
将切面和切入点关联
<!--自定义切面 ref="切面对象 "-->
<aop:aspect ref="logManager">
<!--前置通知 method="日志切面中的方法" pointcut-ref="切入点名称"-->
<aop:before method="before" pointcut-ref="p1"></aop:before>
<!--后置通知,在目标方法调用之后执行-->
<!--在方法成功返回之后执行-->
<aop:after-returning method="afterReturning" pointcut-ref="p1"></aop:after-returning>
<!--在方法调用之后执行,无论是否成功-->
<aop:after method="after" pointcut-ref="p1"></aop:after>
<!--在方法遇到异常时执行-->
<aop:after-throwing method="afterThrowing" pointcut-ref="p1"></aop:after-throwing>
<!--环绕通知-->
<aop:around method="around" pointcut-ref="p1"></aop:around>
</aop:aspect>
<!--预定义切面-->
<!--<aop:advisor advice-ref=""></aop:advisor>--> </aop:config>
可在方法中输出内容,自定义1/0异常测试
方式二 注解模式实现
1.开启注解扫描
开启注解扫描
<context:annotation-config></context:annotation-config>
定义注解扫描的包
<context:component-scan base-package="com.lanou"></context:component-scan>
开启aop注解的支持 开启aop自动代理
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
2. 只配置日志切面类
//将当前类指定为切面类
@Aspect
@Component
public class LogManager {
//定义切入点表达式 切入点名字为方法名
@Pointcut("execution(* com.lanou.service.impl.*.*(..))")
public void pc(){
}
//自定义日志方法
@Before("execution(* com.lanou.service.impl.*.*(..))")
public void before(){
System.out.println("方法开始执行-----------");
}
//after 后置通知
@After("execution(* com.lanou.service.impl.*.*(..))")
public void after(){
System.out.println("----------after-----------");
}
@AfterReturning("LogManager.pc()")
public void afterReturning(){
System.out.println("----------afterReturning-----------");
}
@AfterThrowing("LogManager.pc()")
public void afterThrowing(){
System.out.println("----------afterThrowing-----------");
}
/*
环绕通知
*/
@Around("LogManager.pc()")
public void around(ProceedingJoinPoint point){
//前置通知
System.out.println("-----around before---------");
try {
point.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
//后置通知
System.out.println("-----around after---------");
}
}