编写核心业务代码(目标类的目标方法)
编写切面类,切面类中有通知(增强功能方法)
在配置文件中,配置织入关系,即将哪些通知与哪些连接点进行结合
Spring 框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。
在 spring 中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式。
aop:面向切面编程
aop底层实现:基于JDK的动态代理 和 基于Cglib的动态代理
aop的重点概念:
Pointcut(切入点):被增强的方法
Advice(通知/ 增强):封装增强业务逻辑的方法
Aspect(切面):切点+通知
Weaving(织入):将切点与通知结合的过程
开发明确事项:
谁是切点(切点表达式配置)
谁是通知(切面类中的增强方法)
将切点和通知进行织入配置
①导入 AOP 相关坐标
②创建目标接口和目标类(内部有切点)
③创建切面类(内部有增强方法)
④将目标类和切面类的对象创建权交给 spring
⑤在 applicationContext.xml 中配置织入关系
⑥测试代码
①导入 AOP 相关坐标
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.0.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.8.13version>
dependency>
②创建目标接口和目标类(内部有切点)
//接口
public interface TargetInterface {
public void method();
}
//实现类
public class Target implements TargetInterface {
@Override
public void method() {
System.out.println("Target running....");
}
}
③创建切面类(内部有增强方法)
public class MyAspect {
//前置增强方法
public void before(){
System.out.println("前置代码增强.....");
}
}
④将目标类和切面类的对象创建权交给 spring管理
<bean id="target" class="com.itheima.aop.Target">bean>
<bean id="myAspect" class="com.itheima.aop.MyAspect">bean>
⑤在 applicationContext.xml 中配置织入关系
导入aop命名空间
<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/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
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
⑤在 applicationContext.xml 中配置织入关系
配置切点表达式和前置增强的织入关系
<aop:config>
<aop:aspect ref="myAspect">
<aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.method())">aop:before>
aop:aspect>
aop:config>
⑥测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
@Autowired
private TargetInterface target;
@Test
public void test1(){
target.method();
}
}
表达式语法:
execution([修饰符] 返回值类型 包名.类名.方法名(参数))
访问修饰符可以省略
返回值类型、包名、类名、方法名可以使用星号* 代表任意
包名与类名之间一个点 . 代表当前包下的类,两个点 … 表示当前包及其子包下的类
参数列表可以使用两个点 … 表示任意个数,任意类型的参数列表
例如:
execution(public void com.itheima.aop.Target.method()) //不常用,具体的方法,限制了
execution(void com.itheima.aop.Target.*(..)) //具体的类下的所有方法都会被增强,无返回值
execution(* com.itheima.aop.*.*(..)) //常用(推荐),该包下的任意类的任意方法(参数任意)都会被增强,返回值任意
execution(* com.itheima.aop..*.*(..)) //aop下及其子包下的任意类任意方法都会被增强
execution(* *..*.*(..)) //全部都任意
通知的配置语法:
aop:通知类型>
Java类:
//增强对象
public class MyAspect {
//后置增强
public void before() {
System.out.println("前置增强................................");
}
//后置增强
public void afterReturning() {
System.out.println("后置增强................................");
}
//Proceeding JoinPoint 正在执行的链接点----切点
//环绕增强
public Object Around(ProceedingJoinPoint point) throws Throwable {
System.out.println("环绕前增强................................");
Object proceed = point.proceed();//切点方法
System.out.println("环绕后增强................................");
return proceed;
}
//异常增强
public void afterThrowing() {
System.out.println("异常增强................................");
}
//最终增强
public void after() {
System.out.println("最终增强................................");
}
}
xml:
<aop:before method="before" pointcut="execution( * com.lfs.aop.*.*(..))"/>
<aop:after-returning method="afterReturning" pointcut="execution(* com.lfs.aop.*.*(..))"/>
<aop:around method="Around" pointcut="execution(* com.lfs.aop.*.*(..))"/>
<aop:after-throwing method="afterThrowing" pointcut="execution(* com.lfs.aop.*.*(..))"/>
<aop:after method="after" pointcut="execution(* com.lfs.aop.*.*(..))"/>
当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽取后的切点表达式。
//切点表达式
<aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
<aop:config>
<aop:aspect ref="myAspect">
<aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
<aop:before method="before" pointcut-ref="myPointcut">aop:before>
aop:aspect>
aop:config>
<aop:config>
<aop:aspect ref=“切面类”>
aop:before>
aop:aspect>
aop:config>
execution([修饰符] 返回值类型 包名.类名.方法名(参数))