超抽象超类中实现的超级接口方法(Aspect on super interface method implemented in abstract super class)

编程入门 行业动态 更新时间:2024-10-27 18:18:35
抽象超类中实现的超级接口方法(Aspect on super interface method implemented in abstract super class)

我有一个类似于以下问题: 如何在从“超级”接口扩展的接口方法上创建方面 ,但我的save方法是在抽象超类中。

结构如下 -

接口:

public interface SuperServiceInterface { ReturnObj save(ParamObj); } public interface ServiceInterface extends SuperServiceInterface { ... }

实现:

public abstract class SuperServiceImpl implements SuperServiceInterface { public ReturnObj save(ParamObj) { ... } } public class ServiceImpl implements ServiceInterface extends SuperServiceImpl { ... }

我想检查对ServiceInterface.save方法的任何调用。

我目前的切入点看起来像这样:

@Around("within(com.xyz.api.ServiceInterface+) && execution(* save(..))") public Object pointCut(final ProceedingJoinPoint call) throws Throwable { }

将save方法放入ServiceImpl时会触发它,但不会在SuperServiceImpl触发它。 我在周围的切入点中缺少什么?

I have a problem quite similar to: How to create an aspect on an Interface Method that extends from A "Super" Interface, but my save method is in an abstract super class.

The structure is as follows -

Interfaces:

public interface SuperServiceInterface { ReturnObj save(ParamObj); } public interface ServiceInterface extends SuperServiceInterface { ... }

Implementations:

public abstract class SuperServiceImpl implements SuperServiceInterface { public ReturnObj save(ParamObj) { ... } } public class ServiceImpl implements ServiceInterface extends SuperServiceImpl { ... }

I want to inspect any calls made to the ServiceInterface.save method.

The pointcut I have currently looks like this:

@Around("within(com.xyz.api.ServiceInterface+) && execution(* save(..))") public Object pointCut(final ProceedingJoinPoint call) throws Throwable { }

It gets triggered when the save method is put into the ServiceImpl, but not when it is in the SuperServiceImpl. What am I missing in my around pointcut?

最满意答案

我只想在ServiceInterface上进行切入点,如果我在SuperServiceInterface上执行此操作,它是否也会拦截同样继承自SuperServiceInterface接口上的保存调用?

是的,但您可以通过将target()类型限制为ServiceInterface来避免这种情况,如下所示:

@Around("execution(* save(..)) && target(serviceInterface)")
public Object pointCut(ProceedingJoinPoint thisJoinPoint, ServiceInterface serviceInterface)
    throws Throwable
{
    System.out.println(thisJoinPoint);
    return thisJoinPoint.proceed();
}

I just want to pointcut on the ServiceInterface, if I do it on SuperServiceInterface wouldn't it also intercept save calls on interfaces that also inherit from SuperServiceInterface?

Yes, but you can avoid that by restricting the target() type to ServiceInterface as follows:

@Around("execution(* save(..)) && target(serviceInterface)")
public Object pointCut(ProceedingJoinPoint thisJoinPoint, ServiceInterface serviceInterface)
    throws Throwable
{
    System.out.println(thisJoinPoint);
    return thisJoinPoint.proceed();
}

                    
                     
          

更多推荐

本文发布于:2023-07-23 04:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1227561.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抽象   类中   接口   方法   Aspect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!