在运行时检查所有带注释的方法的参数

发布于 2024-12-08 06:03:04 字数 555 浏览 0 评论 0原文

如何在启动时检查注释的所有用法?

例如,我有这个方面,应用于用@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛徒 2024-12-15 06:03:05

如果您要检查的类是 Spring Bean,那么您可以使用 BeanPostProcessor

public class OnlyAScratchForAnPostProcessor {

    @Inject
    private ApplicationContext context;


    @Override
    public Object postProcessAfterInitialization(final Object bean,
        final String beanName) throws BeansException {

          ReflectionUtils.doWithMethods(bean.getClass(), new MethodCallback() {

             @Override
             public void doWith(Method method) throws IllegalArgumentException,
                         IllegalAccessException {

                  String expecedNameFromAnnotation = scanAnnotation(method);
                  if(expecedNameFromAnnotation != null) {
                      if(context.beanByName(expecedNameFromAnnotation) != null) {
                         throw new RuntimeException("illegal configuration");
                      }
                  }         
             }

             String scanAnnotation(Method method){...}

          }, ReflectionUtils.USER_DECLARED_METHODS);        

    } 

If the Classes you want to check are Spring Beans, then you can use a BeanPostProcessor.

public class OnlyAScratchForAnPostProcessor {

    @Inject
    private ApplicationContext context;


    @Override
    public Object postProcessAfterInitialization(final Object bean,
        final String beanName) throws BeansException {

          ReflectionUtils.doWithMethods(bean.getClass(), new MethodCallback() {

             @Override
             public void doWith(Method method) throws IllegalArgumentException,
                         IllegalAccessException {

                  String expecedNameFromAnnotation = scanAnnotation(method);
                  if(expecedNameFromAnnotation != null) {
                      if(context.beanByName(expecedNameFromAnnotation) != null) {
                         throw new RuntimeException("illegal configuration");
                      }
                  }         
             }

             String scanAnnotation(Method method){...}

          }, ReflectionUtils.USER_DECLARED_METHODS);        

    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文