在pom.xml文件中需要导入依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
新建一个用于测试的Service类
public class UserService {
public void addUser(){
System.out.println("增加用户");
}
public void deleteUser(){
System.out.println("删除用户");
}
}
方式一:使用Spring的API接口
Spring AOP 提供了 Advice 接口多个子接口来支持增强。如下所示:
接口 MethodBeforeAdvice:在目标方法调用之前调用的;
接口 AfterReturningAdvice:在目标方法调用并返回之后调用;
接口 MethodInterceptor:在目标方法的执行前后有效;
接口 ThrowsAdvice:在目标方法抛出异常时调用;
第一步:
新建AfterReturningLog实现AfterReturningAdvice接口
public class AfterReturningLog implements AfterReturningAdvice {
//returnValue: 返回值
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了" + method.getName() + "返回结果是:" + returnValue);
}
}
新建BeforeLog实现MethodBeforeAdvice接口
public class BeforeLog implements MethodBeforeAdvice {
//method: 要执行的目标对象的方法
//args: 参数
//target: 目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName() + "的" + method.getName() + "被执行了");
}
}
第二步:
在applicationContext.xml配置文件中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册bean -->
<bean id="userService" class="com.zhangzhilin.service.UserService"></bean>
<bean id="beforeLog" class="com.zhangzhilin.log.BeforeLog"/>
<bean id="afterReturningLog" class="com.zhangzhilin.log.AfterReturningLog"/>
<!-- 配置aop-->
<aop:config>
<!-- 切入点:expression:表达式 execution(要执行的位置)-->
<aop:pointcut id="pointcut" expression="execution(* com.zhangzhilin.service.UserService.*(..))"/>
<!-- 配置环绕增强 -->
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterReturningLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
方式二:自定义类实现AOP
第一步:
新建DiyLog类作为切面
public class DiyLog {
public void before(){
System.out.println("方法执行前");
}
public void after(){
System.out.println("方法执行后");
}
}
第二步:
在applicationContext.xml配置文件中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册bean -->
<bean id="diyAspect" class="com.zhangzhilin.log.DiyLog"/>
<!-- 配置aop-->
<aop:config>
<!-- 配置切面 -->
<aop:aspect ref="diyAspect">
<aop:pointcut id="pointcut" expression="execution(* com.zhangzhilin.service.UserService.*(..))"/>
<!-- 这里的method指向自定义类中的方法 -->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>