主要学习Spring的AOP介绍、基于XML的AOP开发、基于注解的AOP开发。
Spring AOP的简介
一、AOP的基本概念
1、定义
AOP是Aspect	Oriented	Programming的缩写,意思是面向切面编程,是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生泛型。利用AOP可以对业务的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
动态代理:
- 优点:在不修改源码的基础上对目标方法进行相应的增强。
- 作用:完成程序功能间的松耦合。
2、AOP的作用与优势
(1)作用:在程序运行期间,在不修改源码的情况下对方法进行功能增强。
(2)优势:减少重复代码,提高开发效率,并且便于维护。
3、AOP的底层实现
实际上,AOP的底层是通过Spring提供的动态代理技术实现的。在运行期间,Spring通过动态代理技术动态的生成代理对象,代理对象方法执行时进行增强功能介入,在去调用目标对象的方法,从而完成功能的增强。
常用的动态代理技术:
- JDK代理:基于接口的动态代理技术
- cglib代理:基于父类的动态代理技术
4、JDK的代理实现
(1)创建目标接口和目标类
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | public interface TargetInterface {
 void save();
 }
 
 
 public class Target implements TargetInterface {
 public void save() {
 System.out.println("save...");
 }
 }
 
 | 
(2)创建增强类
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | public class Advice {public void before(){
 System.out.println("前置增强...");
 }
 
 public void afterReturning(){
 System.out.println("后置增强...");
 }
 }
 
 | 
(3)编写测试增强方法
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 
 | public class ProxyTest {public static void main(String[] args) {
 
 final Target target = new Target();
 
 
 final Advice advice = new Advice();
 
 
 
 TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(
 target.getClass().getClassLoader(),
 target.getClass().getInterfaces(),
 new InvocationHandler() {
 
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 advice.before();
 Object invoke = method.invoke(target, args);
 advice.afterReturning();
 return invoke;
 }
 }
 );
 
 
 proxy.save();
 }
 }
 
 
 
 
 
 
 
 | 
5、cglib代理实现
(1)创建代理对象
| 12
 3
 4
 5
 
 | public class Target {public void save() {
 System.out.println("save...");
 }
 }
 
 | 
(2)创建增强类
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | public class Advice {public void before(){
 System.out.println("前置增强...");
 }
 
 public void afterReturning(){
 System.out.println("后置增强...");
 }
 }
 
 | 
(3)创建测试增强方法
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 
 | public class ProxyTest {public static void main(String[] args) {
 
 final Target target = new Target();
 
 
 final Advice advice = new Advice();
 
 
 
 Enhancer enhancer = new Enhancer();
 
 enhancer.setSuperclass(Target.class);
 
 enhancer.setCallback(new MethodInterceptor() {
 public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
 advice.before();
 Object invoke = method.invoke(target, objects);
 advice.afterReturning();
 return invoke;
 }
 });
 
 Target proxy = (Target) enhancer.create();
 
 proxy.save();
 }
 }
 
 
 
 
 
 
 
 | 
二、AOP的相关术语
Spring的AOP实现底层就是对上面的动态代理的代码进行了封装,feng’zhuang’zhi’ho我们只需要对需要关注的部分进行代码编写,并通过配置的方式完成指定目标的方法增强。
在AOP操作之前,需要先了解AOP的常用术语,常用相关术语如下:
- Target(目标对象):代理的目标对象
- Proxy(代理):一个类被AOP织入增强后,就会产生一个结果代理类
- Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在Spring中,这些点指的是方法,因为Spring只支持方法类型的连接点(可以理解为可以被增强的方法)
- Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义(可以理解为实际被增强的方法)
- Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知(增强的逻辑/过程)
- Aspect(切面):是切入点和通知(引介)的结合
- Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程。Spring采用动态代理织入,而Aspectj采用编译期织入和类装载期织入(将切点和增强结合的过程即为织入的过程)
三、AOP开发明确的事项
1、需要编写的内容
(1)编写核心业务代码(目标类的目标方法)
(2)编写切面类,切面类中有通知(增强功能方法)
(3)在配置文件中,配置织入关系,即将哪些通知与哪些连接点进行结合
2、AOP技术实现的内容
Spring框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。
3、AOP底层使用哪种代理方式
在Spring中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式。
四、知识要点
(1)AOP:面向切面编程
(2)AOP底层实现:基于JDK的动态代理和基于Cglib的动态代理
(3)AOP的中带你概念:
				Pointcut(切入点:)被增强的方法
				Advice(通知/增强):封装增强业务逻辑的方法
				Aspect(切面):切点 + 通知
				Weaving(织入):将切点与通知结合的过程
(4)开发明确事项:
				谁是切点(切点表达式配置)
				谁是通知(切面类中的增强方法)
				将切点和通知进行织入配置
