主页 > 人工智能  > 

基于xml配置的AOP

基于xml配置的AOP

目录

xml方式AOP快速入门

xml方式AOP配置详解


xml方式AOP快速入门

xml方式配置AOP的步骤

导入AOP相关坐标 <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency>

    2.准备目标类,准备增强类,并配置给Spring管理

    3.配置切点表达式(哪些方法被增强)

    4.配置织入(切点被哪些通知方法增强,是前置增强还是后置增强)

<aop:config> <!-- 配置切入点 目的是指定哪些方法增强--> <aop:pointcut id="myPointCut1" expression="execution(void com.hsf.service.Impl.UserServiceImpl.show01())"/> <!-- 配置织入 目的是要执行哪些切入点与通知结合--> <aop:aspect ref="advice"> <!-- 配置前置增强 --> <aop:before method="beforeAdvice" pointcut-ref="myPointCut1"></aop:before> </aop:aspect> </aop:config>

 

xml方式AOP配置详解

切点表达式的配置方式

1.

<aop:config> <!-- 配置切入点 目的是指定哪些方法增强--> <aop:pointcut id="myPointCut1" expression="execution(void com.hsf.service.Impl.UserServiceImpl.show01())"/> <!-- 配置织入 目的是要执行哪些切入点与通知结合--> <aop:aspect ref="advice"> <!-- 配置前置增强 --> <aop:before method="beforeAdvice" pointcut-ref="myPointCut1"></aop:before> </aop:aspect> </aop:config>

 2.

<aop:before method="beforeAdvice" pointcut="execution(void com.hsf.service.Impl.UserServiceImpl.show01())"></aop:before>

 切点表达式的配置语法

 

访问修饰符可以省略不写

返回值类型,某一级包名,类名,方法名可以使用*表示所有

包名与类名之间使用单点,表示该包下面的类,使用双点..表示该包及其子包下的类

参数列表可以使用两个点..表示任意参数

通知的类型

 

<aop:aspect ref="advice"> <!-- 配置前置增强 --> <aop:before method="beforeAdvice" pointcut="execution(void com.hsf.service.Impl.UserServiceImpl.show01())"></aop:before> <aop:after-throwing method="error" pointcut-ref="myPointCut1"></aop:after-throwing> <aop:after-returning method="afterAdvice" pointcut="execution(void com.hsf.service.Impl.UserServiceImpl.show01())"></aop:after-returning> <aop:around method="arround" pointcut-ref="myPointCut1"></aop:around> <aop:after method="nessccrsy" pointcut-ref="myPointCut1"></aop:after> </aop:aspect> public Object arround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("环绕前通知"); Object res = proceedingJoinPoint.proceed(); System.out.println("环绕后通知"); return res; }

 

 

public void beforeAdvice(JoinPoint joinPoint){ System.out.println(joinPoint.getTarget()); System.out.println("前置的增强"); }

 

<aop:after-throwing method="error" pointcut-ref="myPointCut1" throwing="e"></aop:after-throwing>

 

AOP的配置的两种方式

使用<advisor>配置切面

使用<aspect>配置切面

Spring定义了一个Advice接口,实现该接口的类都可以作为通知类出现

<aop:advisor advice-ref="myadvice2" pointcut-ref="myPointCut1"></aop:advisor>

 

package com.hsf.advice; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; public class Myadvice2 implements MethodBeforeAdvice, AfterReturningAdvice, MethodInterceptor { //前置增强 @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("before>>>>>"); } //后置增强 @Override public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("after>>>>"); } //环绕通知 @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("环绕前通知方法"); Object res = methodInvocation.getMethod().invoke(methodInvocation.getThis(), methodInvocation.getArguments()); System.out.println("环绕后通知方法"); return res; } }

advisor与aspect的不同点:

语法形式不同:

advisor是通过实现接口来确认通知的类型

aspect是通过配置确认通知的类型,更加灵活

可配置的切面数量不同

一个advisor只能配置一个固定通知和一个切点表达式

一个aspect可以配置多个通知和多个切点表达式任意组合

使用场景不同:

允许随意搭配情况下可以使用ascpect进行配置

如果通知类型单一,切面单一的情况下可以使用advisor进行配置

在通知类型已经固定,不用人为指定通知类型时,可以使用advisor进行配置

标签:

基于xml配置的AOP由讯客互联人工智能栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“基于xml配置的AOP