【发布时间】:2019-05-28 20:36:29
【问题描述】:
如何在方法声明中使用@around spring AOP 注解?实际上java类中有很多重复的代码,所以我正在考虑优化它。只有@around执行值每次都会改变,3-4个方法的方法定义是相同的。你能建议我能做什么这种情况是为了代码优化?在给定的示例中,您可以看到 nicdStatus 和 nicdPortStatus 只发生了变化,其余所有方法定义都是相同的。 请提供一些代码优化建议,因为我的 java 类中有重复的代码。
@Around("execution(* dcp.casa.services.nicd.NicdController.**nicdStatus**(..)) && args(*, relationId,..)")
Object handleRunTest(final ProceedingJoinPoint joinPoint, final String relationId) {
log.info("xyz");
callAbc();
return joinPoint.proceed();
}
@Around("execution(* dcp.casa.services.nicd.NicdController.nicdPortStatus(..)) && args(*, relationId,..)")
Object handleRunTest(final ProceedingJoinPoint joinPoint, final String relationId) {
log.info("xyz");
callAbc();
return joinPoint.proceed();
}
【问题讨论】:
-
方法声明?
-
是的,我可以在方法声明中使用它吗?比如@Around("execution(* dcp.casa.services.nicd.NicdController.nicdStatus(..)) && args(*, relationId,..)") Object handleRunTest(final ProceedingJoinPoint joinPoint, final String relationId);或者做你知道如何优化上面的代码吗?
-
欢迎来到 SO。你的问题有点不清楚。我猜对了,您有多个具有相同方法主体的
@Around建议方法,并且您想将这些方法主体分解为辅助方法,以避免您的方面中的代码重复? -
是的,你是正确的@Kriegaex。你明白我的问题。
标签: java spring-boot methods spring-aop spring-annotations