Spring学习笔记7——xml AOP

博客提到对应Java类的配置方式,可用XML方式实现AOP,且实现情况与Java类配置方式相近,并提及有XML配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对应于java类的配置方式,同样可以使用xml的方式实现AOP。实现差不多,不多说。

package com.glodon.springdemo7;


import org.aspectj.lang.ProceedingJoinPoint;

public class SimpleProfiler {

  public void profilerBefore() throws Throwable {
    System.out.println("profiler before");
  }

  public void profilerAfter() throws Throwable {
    System.out.println("profiler after");
  }

  public void profilerAfterReturning() throws Throwable {
    System.out.println("profiler after returning");
  }

  public void profilerException() throws Throwable {
    System.out.println("profiler exception");
  }

  public Object profilerAround(ProceedingJoinPoint joinPoint) throws Throwable {
    System.out.println("profiler around start");
    Object obj = joinPoint.proceed();
    System.out.println("profiler around end");
    return obj;
  }
}

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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

  <bean id="helloService" class="com.glodon.springdemo7.DefaultHelloService"/>

  <bean id="profiler" class="com.glodon.springdemo7.SimpleProfiler"/>

  <aop:config>
    <aop:aspect ref="profiler">

      <aop:pointcut id="theExecutionOfSomeFooServiceMethod"
                    expression="execution(* com.glodon.springdemo7.DefaultHelloService.hello(..))"/>

      <aop:before pointcut-ref="theExecutionOfSomeFooServiceMethod" method="profilerBefore"/>

      <aop:after-returning pointcut-ref="theExecutionOfSomeFooServiceMethod"
                           method="profilerAfterReturning"/>

      <aop:after pointcut-ref="theExecutionOfSomeFooServiceMethod" method="profilerAfter"/>

      <aop:after-throwing pointcut-ref="theExecutionOfSomeFooServiceMethod"
                          method="profilerException"/>

      <aop:around pointcut-ref="theExecutionOfSomeFooServiceMethod" method="profilerAround"/>

    </aop:aspect>
  </aop:config>

</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值