XML方式实现AOP

本文阅读 4 分钟
首页 代码,Java 正文

img

✨博客主页:👉 不会压弯的小飞侠 ✨欢迎关注:👉点赞👍收藏⭐留言✒ ✨系列专栏:👉 spring专栏 ✨如果觉得博主的文章还不错的话,请三连支持一下博主。 ✨欢迎大佬指正,一起学习!一起加油!

  • 导入AOP相关坐标
  • 创建目标接口和目标类(内部有切点)
  • 创建切面类(内部有增强方法)
  • 将目标类和切面类的对象创建权交给spring
  • 在applicationContext.xml中配置织入关系
  • 测试代码

1.导入AOP相关坐标

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.9.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

2.创建目标接口

package com.study.proxy.aop;

public interface TargetInterface { 
    void save();
}

3.创建目标类

package com.study.proxy.aop;
public class Target implements TargetInterface { 
    public void save() { 
        System.out.println("TargetImpl....");
    }
}

4.创建切面类

package com.study.proxy.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect { 
    public void before(){ 
        System.out.println("前置增强.....");
    }
    public void afterReturning(){ 
        System.out.println("后置增强.....");
    }
    public Object around(ProceedingJoinPoint pjp) throws Throwable { 
        System.out.println("环绕前增强.....");
        //切点方法
        Object proceed = pjp.proceed();
        System.out.println("环绕后增强.....");
        return proceed;
    }
}

5.配置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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--目标对象-->
    <bean id="target" class="com.study.proxy.aop.Target"></bean>
    <!--切面对象-->
    <bean id="myAspect" class="com.study.proxy.aop.MyAspect"></bean>
    <!--配置织入: 告诉Spring框架。哪些方法(切点)需要进行哪些增强(前置,后置)-->
    <aop:config>
       <!-- 声明切面-->
        <aop:aspect ref="myAspect">
           <!-- 切面: 切点+通知-->
            <!--<aop:before method="before" pointcut="execution(public void com.study.proxy.aop.Target.save())"></aop:before>-->
           <!-- <aop:before method="before" pointcut="execution(* com.study.proxy.aop.*.*(..))"></aop:before>
            <aop:after-returning method="afterReturning" pointcut="execution(* com.study.proxy.aop.*.*(..))"></aop:after-returning>
            <aop:around method="around" pointcut="execution(* com.study.proxy.aop.*.*(..))"></aop:around>-->

           <!-- 抽取切点表达式-->
            <aop:pointcut id="all" expression="execution(public void com.study.proxy.aop.Target.save())"/>
            <aop:before method="before" pointcut-ref="all"></aop:before>
            <aop:after method="afterReturning" pointcut-ref="all"></aop:after>
            <aop:around method="around" pointcut-ref="all"></aop:around>
        </aop:aspect>
    </aop:config>

</beans>

6.测试类

package com.study.proxy.aop;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest { 
    @Autowired
    private TargetInterface targetInterface;
    @Test
    public void test(){ 
        targetInterface.save();

    }

}
/* 前置增强..... 环绕前增强..... TargetImpl.... 环绕后增强..... 后置增强..... */

1.切点表达式写法

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

代码:

<aop:before method="before" pointcut="execution(public void com.study.proxy.aop.Target.save())"></aop:before>
<aop:before method="before" pointcut="execution(* com.study.proxy.aop.*.*(..))"></aop:before>

2.通知类型

  • 通知的配置语法: aop:通知类型method=“切面类中方法名”pointcut=“切点表达式"</aop:通知类型>
  • 前置通知
    - aop:before - 用于配置前置通知。指定增强的方法在切入点方法之前执行
  • 后置通知
    - aop:after-returning - 用于配置后置通知。指定增强的方法在切入点方法之后执行
  • 环绕通知
    - aop:around - 用于配置环绕通知。指定增强的方法在切入点方法之前和之后都执行
  • 异常抛出通知
    - aop:throwing - 用于配置异常抛出通知。指定增强的方法在出现异常时执行
  • 最终通知
    - aop:after - 用于配置最终通知。无论增强方式执行是否有异常都会执行

代码:

<aop:before method="before" pointcut="execution(* com.study.proxy.aop.*.*(..))"></aop:before>
<aop:after-returning method="afterReturning" pointcut="execution(* com.study.proxy.aop.*.*(..))"></aop:after-returning>
<aop:around method="around" pointcut="execution(* com.study.proxy.aop.*.*(..))"></aop:around>

3.切点表达式的抽取

  • 当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用pointcut-ref属性代替pointcut属性来引用抽取后的切点表达式。

代码:

<!-- 抽取切点表达式-->
            <aop:pointcut id="all" expression="execution(public void com.study.proxy.aop.Target.save())"/>
            <aop:before method="before" pointcut-ref="all"></aop:before>
            <aop:after method="afterReturning" pointcut-ref="all"></aop:after>
            <aop:around method="around" pointcut-ref="all"></aop:around>

4.知识要点

  • aop织入的配置
<aop:config>
<aop:aspect ref="切面类">
<aop:before method="通知方法名称”pointcut="切点表达式"></aop:before></aop:aspect>
</ aop:config>
  • 通知的类型:前置通知、后置通知、环绕通知、异常抛出通知、最终通知
  • 切点表达式的写法::
    - execution([修饰符]返回值类型包名.类名.方法名(参数))
本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/qq_43514330/article/details/125461564
-- 展开阅读全文 --
安全面试之XSS(跨站脚本攻击)
« 上一篇 07-24

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复