Spring 环绕增强

[java]  view plain  copy
  1. public interface ISomeService1 {  
  2.     public  void  some();  
  3. }  
  4. public class MyThome implements MethodInterceptor{  
  5.     public Object invoke(MethodInvocation methodInvocation) throws Throwable {  
  6.         System.out.println("前置 环绕");  
  7.        methodInvocation.proceed();  
  8.         System.out.println("后置 环绕");  
  9.         return null;  
  10.     }  
  11. }  
  12. public class SomeService1 implements ISomeService1 {  
  13.     public  void  some(){  
  14.         System.out.println("这是测试 bean 代理");  
  15.     }  
  16. }  

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.   
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.   
  7.        xmlns:context="http://www.springframework.org/schema/context"  
  8.   
  9.        xmlns:p="http://www.springframework.org/schema/p"  
  10.   
  11.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  12.        http://www.springframework.org/schema/beans/spring-beans.xsd  
  13.   
  14.        http://www.springframework.org/schema/aop  
  15.        http://www.springframework.org/schema/aop/spring-aop.xsd  
  16.   
  17.        http://www.springframework.org/schema/context  
  18.        http://www.springframework.org/schema/context/spring-context.xsd  
  19.  ">  
  20.   
  21.     <!--属性-->  
  22.     <bean id="some1" class="cn.springAOP2.SomeService1"></bean>  
  23.   
  24.     <!--前置-->  
  25.     <bean id="MyThome1" class="cn.springAOP2.MyThome"></bean>  
  26.   
  27.     <bean id ="HuanRao" class="org.springframework.aop.framework.ProxyFactoryBean">  
  28.         <!--需要增强的对象-->  
  29.         <property name="target" ref="some1"></property>  
  30.         <!--需要拦截的方法-->  
  31.         <property name="interceptorNames" value="MyThome1"></property>  
  32.   
  33.   
  34.     </bean>  
  35. </beans>  
Spring框架提供了一套强大的AOP(面向切面编程)机制,其中就包括了环绕通知(Around Advice)。环绕通知允许你在方法执行前、执行过程中或执行后插入自定义的逻辑,这在需要对业务流程做全局控制或者增强原有功能时非常有用。 在Spring中,我们可以使用`@Around`注解来创建环绕通知。首先,你需要创建一个切点(Pointcut)定义需要拦截的方法或类,然后编一个处理通知逻辑的Advisor,通常是一个实现了`MethodBeforeAdvice`或`MethodInterceptor`接口的类。 以下是一个简单的示例: ```java import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @Aspect @Component public class LoggingAspect { @Around("execution(* com.example.service.*.*(..))") // 定义切点:拦截com.example.service包下的所有方法 public Object logAndExecute(ProceedingJoinPoint joinPoint) throws Throwable { long startTime = System.currentTimeMillis(); // 记录开始时间 try { System.out.println("Before method execution"); // 执行原方法 return joinPoint.proceed(); // call the original method } finally { long endTime = System.currentTimeMillis(); long duration = endTime - startTime; // 计算耗时 System.out.println("After method execution, took " + duration + "ms"); } } } ``` 在这个例子中,`LoggingAspect`切面拦截了`com.example.service`包下所有方法的执行。`logAndExecute`方法会在每个方法调用前打印“Before method execution”,并在执行结束后计算并打印方法执行的时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值