Spring AOP:基本概述

文章导航

Spring AOP:基本概述
Spring AOP源码:配置文件解析过程
Spring AOP源码:配置文件解析过程
Spring AOP源码:拦截器责任链处理过程

前言

AOP在我们日常的开发种用得比较多,如最常见的事务。当我们需要对某些包下面的类的方法执行前后增加某些功能时,就可以使用AOP技术来实现,常见的有xml配置的方法、注解的方式(用的比较多)。Spring AOP 底层使用Cglib和JDK动态代理的方式进行实现,在前面的文章种已经讲解了这两种代码方式。

概述

什么是AOP

(1)面向切面编程(方面),利用AOP可以对业务逻辑的各个部分进行隔离,从而是的业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

(2)通俗描述:不通过修改源代码方式,在主干功能里面添加新功能

在这里插入图片描述

AOP常见名词

Aspect(切面): Aspect 声明类似于 Java 中的类声明,在 Aspect 中会包含着一些 Pointcut 以及相应的 Advice。
Joint point(连接点):表示在程序中明确定义的点,典型的包括方法调用,对类成员的访问以及异常处理程序块的执行等等,它自身还可以嵌套其它 joint point。
Pointcut(切点):表示一组 joint point,这些 joint point 或是通过逻辑关系组合起来,或是通过通配、正则表达式等方式集中起来,它定义了相应的 Advice 将要发生的地方。
Advice(增强):Advice 定义了在 Pointcut 里面定义的程序点具体要做的操作,它通过 before、after 和 around 来区别是在每个 joint point 之前、之后还是代替执行的代码。
Target(目标对象):织入 Advice 的目标对象.。
Weaving(织入):将 Aspect 和其他对象连接起来, 并创建 Adviced object 的过程

案例

通过案例的方案让我们更加清晰的了解Spring AOP的使用。

编写切面类

package aop.service;

public class MyAspect {
	
	public void before(){
		System.out.println("before");
	}
	public void after(){
		System.out.println("after");
	}
	public void afterThrowing(){
		System.out.println("afterThrowing");
	}
	public void afterReturn(){
		System.out.println("afterReturn");
	}
	public void around(){
		System.out.println("around");
	}
}

编写目标类

public class MyClass {
	public void myMethod(){
		System.out.println("我是一个普通方法;");
	}
}

编写配置

<?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 class="aop.test.MyClass" id="myClass"></bean>
		<bean class="aop.service.MyAspect" id="myAspect"></bean>
	<aop:config>
		<aop:pointcut id="myPointCut" expression="execution(* aop.test.*.*())"/>
		<aop:aspect  ref="myAspect">
			<aop:before method="before" pointcut-ref="myPointCut"></aop:before>
			<aop:after method="after" pointcut-ref="myPointCut"></aop:after>
			<aop:after-returning method="afterThrowing" pointcut-ref="myPointCut"></aop:after-returning>
			<aop:after-throwing method="afterThrowing" pointcut-ref="myPointCut"></aop:after-throwing>
		</aop:aspect>
	</aop:config>

</beans>

编写测试用例

public class AopTest {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("application-aop.xml");
		MyClass myClass = (MyClass) context.getBean("myClass");
		myClass.myMethod();
	}
}

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

@猪大肠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值