1.中文名称
面向切面编程
2.英文名称
Aspect Oriented Programming
3.正常程序执行流程
是纵向执行流程,而AOP又叫面向切面编程,在原有纵向执行流程中添加横切面
注:不需要修改原有程序代码
高扩展性
原有功能相当于释放了部分逻辑,让职责更加明确
4.面向切面编程是什么?
- 概述
在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面过程就叫做面向切面编程. - 常用概念
原有功能: 切点,pointcut
前置通知: 在切点之前执行的功能.beforeadvice
后置通知: 在切点之后执行的功能,afteradvice
如果切点执行过程中出现异常,会触发异常通知.throwsadvice
所有功能总称叫做切面.
织入: 把切面嵌入到原有功能的过程叫做织入
5.spring 提供了 2 种 AOP 实现方式
一.Schema-based
- 每个通知都需要实现接口或类
- 配置 spring 配置文件时在 aop:config 标签配置
二.AspectJ
- 每个通知不需要实现接口或类
- 配置 spring 配置文件是在aop:config 标签 的子标签——aop:aspect 标签 中配置
6.Schema-based
1. 导入 jar包

2. 通知类
- 新建前置通知类
arg0: 切点方法对象 Method 对象
arg1: 切点方法参数
arg2:切点在哪个对象中
public class MyBeforeAdvice
implements MethodBeforeAdvice {
@Override
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("执行前置通知");
}
}
- 新建后置通知类
arg0: 切点方法返回值
arg1:切点方法对象
arg2:切点方法参数
arg3:切点方法所在类的对象
public class MyAfterAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("执行后置通知");
}
}
- 配置 spring 配置文件
①引入 aop 命名空间
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop. xsd"
②配置通知类的 bean 标签
<bean id="mybefore" class="com.youdian.advice.MyBeforeAdvice"></bean>
<bean id="myafter" class="com.youdian.advice.MyAfterAdvice"></bean>
③配置切面
<!-- 配置切面 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* com.youdian.test.Demo.demo2())"
id="mypoint"/>
<!-- 通知 -->
<aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/>
<aop:advisor advice-ref="myafter" pointcut-ref="mypoint"/>
</aop:config>
④* 通配符,匹配任意方法名,任意类名,任意一级包名
⑤ (…) 匹配任意方法参数
<?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="mybefore" class="com.youdian.advice.MyBeforeAdvice"></bean>
<bean id="myafter" class="com.youdian.advice.MyAfterAdvice"></bean>
<!-- 配置切面 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* com.youdian.test.Demo.demo2())"
id="mypoint"/>
<!-- 通知 -->
<aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/>
<aop:advisor advice-ref="myafter" pointcut-ref="mypoint"/>
</aop:config>
<!-- 配置 Demo 类,测试使用 -->
<bean id="demo" class="com.youdian.test.Demo"></bean> </beans>
- 编写测试代码
public class Test {
public static void main(String[] args) {
// Demo demo = new Demo();
// demo.demo1();
// demo.demo2();
// demo.demo3();
ApplicationContext ac = new
ClassPathXmlApplicationContext("applicationContext.xm l");
Demo demo = ac.getBean("demo",Demo.class);
demo.demo1();
demo.demo2();
demo.demo3();
}
}
本文详细介绍了面向切面编程(AOP)的概念及其在Spring框架中的应用。AOP允许在不修改原有代码的情况下,通过横切关注点增强程序功能,如日志记录、事务管理等。文章解释了切点、通知、切面、织入等核心概念,并对比了Schema-based和AspectJ两种AOP实现方式。
1万+

被折叠的 条评论
为什么被折叠?