基于XML的AOP开发
一、基于XML开发的快速入门
1、导入AOP相关坐标;
2、创建目标接口和目标类(内部有切点);
3、创建切面类(内部有增强方法);
4、将目标类和切面类的对象创建权交给Spring
5、在applicationContext.xml中配置织入关系
6、测试代码
二、基于XML开发的代码实现
1、配置接口和目标类
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | public interface TargetInterface {
 void save();
 }
 
 
 public class Target implements TargetInterface {
 public void save() {
 System.out.println("save...");
 }
 }
 
 | 
2、配置(通知/增强)
| 12
 3
 4
 5
 
 | public class MyAspect {public void before(){
 System.out.println("前置增强...");
 }
 }
 
 | 
3、配置xml文件
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 
 | <?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="aop.Target"/>
 
 <bean id="myAspect" class="aop.MyAspect"/>
 
 <aop:config>
 
 <aop:aspect ref="myAspect">
 
 <aop:before method="before" pointcut="execution(public void aop.Target.save())"/>
 </aop:aspect>
 </aop:config>
 </beans>
 
 | 
4、配置测试类
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | @RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")
 public class AopTest {
 @Autowired
 private TargetInterface targetInterface;
 
 @Test
 public void aopTest(){
 targetInterface.save();
 }
 }
 
 
 
 
 
 
 | 
三、切点表达式的写法
1、全通配写法
*  * ..*.*(..)
返回值可以使用通配符,表示任意返回值
* aop.Target.save()
包名可以使用通配符,表示任意包。但是有几级包,就需要写几个*.
例如:假设上面的save()方法前面有四个包就需要写成:
* *.*.*.*.Target.save()
但包名还可以简写为..表示当前包及其子包
* *..*.Target.save()
类名和方法名都可以使用*来实现通配
即:* *..*.*.*()
参数列表可以使用..来通配,但也可以直接使用数据类型
基本数据类型直接写名称
应用类型写包名.类名的方式
2、实际开发中切入点表达式的通常写法
切到业务层表现类下的所有方法
例:	*	aop.*.*(..)
3、切点表达式的抽取
在配置文件的过程中,切点表达式会有很多重复的部分,所以可以将重复的部分抽取出来,便于维护。
在增强中使用pointcut-ref属性代替pointcut属性来引用抽取后的切点表达式。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | <aop:config>
 <aop:aspect ref="myAspect">
 
 <aop:pointcut id="myPointcut" expression="execution(* aop.*.*(..))"/>
 
 <aop:before method="before" pointcut-ref="myPointcut"/>
 <aop:after-returning method="afterRunning" pointcut-ref="myPointcut"/>
 <aop:around method="around" pointcut-ref="myPointcut"/>
 <aop:after-throwing method="afterThrowing" pointcut-ref="myPointcut"/>
 <aop:after method="after" pointcut-ref="myPointcut"/>
 </aop:aspect>
 </aop:config>
 
 | 
四、AOP的通知种类
1、通知的类型
通知的配置语法:
| 1
 | <aop:通知类型	method="切面类中方法名"	pointcut="切点表达式"></aop:通知类型>
 | 
| 名称 | 标签 | 说明 | 
| 前置通知 | <aop:before> | 用于配置前置通知,指定增强的方法在切入点方法之前执行 | 
| 后置通知 | <aop:after-returning> | 用于配置后置通知。指定增强的方法在切入点方法之后执行 | 
| 环绕通知 | <aop:around> | 用于配置环绕通知。指定增强的方法在切入点方法之前和之后都执行 | 
| 异常抛出通知 | <aop:throwing> | 用于配置异常抛出通知。指定增强的方法在出现异常时执行 | 
| 最终通知 | <aop:after> | 用于配置最终通知。无论增强方式执行是否有异常都会执行 | 
2、代码演示
(1)配置增强类
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 
 | public class MyAspect {
 
 
 public void before(){
 System.out.println("前置增强...");
 }
 
 
 
 
 public void afterRunning(){
 System.out.println("后置增强...");
 }
 
 
 
 
 
 
 
 
 public Object around(ProceedingJoinPoint pjp) throws Throwable {
 System.out.println("环绕前增强...");
 
 Object proceed = pjp.proceed();
 System.out.println("环绕后增强...");
 return proceed;
 }
 
 
 
 
 public void afterThrowing(){
 System.out.println("异常抛出增强...");
 }
 
 
 
 
 public void after(){
 System.out.println("最终增强...");
 }
 }
 
 | 
(2)配置目标类
| 12
 3
 4
 5
 6
 
 | public class Target implements TargetInterface {public void save() {
 
 System.out.println("save...");
 }
 }
 
 | 
