• 基于xml的AOP开发


    目录

    基于xml的AOP开发

     XML配置AOP详解

    切点表达式的写法

    通知/(增强)的类型

     前后置增强

     环绕增强

      异常抛出增强

     最终增强

     切点表达式的抽取

     知识要点


    基于xml的AOP开发

    ①导入AOP相关坐标

    ②创建目标接口和目标类(内部有切点)

    ③创建切面类(内部有增强方法)

    ④将目标类和切面类的对象创建权交给spring

    ⑤在applicationContext.xml中配置织入关系

    ⑥测试代码

     1、导入坐标

    1. <dependency>
    2. <groupId>org.aspectjgroupId>
    3. <artifactId>aspectjweaverartifactId>
    4. <version>1.8.4version>
    5. dependency>

    spring-context本身有aop的实现,但是aspectj更好,aspectj本身就是一个小框架。

    MyAspect切面类(有增强方法)

    1. package aop;
    2. public class MyAspect {
    3. public void before(){
    4. System.out.println("前置增强....");
    5. }
    6. }

     applicationContext.xml下

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:aop="http://www.springframework.org/schema/aop"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    5. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    6. ">
    7. <bean id="target" class="aop.Target">bean>
    8. <bean id="myAspect" class="aop.MyAspect">bean>
    9. <aop:config>
    10. <aop:aspect ref="myAspect">
    11. <aop:before method="before" pointcut="execution(public void aop.Target.save())"/>
    12. aop:aspect>
    13. aop:config>
    14. beans>

    AopTest测试类下

    1. package aop;
    2. import org.junit.Test;
    3. import org.junit.runner.RunWith;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.test.context.ContextConfiguration;
    6. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    7. @RunWith(SpringJUnit4ClassRunner.class)
    8. @ContextConfiguration("classpath:applicationContext.xml")
    9. public class AopTest {
    10. @Autowired
    11. private TargetInterface target;
    12. @Test
    13. public void test1(){
    14. target.save();
    15. }
    16. }

    运行之后报错了

     仔细一看需要Junit4.12以上的版本,改完之后,

     XML配置AOP详解

    切点表达式的写法

    表达式语法:

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

    例如

    通知/(增强)的类型

    通知的配置语法:

    "切面类中的方法名" pointcut="切点表达式"/>

     前后置增强

    1. <aop:config>
    2. <aop:aspect ref="myAspect">
    3. <aop:before method="before" pointcut="execution(public void aop.Target.save())"/>
    4. <aop:after-returning method="afterReturn" pointcut="execution(public void aop.Target.save())"/>
    5. aop:aspect>
    6. aop:config>

    写下前后置增强

    1. public class MyAspect {
    2. public void before(){
    3. System.out.println("前置增强....");
    4. }
    5. public void afterReturn(){
    6. System.out.println("后置增强....");
    7. }
    8. }

    运行之后

     环绕增强

    切面类中的方法 

    1. import org.aspectj.lang.ProceedingJoinPoint;
    2. public class MyAspect
    3. //ProceedingJoinPoint:正在执行的连接点===切点
    4. public Object around(ProceedingJoinPoint point) throws Throwable {
    5. System.out.println("环绕前增强...");
    6. Object proceed=point.proceed();//切点方法
    7. System.out.println("环绕后增强...");
    8. return proceed;
    9. }
    10. }

    applicationContext.xml下

    1. <aop:config>
    2. <aop:aspect ref="myAspect">
    3. <aop:around method="around" pointcut="execution(public void aop.Target.save())"/>
    4. aop:aspect>
    5. aop:config>
    6. beans>

     运行结果

      异常抛出增强

    切面类下

    1. public void afterThrows(){
    2. System.out.println("异常抛出增强");
    3. }

     目标类中需要手动加一个异常

    1. public class Target implements TargetInterface {
    2. @Override
    3. public void save() {
    4. System.out.println("save running。。。");
    5. int i=1/0;
    6. }
    7. }

     applicationContext.xml中

    1. <aop:config>
    2. <aop:aspect ref="myAspect">
    3. <aop:after-throwing method="afterThrows" pointcut="execution(public void aop.Target.save())"/>
    4. aop:aspect>
    5. aop:config>

     最终增强

    最终增强即为无论抛不抛出异常,这个方法都会被执行 

    1. public void after(){
    2. System.out.println("最终增强...");
    3. }

    1. <aop:config>
    2. <aop:aspect ref="myAspect">
    3. <aop:after-throwing method="afterThrows" pointcut="execution(public void aop.Target.save())"/>
    4. <aop:after method="after" pointcut="execution(public void aop.Target.save())"/>
    5. aop:aspect>
    6. aop:config>

     运行结果

     切点表达式的抽取

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

      <aop:pointcut id="myPointcut" expression="execution(public void aop.Target.save())"/>

    applicationContext.xml中

    1. <aop:config>
    2. <aop:pointcut id="myPointcut" expression="execution(public void aop.Target.save())"/>
    3. <aop:aspect ref="myAspect">
    4. <aop:around method="around" pointcut-ref="myPointcut"/>
    5. <aop:after-returning method="after" pointcut-ref="myPointcut"/>
    6. aop:aspect>
    7. aop:config>

    运行结果

     知识要点

    aop织入

     通知的类型:前置通知、后置通知、环绕通知、异常抛出通知、最终通知

    点表达式的写法

  • 相关阅读:
    分享4个MSVCP100.dll丢失的解决方法
    【无标题】
    R语言使用data.table包的fread函数读取(加载)csv数据为data.table格式、使用anyNA函数判断data.table中是否存在缺失值
    Java-day15(Java常用类)
    使用react-grid-layout和echarts-for-react实现一个支持拖拽的自定义响应式dashboard页面
    什么是HTML?
    使用Python编写一个多线程的12306抢票程序
    二叉树题目:二叉树剪枝
    在游戏博弈中才是博弈游戏的最佳实践
    增速4755%!撬动海外美妆市场,赛盈分销洞察美国市场年终布局方向!
  • 原文地址:https://blog.csdn.net/weixin_60719453/article/details/126283558