我正在尝试围绕扩展如下所示接口(interface)的方法提出建议:
public interface StructureService {
void delete(FileEntry entry);
}
public interface FileService extends StructureService {
void dummy();
}
实现这些的类如下所示:
public class DbStructureService implements StructureService {
public void delete(FileEntry entry) {
}
}
public class DbFileService extends DbStructureService implements FileService {
public void dummy() {
}
}
我正在尝试匹配删除方法,但仅限于实现 FileService 的类。
我定义了以下方面:
public aspect FileServiceEventDispatcherAspect {
pointcut isFileService() : within(org.service.FileService+);
pointcut delete(FileEntry entry) :
execution(void org.service.StructureService.delete(..))
&& args(entry) && isFileService();
void around(FileEntry entry) : delete(entry) {
proceed(entry);
}
}
问题是,只要我启用了 isFileService 切入点,它就不会匹配任何类;尽管有很多方法应该与此匹配
如果我将within(org.service.FileService+)
替换为within(org.service.StructureService+)
,它也可以正常工作。
我尝试过使用 this() 等进行实验,但没有成功。我该如何在aspectj中执行此操作?
编辑: 更新了实现接口(interface)的类的外观。我认为这种情况可能很难建议,因为 DbFileService 中没有重写方法
最佳答案
我想您的意思是 DbFileService 实现了 FileService 但没有实现 StructureService。鉴于此,这段代码应该可以工作:
public aspect FileServiceEventDispatcherAspect {
pointcut delete(FileService this_, FileEntry entry) :
execution(void org.service.StructureService.delete(..))
&& args(entry) && this(this_);
void around(FileService this_, FileEntry entry) : delete(this_, entry) {
proceed(this_, entry);
}
}
“内部”切入点在这里不合适,因为它是“基于词法结构的切入点”( "AspectJ in Action", second edition. )
关于java - 关于实现扩展接口(interface)的方法的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7485029/