【Spring】基于xml(配置文件)实现aop

一、基于xml方式实现aop

1.相关概念

  • Target(目标对象):代理的目标对象
  • Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类
  • Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在Spring中,这些点指的是方法,因为spring中只支持方法类型的连接点

    可以被增强的点

  • Pointcut(切入点):所谓切入点就是指我们要对哪些Joinpoint进行拦截的定义

    要增强的点

  • Advice(通知/增强):所谓通知是指拦截到Joinpoint之后要做的事情就是通知
  • Aspect(切面):是切入点和通知的结合
  • Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程。spring采用动态代理织入,而AspectJ采用编译期织入和类装载期织入

2.代码

  1. pom.xml导入相关坐标
       <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.4</version>
        </dependency>
  1. applicationContext.xml中引入aop命名空间(类似加载properties文件时要引入context命名空间)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       <!--引入aop命名空间-->
       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
        <!--引入aop命名空间-->
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--目标对象-->
    <bean id="target" class="com.it.aop.Target"></bean>

    <!--切面对象-->
    <bean id="myAspect" class="com.it.aop.MyAspect"></bean>

    <!--配置织入:告诉spring框架 哪些方法(切点)需要进行哪些增强(前置、后置......)-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
            <!--切面:切点+通知-->
            <aop:before method="before" pointcut="execution(public void com.it.aop.Target.save())"></aop:before>
        </aop:aspect>
    </aop:config>

</beans>
  1. AopTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
    @Autowired
    private TargetInterface target;

    @Test
    public void test1(){
        target.save();
    }
}

  1. MyAspect
public class MyAspect {

    public void before(){
        System.out.println("前置增强.........");
    }

二、XML配置AOP详解

1.切点表达式的写法

表达式语法:

execution([修饰符]返回值类型 包名.类名.方法名(参数))

  • 访问修饰符可以省略
  • 返回值类型、包名、类名、方法名可以使用星号*代表任意
  • 包名与类名之间一个点.代表当前包下的类,两个…表示当前包及其子包下的类
  • 参数列表可以使用两个点…表示任意个数,任意类型的参数列表

例如:

execution(public void com.it.aop.Target.save())
execution(void com.it.aop.Target.save())
execution(void com.it.aop.Target.*(..))
execution(* com.it.aop.*.*(..))
execution(* com.it.aop..*.*(..))
execution(* *..*.*(..))

2、通知的类型

名称标签说明
前置通知aop:before指定增强的方法在切入点方法之前执行
后置通知aop:after-returning指定增强的方法在切入点方法之后执行
环绕通知aop:around指定增强的方法在切入点方法之前和之后都执行
异常抛出通知aop:throwing指定增强的方法在出现异常的时候执行
最终通知aop:after无论增强方法执行是否有异常都会执行

环绕增强示例:

public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕前增强.........");
        //目标方法 Target.method()
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("环绕后增强");

    }

3、切点表达式的抽取

<aop:pointcut id="myPointcut" expression="execution(* com.it.aop.Target.*(..))"></aop:pointcut>

 <aop:before method="before" pointcut-ref="myPointcut"></aop:before>
            <aop:after-returning method="afterReturning" pointcut-ref="myPointcut"></aop:after-returning>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值