SpringAOP

本文详细介绍了AOP(面向切面编程)的概念,包括其核心思想、术语解释以及在Spring框架中的实现方式。通过租房业务场景,展示了如何使用注解和XML配置实现前置通知、后置通知、环绕通知等增强功能。AOP的主要目的是解耦核心业务和辅助业务,简化代码维护。
摘要由CSDN通过智能技术生成

AOP简介

AOP(Aspect Object Programmming) 面向切面编程,是一种面向对象编程OOP的一种补充,底层是动态代理的应用。
采用了两种代理:

  • JDK的动态代理
  • CGLIB的动态代理

个人理解就是所谓的面向切面,通俗的讲就是将业务分为核心业务和其他辅助业务

核心业务

  1. 核心业务:就是像转账,支付,预约,登录,删除等真实的操作数据的业务
  2. 辅助性业务:类似于系统日志,事务管理,性能分析等非必要的,不直接操作数据的一类业务

故总结来说就是AOP的作用是为了解耦,是的核心业务就是专门做核心业务,其余的辅助性业务用切面的思想横切进核心业务里。

  • 实现了程序之间的解耦合
  • 降低了非核心业务编写的次数
  • 利于后期代码的扩展与维护

AOP的编程术语

  1. 切入点(Pointcut)
    在哪些类,哪些方法上切入(where)也就是核心业务类。在这些被连接的方法中,可以选择那些方法被切入,被切入的方法就是切入点,例如:在StduentServiceImpl里,若想增强 exam(),而paySchoolFee()不被增强,则exam()就是切入点
  2. 通知(Advice)
    具体增强的方法 定义了增强代码切入到目标代码的时间点。切入点定义切入的位置和时间
  3. 切面(Aspect)
    切面 = 切入点 + 通知,通俗点就是:辅助性业务,对核心业务做增强的类
  4. 织入(Weaving)
    把切面加入到对象,并创建出代理对象的过程。(由 Spring 来完成)
  5. 连接点(JointPoint)
    可以被切面切入的方法,核心业务类中的方法都可以作为切入点

AOP例子

业务需求

完成一个租房的业务,具体如下图所示:
在这里插入图片描述

环境搭建

导入依赖


或者导入对应的jar文件
在这里插入图片描述
核心的包还有一个AspectAliien联盟的包,一定要导入,不然会有问题。

1️⃣编写一个切入类

public interface SecondaryLoaderService {
	public void LoaderBefore();
	public void LoaderAfter();
	public void LoaderAround(ProceedingJoinPoint joinPoint);
}

2️⃣选择连接点

接口类

public interface LoaderService {
	
	// 1、签租房合同
	// 2、交付租房租金
	void RentRoom();
}

实现类

public class LoaderServiceImpl implements LoaderService{
	@Override
	public void RentRoom() {
		System.out.println("step3、签合同..");
		System.out.println("step4、交租金..");
	}
}

3️⃣定义切入点

execution(* com.wei08.service.impl.LoaderServiceImpl.*(..))

这个表达式是利用正则表达式来拦截在哪一个或一些类的方法进行拦截

  1. 具体的方法拦截
execution(* com.wei08.service.impl.LoaderServiceImpl.RentRoom())
  1. 模糊匹配拦截部分方法
execution(* com.wei08.service.impl.LoaderServiceImpl.*(..))
  • com.wei08.service.impl.LoaderServiceImpl为某一个类的全限定类名
  • .*表示该类下的所有方法
  • *.(…)表示该类下的所有方法参数不限的方法

4️⃣配置AOP

方式一:注解配置🔥

代码部分中在方法上面的注解看名字也能猜出个大概,下面来列举一下 Spring 中的 AspectJ 注解:

注解说明
@Before前置通知,在连接点方法前调用
@After后置通知,在连接点方法后调用
@Around环绕通知,它将覆盖原有方法,但是允许你通过反射调用原有方法
@AfterReturning返回通知,在连接点方法执行并正常返回后调用,要求连接点方法在执行过程中没有发生异常
@AfterThrowing异常通知,当连接点方法异常时调用

有了上表,我们就知道 before() 方法是连接点方法调用前调用的方法,而 after() 方法则相反,这些注解中间使用了定义切点的正则式,也就是告诉 Spring AOP 需要拦截什么对象的什么方法。

代码示例:

applicationContext.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: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-4.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">

	<context:component-scan base-package="com.wei07.aop.aop03"></context:component-scan>
	<aop:aspectj-autoproxy/>