(3)配置xml文件
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 
 | <?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="aop.Target"/>
 
 <bean id="myAspect" class="aop.MyAspect"/>
 
 <aop:config>
 
 <aop:aspect ref="myAspect">
 
 <aop:before method="before" pointcut="execution(* aop.*.*(..))"/>
 <aop:after-returning method="afterRunning" pointcut="execution(* aop.*.*(..))"/>
 <aop:around method="around" pointcut="execution(* aop.*.*(..))"/>
 <aop:after-throwing method="afterThrowing" pointcut="execution(* aop.*.*(..))"/>
 <aop:after method="after" pointcut="execution(* aop.*.*(..))"/>
 </aop:aspect>
 </aop:config>
 </beans>
 
 | 
(4)编写测试类
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 
 | @RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")
 public class AopTest {
 @Autowired
 private TargetInterface targetInterface;
 
 @Test
 public void aopTest(){
 targetInterface.save();
 }
 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 
五、知识要点、
1、AOP织入的配置
| 12
 3
 4
 5
 
 | <aop:config><aop:aspect ref="切面类">
 <aop:before method="通知方法名称" pointcut="切点表达式"/>
 </aop:aspect>
 </aop:config>
 
 | 
2、通知的类型
前置通知、后置通知、环绕通知、异常抛出通知、最终通知
3、切点表达式的写法
| 1
 | execution([修饰符] 返回值类型 包名.类名.方法名(参数))
 | 
基于注解的AOP开发
一、基于注解的AOP开发快速入门
1、基于注解的AOP开发的步骤
(1)创建目标接口和目标类(内部有切点)
(2)创建切面类(内部有增强方法)
(3)将目标类和切面类的对象创建权交给Spring
(4)在切面类中使用注解配置织入关系
(5)在配置文件中开启组件扫描和AOP的自动代理
(6)测试
2、基于注解的AOP开发的代码演示
(1)创建目标接口和目标类(内部有切点)
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | public interface TargetInterface {
 void save();
 }
 
 
 @Component("target")
 public class Target implements TargetInterface {
 public void save() {
 System.out.println("save...");
 }
 }
 
 | 
(2)创建切面类(内部有增强方法)
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | @Component("myAspect")@Aspect
 public class MyAspect {
 
 
 
 @Before("execution(* anno.*.*(..))")
 public void before(){
 System.out.println("前置增强...");
 }
 }
 
 | 
(3)编写测试类
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 
 | @Configuration
 @ComponentScan("com.itheima")
 @Import({DataSourceConfiguration.class})
 public class SpringConfiguration {
 }
 
 
 @PropertySource("jdbc.properties")
 public class DataSourceConfiguration {
 
 @Value("${jdbc.driver}")
 private String driver;
 @Value("${jdbc.url}")
 private String url;
 @Value("${jdbc.username}")
 private String user;
 @Value("${jdbc.password}")
 private String password;
 
 @Bean("dataSource")
 public DataSource getDataSource() throws PropertyVetoException {
 ComboPooledDataSource dataSource = new ComboPooledDataSource();
 dataSource.setDriverClass(driver);
 dataSource.setJdbcUrl(url);
 dataSource.setUser(user);
 dataSource.setPassword(password);
 return dataSource;
 }
 }
 
 | 
(4)测试
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 
 | @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(classes = {SpringConfiguration.class})
 public class SpringJunitTest {
 
 @Autowired
 private UserService userService;
 @Autowired
 private UserDao userDao;
 
 @Test
 public void test2(){userDao.save();}
 @Test
 public void test1(){
 userService.save();
 }
 }
 
 | 
二、注解配置AOP的类型
通知的配置语法:@通知注解(”切点表达式“)
| 名称 | 注解 | 说明 | 
| 前置通知 | @Before | 用于配置前置通知,指定增强的方法在切入点方法之前执行 | 
| 后置通知 | @AfterReturning | 用于配置后置通知,指定增强的方法在切入点方法之后执行 | 
| 环绕通知 | @Around | 用于配置环绕通知,指定增强的方法在切入点方法之前和之后都执行 | 
| 异常抛出通知 | @AfterThrowing | 用于配置异常抛出通知,指定增强的方法在出现异常时执行 | 
| 最终通知 | @After | 用于配置最终通知,无论增强方式执行是否有异常都会执行 | 
三、切点表达式的抽取
和XMl配置AOP一样,可以将切点表达式抽取,抽取方式实在切面内定义方法,在该方法上使用@Pointcut注解定义切点表达式,然后再在增强注解中进行引用。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | @Component("myAspect")@Aspect
 public class MyAspect {
 
 @Pointcut("execution(* anno.*.*(..))")
 public void myPoint(){}
 
 
 
 
 
 @Before("myPoint()")
 public void before(){
 System.out.println("前置增强...");
 }
 }
 
 | 
四、基于注解开发的知识要点
1、注解AOP开发步骤
(1)使用**@Aspect**标注切面类
(2)使用**@通知注解**标注通知方法
(3)在配置文件中配置组件扫描和AOP自动代理
| 12
 3
 
 | <aop:aspectj-autoproxy/>
 
 
 | 
参考资料:2020年 最新版 传智黑马Java SSM 阶段 采用IDEA教学