当前位置: 首页> 游戏> 网游 > 使用自定义注解和Spring AOP实现前后置方法调用

使用自定义注解和Spring AOP实现前后置方法调用

时间:2025/7/15 6:02:43来源:https://blog.csdn.net/weixin_40413961/article/details/140112621 浏览次数:0次

使用自定义注解和Spring AOP实现前后置方法调用

在开发过程中,经常会遇到需要在方法执行前后添加一些逻辑的情况,例如记录日志、性能监控、事务管理等。使用Spring AOP(Aspect-Oriented Programming)和自定义注解,可以方便地实现这些需求。本文将详细介绍如何通过Java和Spring AOP实现一个自定义注解,用于在方法执行前后调用指定的函数。

1. 创建自定义注解

首先,我们创建一个自定义注解 @ExecuteFunctions,并增加一个 beforeParam 参数,用于传递给前置方法。注解的代码如下:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExecuteFunctions {int beforeParam() default 0;
}

这个注解具有一个 beforeParam 参数,默认值为 0,它将用于传递一个整数值给前置方法。

2. 创建服务类

接下来,我们创建一个服务类 FunctionService,其中包含前置和后置方法。前置方法 functionBefore 接收一个整数参数,后置方法 functionAfter 不需要参数。

import org.springframework.stereotype.Service;@Service
public class FunctionService {public void functionBefore(int param) {System.out.println("Function Before Execution with param: " + param);}public void functionAfter() {System.out.println("Function After Execution");}
}
3. 创建切面类

为了在方法执行前后调用指定的函数,我们需要创建一个切面类 FunctionAspect。这个类使用 @Aspect 注解定义一个切面,并使用 @Around 注解定义环绕通知,处理前置和后置函数的执行。

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.lang.reflect.Method;@Aspect
@Component
public class FunctionAspect {@Autowiredprivate FunctionService functionService;@Pointcut("@annotation(executeFunctions)")public void executeFunctionsPointcut(ExecuteFunctions executeFunctions) {}@Around("executeFunctionsPointcut(executeFunctions)")public Object around(ProceedingJoinPoint joinPoint, ExecuteFunctions executeFunctions) throws Throwable {int beforeParam = executeFunctions.beforeParam();// 执行前置函数invokeFunction("functionBefore", beforeParam);Object result = null;try {// 执行目标方法result = joinPoint.proceed();} finally {// 执行后置函数invokeFunction("functionAfter");}return result;}private void invokeFunction(String functionName, int param) throws Exception {Method method = FunctionService.class.getMethod(functionName, int.class);method.invoke(functionService, param);}private void invokeFunction(String functionName) throws Exception {Method method = FunctionService.class.getMethod(functionName);method.invoke(functionService);}
}

FunctionAspect 类中,我们定义了一个 @Around 通知,用于在目标方法执行前后调用前置和后置方法。invokeFunction 方法通过反射调用带有参数或不带参数的方法。

4. 使用自定义注解

在需要执行前置和后置函数的方法上使用 @ExecuteFunctions 注解,并指定 beforeParam 参数。例如,在一个控制器类中使用这个注解:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class DemoController {@Autowiredprivate FunctionService functionService;@ExecuteFunctions(beforeParam = 42)@GetMapping("/test")public String test() {System.out.println("Executing target method");return "Hello, World!";}
}
5. 启动Spring Boot应用

确保你的Spring Boot应用正确配置并启动。访问 /test 路径,应该会看到如下输出:

Function Before Execution with param: 42
Executing target method
Function After Execution
解释
  1. 自定义注解

    • @ExecuteFunctions 注解增加了一个 beforeParam 参数,用于传递一个整数值给 functionBefore 方法。
  2. 服务类

    • FunctionService 定义了带有 int 参数的 functionBefore 方法和无参数的 functionAfter 方法。
  3. 切面类

    • FunctionAspect 类使用 @Aspect 注解定义一个切面,使用 @Around 注解定义了环绕通知,处理前置和后置函数的执行。
    • invokeFunction 方法通过反射调用带有 int 参数的 functionBefore 方法,并在方法参数不为空时传递参数。
  4. 控制器

    • DemoController 类中,使用 @ExecuteFunctions 注解标注需要执行前置和后置函数的方法,并指定 beforeParam 参数。

通过这种方式,我们实现了一个灵活的解决方案,可以在方法执行前后调用指定的函数,并且支持通过注解参数传递值给前置函数。这样的实现不仅提高了代码的可读性和可维护性,还增强了代码的扩展性和复用性。

关键字:使用自定义注解和Spring AOP实现前后置方法调用

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: