在XML中配置AOP

在Spring的aop命名空间中,提供了多个元素用来在XML中声明切面:
这里写图片描述

在XML文件中配置aop命名空间:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop.xsd">

</beans>

一、声明前置和后置通知

1、定义一个普通的POJO作为通知(增强advice)

public class Audience {
    public void silenceCellphone(){
        System.out.println("please silence cell phone");
    }

    public void takeSeates(){
        System.out.println("please take Seate");
    }

    public void applause(){
        System.out.println("CLAP  CLAP  CLAP");
    }

    public void demandRefund(){
        System.out.println("Refund  refund");
    }
}

2、在XML中声明前置通知和后置通知

<!--将通知定义为一个bean,也可以不用明确定义,采用注解方式-->
<bean id="audience" class="org.aop.Audience"/>

    <aop:config>
        <!--定义一个切面-->
        <aop:aspect ref="audience">
            <aop:before pointcut="execution(* org.aop.Perform.play())" method="silenceCellphone"/>
            <aop:before pointcut="execution(* org.aop.Perform.play())" method="takeSeates"/>
            <aop:after method="applause" pointcut="execution(* org.aop.Perform.play())"/>
            <aop:after-throwing method="demandRefund" pointcut="execution(* org.aop.Perform.play())"/>
        </aop:aspect>
    </aop:config>

在<\aop:config>元素内,可以声明一个或多个通知器、切面或切点。

它拥有一个proxy-target-class属性,当设置为true时,表示其中声明的切面均使用CGLib动态代理技术,当设置为false时,使用Java动态代理技术。

对于上面重复的切点pointcut属性值,可以进行优化:

<aop:config>
        <!--定义一个切面-->
        <aop:aspect ref="audience">
          <!--定义一个共同的切点-->
            <aop:pointcut id="point" expression="execution(* org.aop.Perform.play())"/>
            <!--引用切点-->
            <aop:before pointcut-ref="point" method="silenceCellphone"/>
            <aop:before pointcut-ref="point" method="takeSeates"/>
            <aop:after method="applause" pointcut-ref="point"/>
            <aop:after-throwing method="demandRefund" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

如果想让定义的切点可以在多个切面使用,可以把<\aop:pointcut>元素放在<\aop:config>元素的范围内。

3、目标对象

@Component
public class Perform implements Performence {
    public void play(){
        System.out.println("play what");
    }
}

4、测试一下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-aop.xml"})
public class CDPConfigTest {

    @Autowired
    private Performence perform;

    @Test
    public void doP(){
        perform.play();
    }
}
//结果:
please silence cell phone
please take Seate
play what
CLAP  CLAP  CLAP

二、声明环绕通知
1、定义一个新的环绕通知

@Component
public class AroundAudience {
    public void watchPerformance(ProceedingJoinPoint jp){
        try{
            System.out.println("Silencing cell phones");
            System.out.println("Taking seats");
            //执行被通知的方法
            jp.proceed();
            System.out.println("CLAP CLAP CALP");
        }catch (Throwable e){
            System.out.println("Demanding a refund");
        }
    }
}

2、在XML中声明环绕通知

 <aop:config>
        <aop:aspect ref="aroundAudience">
            <aop:pointcut id="aroundPoint" expression="execution(* org.aop.Perform.aroundPlay())"/>
            <aop:around method="watchPerformance" pointcut-ref="aroundPoint"/>
        </aop:aspect>
    </aop:config>

3、测试一下

@Autowired
    private Performence perform;
@Test
    public void doP(){
        //类型如果是接口,那方法一定要在接口中有定义
        perform.aroundPlay();
    }

三、为通知传递参数
传递到目标对象中的某个方法的参数,也可以传递到通知中。
通知中与目标对象方法中的参数名保持一致。并且,在通知中对参数的修改不会改变目标对象的方法参数
1、一个通知

public void countTrack(int Num){
        Num++;
        System.out.println("通知中  trackNum="+Num);
    }

2、XML中配置

<aop:config>
        <aop:aspect ref="audience">
            <aop:pointcut id="trackplay" expression="execution(* org.aop.Perform.palyTrace(int)) and args(Num)"/>
            <aop:before method="countTrack" pointcut-ref="trackplay"/>
        </aop:aspect>
    </aop:config>

3、目标对象

public void palyTrace(int Num){
        System.out.println("目标对象  trackNum="+Num);
    }

4、测试一下

@Test
    public void doP(){
        //类型如果是接口,那方法一定要在接口中有定义
        perform.palyTrace(8);
    }
//结果
通知中  trackNum=9
目标对象  trackNum=8

四、通过切面引入新的功能
1、新功能所在的接口(要引入的)和实现类

//新功能接口
public interface Encorable {
    void performEncore();
}

//新更能实现类
@Component
public class defaultEncore implements Encorable {
    public void performEncore(){
        System.out.print("这是新引入的功能");
    }
}

2、XML中的配置

  <aop:config>
        <aop:aspect>
            <aop:declare-parents types-matching="org.aop.Performence+"
                                 implement-interface="org.aop.Encorable"
                                 default-impl="org.aop.defaultEncore"/>
        </aop:aspect>
    </aop:config>
types-matching:Performence接口所实现的子类,也就是对应的目标切点类。
implement-interface:新加入的接口类。
default-impl:新加入的接口的默认实现类。

也可以修改为:

<aop:config>
        <aop:aspect>
            <aop:declare-parents types-matching="org.aop.Performence+"
                                 implement-interface="org.aop.Encorable"
                                 delegate-ref="defaultEncore"/>
        </aop:aspect>
    </aop:config>

    <bean id="defaultEncore" class="org.aop.defaultEncore"/>

4、测试一下

@Autowired
    private Performence perform;

    @Test
    public void doP(){
        //就像perform实现了Encorable接口一样
        Encorable perEncore=(Encorable)perform;
        perEncore.performEncore();
    }

Advisor的配置
<\aop:advisor advice-ref=”” pointcut-ref=”“/>
它是切点和通知的复合体,仅包含一个切点和一个通知。它的切点表示方法和<\aop:aspect>相同,增强定义方式则要实现相关的接口(MethodBeforeAdvice、AfterReturningAdvice/ThrowsAdvice)。

<\aop:config>元素中的<\aop:pointcut>、<\aop:advisor>、<\aop:aspect>三者的配置顺序:
首先是<\aop:pointcut>,其次是<\aop:advisor>,最后是<\aop:aspect>。
而在<\aop:aspect>中定义的<\aop:pointcut>则没有先后顺序要求,可以在任何位置定义。

这里写图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring,基于XML配置AOP的步骤如下: 1. 添加依赖并准备环境。 2. 创建通知类和接口,并实现方法。 3. 配置spring.xml文件。 4. 执行测试。 具体步骤如下: 1. 创建通知类和接口,并实现方法。 2. 在spring.xml文件配置AOP。 3. 使用aop:config标签开始AOP配置。 4. 使用aop:aspect标签配置切面,其id属性是给切面提供一个唯一标识,ref属性是指定通知类bean的Id。 5. 在aop:aspect标签内部使用对应标签来配置通知的类型,例如使用aop:before标签配置前置通知。 6. 在aop:before标签,使用method属性指定Logger类哪个方法是前置通知,使用pointcut属性指定切入点表达式,该表达式指定了对业务层哪些方法进行增强。 7. 切入点表达式的写法可以使用关键字execution和具体的表达式来指定方法的访问修饰符、返回值、包名、类名和方法名等。 8. 在通知类添加环绕通知方法,使用ProceedingJoinPoint接口作为环绕通知的方法参数,该接口提供了proceed()方法,用于明确调用切入点方法。 9. 在环绕通知方法,可以通过proceedingJoinPoint.proceed(args)调用切入点方法,并在方法执行前后进行相应的操作。 10. 最后,执行测试来验证AOP配置的效果。 以上是基于XML配置AOP的步骤。通过配置AOP,可以在指定的切入点方法执行前后,或者在方法执行过程进行增强操作。 #### 引用[.reference_title] - *1* *3* [Spring之基于XML配置AOP](https://blog.csdn.net/qq_38628046/article/details/108065715)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Spring基于XMLAOP配置](https://blog.csdn.net/weixin_45430616/article/details/104101117)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值