</beans>
  • context:component-scan 这个标签是扫描指定包下的Bean,将其交由Spring管理
  • aop:aspectj-autoproxy是使用自动代理,即支持注解式的AOP

核心业务类

@Component("loadService")
public class LoaderServiceImpl implements LoaderService{

	@Override
	public void RentRoom() {
		System.out.println("step3、签合同..");
		System.out.println("step4、交租金..");
	}
}

增强的业务类

package com.wei07.aop.aop03.service.impl;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import com.wei07.aop.aop03.service.SecondaryLoaderService;

@Component
@Aspect
public class SecondaryLoaderServiceImpl implements SecondaryLoaderService{

	// 前置通知
	@Before("execution(* com.wei07.aop.aop03.service.impl.LoaderServiceImpl.*(..))")
	@Override
	public void LoaderBefore() {
		/*
		 *交完房租之前:
			1、看房子
			2、谈价格
		*/
		System.out.println("step1、看房子...");
		System.out.println("step2、谈价格...");
		
	}

	// 后置通知
	@After("execution(* com.wei07.aop.aop03.service.impl.LoaderServiceImpl.*(..))")
	@Override
	public void LoaderAfter() {
		/*
		 * 交完房租后: 
		 * 	1、交钥匙
		 */
		System.out.println("step5、交钥匙...");
	}

	// 环绕通知
	@Around(value = "execution(* com.wei07.aop.aop03.service.impl.LoaderServiceImpl.RentRoom())")
	@Override
	public void LoaderAround(ProceedingJoinPoint joinPoint) {
        System.out.println("带租客看房");
        System.out.println("谈价格");

        try {
            joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        System.out.println("交钥匙");
    }

	// 返回通知
	@AfterReturning(value = "execution(* com.wei07.aop.aop03.service.impl.LoaderServiceImpl.cacluate(..))",returning = "result")
	@Override
	public void LoaderReturn(Object result) {
		System.out.println("返回结果的数据类型==>"+result.getClass().getSimpleName());
		if (result!=null) {
			System.out.println("后置返回通知==>"+result);			
		}
	}

	@AfterThrowing(value = "execution(* com.wei07.aop.aop03.service.impl.LoaderServiceImpl.cacluate(..))",throwing = "ex")
	@Override
	public void LoaderExecption(JoinPoint joinPoint, Exception ex) {
		String name = joinPoint.getSignature().getName();
		System.out.println(name+"is execption is"+ex);
	}
}

方式二:xml配置

注解是很强大的东西,但基于 XML 的开发我们仍然需要了解,我们先来了解一下 AOP 中可以配置的元素:

AOP 配置元素用途备注
aop:advisor定义 AOP 的通知其 一种很古老的方式很少使用
aop:aspect定义一个切面🔥
aop:before定义前置通知🔥
aop:after定义后置通知🔥
aop:around定义环绕通知🔥
aop:after-throwing定义异常通知🔥
aop:after-returning定义返回通知🔥
aop:config顶层的 AOP 配置元素 AOP 的配置是以它为开始的
aop:declare-parents给通知引入新的额外接口,增强功能
aop:pointcut定义切点🔥

有了之前通过注解来编写的经验,并且有了上面的表,我们将上面的例子改写成 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"
	xmlns:context="http://www.springframework.org/schema/context"
	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-4.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">

	
	<bean id="loadService" class="com.wei08.service.impl.LoaderServiceImpl"/>
	<!-- 将切面类注入到Spring容器 -->
	<bean id="secondaryService" class="com.wei08.service.impl.SecondaryLoaderServiceImpl"/>
	
	<aop:config>
		<!-- 定义切入点 和核心业务类中具体的方法 -->
		<aop:pointcut expression="execution(* com.wei08.service.impl.*.*(..))" id="point"/>
		<!-- 将切面引入到aop中 -->
		<aop:aspect ref="secondaryService">
			<!-- 定义切入点方法 -->
			<aop:before method="LoaderBefore" pointcut-ref="point"/>
			<aop:after method="LoaderAfter" pointcut-ref="point"/> 
			<aop:around method="LoaderAround" pointcut-ref="point"/>
		</aop:aspect>
	</aop:config>
</beans>

5️⃣测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:com/wei08/applicationContext.xml")
public class MyTest {

	@Autowired
	@Qualifier("loadService")
	private LoaderService service;
	
	@Test
	public void test() {
		service.RentRoom();
	}
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@WAT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值