在运行时检查所有带注释的方法的参数
如何在启动时检查注释的所有用法?
例如,我有这个方面,应用于用@Protect注释的方法,应用一些安全策略。在整个系统中,我们有使用 @Protect("valid-operation-1")
、@Protect("valid-operation-2")
或 注解的方法@Protect(“无效操作”)
。应用程序启动后,我想检查为所有这些注释提供的参数,以检测此类错误配置。
特别是,我将检查 Spring 应用程序上下文中是否定义了一个 bean,其 ID 与注释的参数相匹配。这意味着,为了保护方法 voiddrive()
,我将使用 @Protect("drive")
进行注释,并期望一个 bean protect_drive
> 存在于应用程序上下文中。
您可以轻松地等到调用该方法,然后调用建议,然后检查参数。然后您会看到 INVALID-operation
定义错误。但这已经太晚了。
是否可以在应用程序启动时检查所有带注释的方法?
How can you perform a check at startup-time on all the usages of an annotation?
For instance, I have this aspect, that is applied to the methods annotated with @Protect
, that applies some security policy. Across the system, we have methods annotated with @Protect("valid-operation-1")
, @Protect("valid-operation-2")
or @Protect("INVALID-operation")
. As soon as the application starts up, I'd like to check the arguments provided for all these annotations in order to detect such misconfigurations.
In particular, I'll check that we have a bean defined in the Spring application context whose ID matches the argument of the annotation. That means, to protect the method void drive()
, I'll annotate with @Protect("drive")
, and expect a bean protect_drive
to be present in the application context.
You can easily just wait until the method is invoked, then the advice is called, and you check the argument. Then you'll see that INVALID-operation
is wrongly defined. But this is too late.
Is it possible to have this checked for all annotated methods when the application starts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要检查的类是 Spring Bean,那么您可以使用 BeanPostProcessor。
If the Classes you want to check are Spring Beans, then you can use a BeanPostProcessor.