引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
public interface MyCalculator {
int add(int a, int b);
}
public class MyCalculatorImpl implements MyCalculator {
public int add(int a, int b) {
return a+b;
}
}
基于xml 配置
aop.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 class="com.example.springbootaop.aspect.LogAspect" id="logAspect"/>
<bean class="com.example.springbootaop.service.impl.MyCalculatorImpl" id="myCalculator"/>
<aop:config>
<aop:pointcut id="pc1" expression="execution(* com.example.springbootaop.service.*.*(..))"/>
<aop:aspect ref="logAspect">
<aop:before method="before" pointcut-ref="pc1"/>
<aop:after method="after" pointcut-ref="pc1"/>
<aop:after-returning method="returing" pointcut-ref="pc1" returning="r"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pc1" throwing="e"/>
<aop:around method="around" pointcut-ref="pc1"/>
</aop:aspect>
</aop:config>
</beans>
引入xml 配置
@Configuration
@ImportResource(locations = "classpath:aop.xml")
public class AopConfig {
}
启动项目
@SpringBootApplication
public class SpringbootAopApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootAopApplication.class, args);
MyCalculator myCalculator = (MyCalculator) context.getBean("myCalculator");
myCalculator.add(1,3);
}
}
基于注解配置
@Component
@Aspect
public class LogAspectAnnotation {
@Pointcut("execution(* com.example.springbootaop.service.*.*(..))")
public void pc(){
}
@Before("pc()")
public void before(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
String name = signature.getName();
System.out.println(name + "方法开始执行了...");
}
@After("pc()")
public void after(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
String name = signature.getName();
System.out.println(name + "方法执行结束了...");
}
@AfterReturning(value = "pc()",returning = "returnVal")
public void returing(JoinPoint joinPoint,Object returnVal) {
Signature signature = joinPoint.getSignature();
String name = signature.getName();
}
@AfterThrowing(pointcut="pc()",throwing = "e")
public void afterThrowing(JoinPoint joinPoint,Throwable e) {
Signature signature = joinPoint.getSignature();
String name = signature.getName();
}
@Around("pc()")
public Object around(ProceedingJoinPoint pjp) {
Object proceed = null;
try {
//这个相当于 method.invoke 方法,我们可以在这个方法的前后分别添加日志,就相当于是前置/后置通知
System.out.println("around执行了1");
proceed = pjp.proceed();
System.out.println("around执行了2");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return proceed;
}
}
启动项目和xml 有相同的效果
execution 表达式
学习Spring中的aop组装切面时遇到的execution表达式,下面是execution表达式的详解
切入点表达式:
整个表达式可以分为五个部分:
1、execution(): 表达式主体。
2、第一个*号:方法返回类型, *号表示所有的类型。
3、包名:表示需要拦截的包名。
4、第二个*号:表示类名,*号表示所有的类。
5、*(…):最后这个星号表示方法名,*号表示所有的方法,后面( )里面表示方法的参数,两个句点表示任何参数
execution(* 包名..(…))
规范写法:execution(* com.baizhi.service.UserServiceImpl.*(..))重点 最通用的 //执行接口下的所有接口方法