spring中AOP的xml与注解开发
2020-06-19 15:04
387 查看
spring中AOP的xml与注解开发
什么是AOP
AOP是Aspect Oriented Programming的缩写,意思就是面向切面的编程,通过预编译的方式和运行期间动态代理实现程序功能的统一维护的技术,它是OOP(面向对象的编程)的延续,是Spring框架中的重要内容,是函数式编程的一种衍生范型,利用AOP对业务逻辑的各个部分进行隔离,从而使得业务逻辑各个部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
AOP的作用与优势
AOP的作用:在程序运行期间,在不修改源码的情况下,对其功能进行增强。
AOP的优势:减少重复代码,提高开发效率,更便于维护。
AOP的底层
AOP的底层实现:AOP的底层就是Spring通过动态代理技术实现的,在运行期间,Spring通过动态代理技术动态的生成代理对象,代理对象方法执行时进行功能增强,再去调用目标对象的方法,从而完成功能的增强。
AOP的动态代理技术:比较常用的动态代理技术有JDK代理:基于接口的动态代理技术;cglib代理:基于父类的动态代理技术;
AOP相关术语
- Target:目标对象;
- Proxy:一个类被AOP织入增强后产生的一个结果代理类;
- Joinpoint:连接点,在Spring中指的是方法,因为Spring只支持方法类型的连接点
- PoinCut:切入点,就是我们要对哪些JoinPoint进行链接的定义;
- Advice:通知/增强,就是指Joinpoint之后所有做的事情就是通知;
- Aspect:切面,就是切入点和通知的结合;
- Weaving:织入,就是把增强应用到目标对象来创建新的代理对象的过程.Spring采用的是动态代理织入,而Aspect采用编译期织入和类装载期织入;
AOP的常用配置及其注解介绍
切点表达式语法
execution([修饰符] 返回值 包名.类名.方法名(参数))
- 修饰符可以省略不写
- 返回值类型、包名、类名、方法名可以使用星号*代替(通配符*)
- 包名和类名之间一个点.代表当前包下的类,两个点…代表了当前包及子包下的类
- 参数列表可以I使用两个点…表示任意个数.任意类型的参数列表
举个例子
execution(public void com.hhx.aop.Target.method()) execution(void com.hhx.aop.Target.*(..)) execution(* com.hhx.aop.*.*(..)) execution(* com.hhx.aop..*.*(..)) execution(* *..*.*(..))
通知的类型
<aop:通知类型 method=“切面类中方法名” pointcut=“切点表达式"></aop:通知类型>
名称 | 标签 | 说明 |
---|---|---|
前置通知 | <aop:befor> | 用于配置前通知。指定增强的方法在切点方法之前执行 |
后置通知 | <aop:after-returning> | 用于配置后置通知。指定增强的方法在切入点之后执行 |
环绕通知 | <aop:around> | 用于配置环绕通知。指定增强的方法在切入点方法之前和之后都执行 |
抛出异常通知 | <aop:throwing> | 用于配置异常抛出通知。指定增强的方法在抛出异常时执行 |
最终通知 | <aop:after> | 用于配置最终通知。无论增强方式执行是否出现异常 |
AOP相关坐标
<!--导入spring的context坐标,context依赖aop--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.5.RELEASE</version> </dependency> <!-- aspectj的织入 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency>
AOP的xml开发
- 创建接口和目标类
//接口 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("前置代码增强...."); } //后置增强 public void afterretuening(){ System.out.println("后置代码增强...."); } //最终增强 public void after(){ System.out.println("最终代码增强...."); } //抛出异常时的增强 public void throwing(){ System.out.println("增强方法出现异常通知..."); } //环绕增强 /* 1)JoinPoint java.lang.Object[] getArgs():获取连接点方法运行时的入参列表; Signature getSignature() :获取连接点的方法签名对象; java.lang.Object getTarget() :获取连接点所在的目标对象; java.lang.Object getThis() :获取代理对象本身; 2)ProceedingJoinPoint ProceedingJoinPoint继承JoinPoint子接口,它新增了两个用于执行连接点方法的方法: java.lang.Object proceed() throws java.lang.Throwable:通过反射执行目标对象的连接点处的方法; java.lang.Object proceed(java.lang.Object[] args) throws java.lang.Throwable:通过反射执行目标对象连接点处的方法,不过使用新的入参替换原来的入参。 */ public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("环绕前代码增强...."); //执行原有方法,括号内可以有参数,也可以没有参数; //如果没有参数,就相当于传递的是原来的参数,如果有参数,传递的线参数会替代原来的参数 Object proceed = pjp.proceed(); System.out.println("环绕后代码增强...."); return proceed; } }
- 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--先将myAspect和targetInterface交给spring容器管理--> <bean id="myAspect" class="com.hhx.aspect.MyAspect"/> <bean id="targetInterface" class="com.hhx.dao.impl.TargetInterfaceImpl"/> <!--编辑aop配置--> <aop:config> <!--设置切入点地址来供后边的代码关联,方便维护,切点表达式表示所有包下的所有类的所有方法 不建议这么写--> <aop:pointcut id="aspectCut" expression="execution(* *..*.*(..))"/> <!--设置切入面 关联切入面--> <aop:aspect ref="myAspect"> <!--在方法执行前增强代码--> <aop:before method="before" pointcut-ref="aspectCut"/> <!--在方法执行后增强代码,如果抛出异常则不增强代码--> <aop:after-returning method="afterretuening" pointcut-ref="aspectCut"/> <!--最终增强代码,即使抛出异常,最后也会增强代码--> <aop:after method="after" pointcut-ref="aspectCut"/> <!--环绕增强代码--> <aop:around method="around" arg-names="pjp" pointcut-ref="aspectCut"/> <!--抛出异常是增强代码--> <aop:after-throwing method="throwing" pointcut-ref="aspectCut"/> </aop:aspect> </aop:config> </beans>
aop的注解开发
- 将dao的实现类交给Spring容器管理
//将接口实现类交给Spring容器 @Repository("targetInterface") public class TargetInterfaceImpl implements TargetInterface { @Override public void save() { System.out.println("save running...."); } }
- 将增强类MyAspect的注解配置
//将此类定义成切面类 @Aspect //扫描包 @ComponentScan("com.hhx") @EnableAspectJAutoProxy public class MyAspect { //前置增强 @Before("execution(* com.hhx..*(..))") public void before(){ System.out.println("前置代码增强...."); } //后置增强 @AfterReturning("execution(* com.hhx..*(..))") public void afterretuening(){ System.out.println("后置代码增强...."); } //最终增强 @After("execution(* com.hhx..*(..))") public void after(){ System.out.println("最终代码增强...."); } //抛出异常增强 @AfterThrowing("execution(* com.hhx..*(..))") public void throwing(){ System.out.println("增强方法出现异常通知..."); } //环绕增强 @Around("execution(* com.hhx..*(..))") public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("环绕前代码增强...."); Object proceed = pjp.proceed(); System.out.println("环绕后代码增强...."); //这里返回的是原始方法的返回值 如果不返回则测试方法无返回值 return proceed; } }
测试代码
@RunWith(SpringJUnit4ClassRunner.class) //读取配置类 如果有多个可以逗号隔开 @ContextConfiguration(classes = {MyAspect.class}) public class TargetInterfaceImplTest { //自动按类型注入 @Autowired private TargetInterface t; @Test public void save() { t.save(); } }
执行结果
相关文章推荐
- Spring AOP开发 XML配置和注解配置
- Spring AOP 注解和xml实现 --转载
- Spring AOP编程-aspectJ注解开发(@Pointcut声明切点)
- 【从零开始学Spring笔记】AOP的注解开发
- Spring深入学习(10)AOP(面向切面编程)中的通知xml配置以及注解配置
- 走进Spring --- AOP-XML配置开发
- Spring学习-21:Spring的AOP:基于AspectJ的注解开发
- Spring学习笔记四(AOP中的通知参数和注解开发)
- spring 学习笔记 使用pojo+xml的方式开发aop
- [置顶] spring切点切面aop——xml非注解
- spring aop相关概念和xml与注解实现方式
- Spring中基于配置XML与Annotation注解配置AOP
- [Spring框架]Spring开发实例: XML+注解.
- Spring学习笔记四(AOP中的通知参数和注解开发)
- Spring基础入门五:基于XML和注解的Spring的AOP使用
- 【从零开始学Spring笔记】AOP的XML开发
- spring切点切面aop——xml非注解
- Spring 动态代理分析&AOP的XML的方式&注解方式
- 六、Spring基于纯注解的开发和基于注解加xml配置你怎么选
- Spring AOP 注解和xml实现 --转载