java 如何在从“超级”接口扩展的接口方法上创建方面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4958400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:47:23  来源:igfitidea点击:

How to create an aspect on an Interface Method that extends from A "Super" Interface

javaspringaopaspectj

提问by El Guapo

I have a service-layer Interface that extends from a base Interface; I would like to create a Pointcut around my service-layer Interface, but on one of the methods defined in the base Interface.

我有一个从基本接口扩展的服务层接口;我想围绕我的服务层接口创建一个切入点,但是在基本接口中定义的方法之一上。

For instance.... I have a method in my base Interface called "save()", I put it in my base Interface since just all of my "child" Interfaces will provide "save" functionality.

例如....我在我的基本接口中有一个名为“save()”的方法,我把它放在我的基本接口中,因为我所有的“子”接口都将提供“保存”功能。

I would like to create a PointCut on only one of my "child" interfaces for when my "save" gets called.

当我的“保存”被调用时,我只想在我的一个“子”界面上创建一个 PointCut。

I created the pointcut like the following:

我创建了如下切入点:

@Pointcut("execution(* com.xyz.someapp.ChildServiceInterface.save(..))")  
public void childServiceSavePointCut();

I then created a @Around advice around the above pointcut like the following:

然后我围绕上述切入点创建了一个@Around 建议,如下所示:

@Around("childServiceSavePointCut()")
public void doMyAdvice()....

where "ChildServiceInterface" extends another Interface which has the "save()" method defined.

其中“ChildServiceInterface”扩展了另一个定义了“save()”方法的接口。

My Advice never runs... I debugged my code and do not see my Advice in the list of Advisors for my target service.

我的建议从未运行...我调试了我的代码,但在目标服务的顾问列表中没有看到我的建议。

Am I way off base thinking this will work, or am I implementing it incorrectly?

我是不是认为这会奏效,还是我错误地实施了它?

回答by OrangeDog

Try this pointcut instead.

试试这个切入点。

within(com.xyz.someapp.ChildServiceInterface+) && execution(* save(..))

The +indicates a subtype pattern.

所述+指示子类型图案

回答by pokemon blue

Or you can put pointcut on all the methods of that class using

或者您可以使用该类的所有方法放置切入点

@Pointcut("execution(* com.xyz.someapp.ChildServiceInterface.*(..))")  
public void childServiceSavePointCut();

The *indicates all method type.

*表示所有方法类型。