Spring第七篇【AOP】
aop术语
JoinPoint(连接点): 可以增强的方法
PointCut(切入点): 需要被增强的方法
Advice(通知/增强): 封装增强业务逻辑代码的方法
Aspect(切面): 切点+通知
Weaving(织入): 通知与切入点结合的过程
切点表达式
表达式写法
execution([修饰符] 返回值类型 包名 类名 方法名(参数列表))
- 修饰符可以省略
- 返回值类型、包名、类名、方法名可以用*代替,*表示任意
- 包名和类名之间的一个点.表示当前包,两个点..表示当前包及其子包
- 参数列表可以用两个点..表示任意个参数,任意类型
通知类型

aop以xml的方式
package com.example.demo;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAspect {
public void before() {
System.out.println("before-----------");
}
public void afterReturning(){
System.out.println("afterReturning-----------");
}
public void afterThrowing(){
System.out.println("afterThrowing-----------");
}
public void after() {
System.out.println("after-----------");
}
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("aroundBefore-----------");
Object proceed = proceedingJoinPoint.proceed();
System.out.println("aroundAfter-----------");
return proceed;
}
}
<?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 http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="target" class="com.example.demo.Target"/>
<bean id="myAspect" class="com.example.demo.MyAspect"/>
<aop:config>
<aop:pointcut id="save" expression="execution(void com.example.demo.*.save(..))"/>
<aop:aspect ref="myAspect">
<aop:before method="before" pointcut-ref="save"/>
<aop:after-returning method="afterReturning" pointcut-ref="save"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="save"/>
<aop:after method="after" pointcut-ref="save"/>
<aop:around method="around" pointcut-ref="save"/>
</aop:aspect>
</aop:config>
</beans>
aop以注解的方式
package com.example.demo.anno;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MyAspect {
//切点表达式
@Pointcut("execution(void com.example.demo.anno.*.save(..))")
public void myPoint(){}
@Before("myPoint()")
public void before() {
System.out.println("before-----------");
}
@After("myPoint()")
public void after() {
System.out.println("after-----------");
}
@Around("myPoint()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("aroundBefore-----------");
Object proceed = proceedingJoinPoint.proceed();
System.out.println("aroundAfter-----------");
return proceed;
}
@AfterThrowing("myPoint()")
public void afterThrowing(){
System.out.println("afterThrowing-----------");
}
@AfterReturning("MyAspect.myPoint()")
public void afterReturning(){
System.out.println("afterReturning-----------");
}
}
<?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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--组件扫描-->
<context:component-scan base-package="com.example.demo.anno"/>
<!--开启aop自动代理-->
<aop:aspectj-autoproxy/>
</beans>
备注:
除环绕通知外,其它通知执行顺序不一定

浙公网安备 33010602011771号