- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试打印“Hello, AOP!”每当 Guice/AOP Alliance 截获标有特定(自定义)注解的方法时,就会发出消息。我遵循了官方文档(可以找到的 PDF here - 第 11 页上的 AOP 方法拦截内容)但无法让它工作,只能编译。
首先,我的注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@BindingAnnotation
public @interface Validating {
// Do nothing; used by Google Guice to intercept certain methods.
}
然后,我的Module
实现:
public class ValidatingModule implements com.google.inject.Module {
public void configure(Binder binder) {
binder.bindInterceptor(Matchers.any(),
Matchers.annotatedWith(Validating.class,
new ValidatingMethodInterceptor()),
}
}
接下来,我的方法拦截器:
public class ValidatingMethodInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("Hello, AOP!");
}
}
最后,尝试使用所有这些 AOP 内容的驱动程序:
public class AopTest {
@Validating
public int doSomething() {
// do whatever
}
public static main(String[] args) {
AopTest test = new AopTest();
Injector injector = Guice.createInjector(new ValidatingModule());
System.out.println("About to use AOP...");
test.doSomething();
}
}
当我运行这个小测试驱动程序时,我得到的唯一控制台输出是 About to use AOP...
... Hello, AOP!
永远不会被执行,这意味着 @Validating doSomething()
方法永远不会像 Guice 文档显示的那样被拦截。
唯一我能想到的是在我的 Module
中实现我指定 MethodInterceptor
绑定(bind)到(作为 bindInterceptor
方法的第三个参数)作为 new ValidatingMethodInterceptor()
,而在那个拦截器中,我只定义了一个必需的 invoke(MethodInvocation
) 方法。
也许我没有正确地将这两者连接在一起?也许 Guice 并不知道 invoke
发生拦截时应该运行方法?!?!
话又说回来,我不仅遵循了 Guice 文档,而且还遵循了其他几个教程,但都无济于事。
我在这里明显遗漏了什么吗?提前致谢!
编辑 我的代码和我遵循的示例之间的另一个差异虽然很小,但事实上我的 invoke
方法(在拦截器内部)未用 @Override
注释.如果我尝试添加此注释,则会出现以下编译错误:
The method invoke(MethodInvocation) of type ValidatingMethodInterceptor must override a superclass method.
这个错误是有道理的,因为org.aopalliance.intercept.MethodInterceptor
是一个接口(interface)(不是类)。再一次,每个使用 Guice/AOP Alliance 的例子都使用这个 @Override
invoke
上的注释方法,所以它显然对某些人有效/编译...很奇怪。
最佳答案
如果您不让 Guice 构造您的对象,它就无法为您提供一个包含拦截器的实例。您不得使用 new AopTest()
来获取对象的实例。相反,您必须要求 Guice 给您一个实例:
Injector injector = Guice.createInjector(new ValidatingModule ());
AopTest test = injector.getInstance(AopTest.class);
参见 http://code.google.com/p/google-guice/wiki/GettingStarted
关于java - 无法让 Guice 方法拦截工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8862531/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!