Java注解执行注解声明中的方法(适用于android)
我对注释术语相当陌生。 我阅读了一些资料,得出的结论是没有回答我的问题。 也许我用错误的搜索方式进行了谷歌搜索。也许我忽略了,或者也许我只是一无所知……
无论如何,这就是交易。
我正忙于编写一个需要“角色验证”的应用程序。
为此,我想使用注释。
所以,
@interface Validate (){
}
我的目标是:
public @interface Validate() {
public Validate() {
//Do validation stuff
return true/false
}
}
所以基本上我想在注释中定义方法。 我希望能够调用
@Validate
public void methodThatRequiresAdminRole() {
doStuff();
}
只有管理员才能进入此方法。否则会产生错误。
所以如果@validate返回true,该方法应该被执行
抱歉,如果我真的不清楚。我不太确定如何正确地询问我想要什么。 所以我希望这些例子能够讲述故事。
希望尽快阅读提示甚至答案。 谢谢。
** 编辑 **
我需要强调这样一个事实:该代码必须在 Android 应用程序上使用。 所以我宁愿不使用不适合 android 的奇怪框架。我可以毫无问题地添加一个自定义库,该库无需任何类型的应用程序框架即可提供功能。
I'm fairly new to the annotation terms.
I have read some sources and came to the conclusion that non answered my question.
Perhaps I googled using the wrong search. Perhaps I overlook, or mayhbe im just clueless..
Anyway here is the deal.
I am busy writing an application that requires "role validation".
To do this I want to use an annotation.
So something along the line of:
@interface Validate (){
}
What I aim to achieve is sometihng along the lines of:
public @interface Validate() {
public Validate() {
//Do validation stuff
return true/false
}
}
So basically I want to define methods within the annotation.
I want to be able to call
@Validate
public void methodThatRequiresAdminRole() {
doStuff();
}
Only admins should be able to enter this method. Otherwise an error should be generated.
so if @validate returns true, the method should be executed
Sorry if I am really unclear. I am not quite sure how to properly ask what I want.
So I hope the examples tell the stories.
Hope to read tips and perhaps even an answer soon.
Thanks.
** EDIT **
I need to stress out the fact that the code must be used on an android application.
So I prefer to not use strange frameworks not meant for android. I have no problem adding a custom library that gives the functionality without any kind of application framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注释是元数据。您需要编写的是注释处理器。注释本身无法完成验证逻辑。注释处理器将查看注释,然后进行验证并控制应用程序流程。这个答案有一个很好的解释Java注释可以帮助我解决这个问题?
您还需要使用 @Retention(RetentionPolicy.RUNTIME) 对注解进行注解,以便将注解信息保留到运行时。
Annotations are meta data. What you need to write is an annotation processor. An annotation in itself cannot accomplish the validation logic. The annotation processor will look at the annotations and then do the validation and control the application flow. This SO answer has a good explanation Can Java Annotations help me with this?
You also need to annotate the annotation with @Retention(RetentionPolicy.RUNTIME) so that the annotation information is preserved till the runtime.
请注意,这可能完全偏离主题。使用 spring AOP 处理注释相当简单:
创建一个切面:
设置您的切面:
在 META-INF/aop.xml 中
设置 加载时间编织
在 spring 上下文中:
确保 spring-agent 与您的应用程序一起启动:
Note, this might be quite off-topic. Using spring AOP with, processing the annotations is fairly straightforward:
Create an aspect:
Set-up your aspect:
In META-INF/aop.xml
Set-up load time weaving
In the spring context:
Ensure that the spring-agent is started with your app:
我认为仅通过注释无法实现这一目标。它们的目的是提供有关代码元素的元信息,而不是处理逻辑。
要实际实现对带注释的方法调用的限制,您需要在方法内部或外部手动检查访问权限,或者使用类似 http://www.eclipse.org/aspectj/
I don't think you can achieve this with annotations alone. They are meant to provide meta information about code elements and not processing logic.
To actually implement the restriction on the annotated methods invocation you will need to check the access by hand inside or outside the method or inject such a check using something like http://www.eclipse.org/aspectj/