CDI扩展 - 在ProcessAnnotatedType阶段添加拦截器 [英] CDI Extensions - Add Interceptors in ProcessAnnotatedType phase

查看:123
本文介绍了CDI扩展 - 在ProcessAnnotatedType阶段添加拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式添加拦截器。拦截器称为LogginInterceptor,只记录它运行的方法的名称。当在方法上使用注释@Interceptors(LogginInterceptor.class)时,一切正常。

I am trying to add an interceptor programatically. The interceptor is called LogginInterceptor and just logs the name of method it runs on. When using it with annotation @Interceptors(LogginInterceptor.class) on a method, everything works fine.

然而,我正在尝试创建添加此@Interceptors的CDI扩展(LogginInterceptor) )以编程方式对某个类的每个方法进行注释(简单)。所以,尝试一下,我用几种方法创建了Hello类。当手动注释这些方法时,LogginInterceptor将工作并打印方法的名称。但是,我通过CDI扩展以编程方式添加此代码的代码不起作用。

I am however trying to create CDI Extension that adds this @Interceptors(LogginInterceptor) annotation programatically to every method of a certain class (to be simple). So, try it, I've created class Hello with few methods. When these methods are annotated manually, the LogginInterceptor works and prints the name of the method. However, my code to add this programatically via CDI extensions does not work.

    <T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> pat) {

    if (pat.getAnnotatedType().getJavaClass().equals(Hello.class)) {
        Logger.getLogger("").info("Initial annotations: " + pat.getAnnotatedType().getAnnotations());


        Map<String, Class[]> values = new HashMap<>();
        Class[] classes = {LoggingInterceptor.class};
        values.put("value", classes);
        Interceptors interceptors = AnnotationInstanceProvider.of(Interceptors.class, values);
        AnnotatedTypeBuilder<T> builder = new AnnotatedTypeBuilder<T>().readFromType(pat.getAnnotatedType());

        pat.getAnnotatedType().getMethods().forEach(method -> {
            builder.addToMethod(method, interceptors);
        });

        pat.setAnnotatedType(builder.create());
        Logger.getLogger("").info("Ending annotations: " + pat.getAnnotatedType().getAnnotations());
    }
}

我正在为AnnotationInstanceProvider和AnnotatedTypeBuilder使用Apache Deltaspike。创建注释,以及包装AnnotatedType。但是,拦截器无法正常工作。

I am using Apache Deltaspike for AnnotationInstanceProvider and AnnotatedTypeBuilder. The annotation is created, as well as the wrapping AnnotatedType. However, the interceptor is not working.

我使用 WildFly 9

PS:我也注意到拦截器绑定对我不起作用。只有@Interceptors注释才有效。

PS: I also noticed that interceptor bindings do not work for me. Only @Interceptors annotation works.

感谢您的任何建议。

推荐答案

最好通过AnnotatedTypeBuilder#addToClass将注释实例添加到整个bean。不要忘记每个归档(通过beans.xml)启用拦截器,或者通过@javax.annotation.Priority全局启用拦截器。您甚至可以动态创建它的实例并将其添加到拦截器实现类(使用@Interceptor注释)。没有它,你的@InterceptorBinding根本无法工作。

It's better to add the annotation-instance to the whole bean via AnnotatedTypeBuilder#addToClass. Don't forget to enable the interceptor per archive (via beans.xml) or globally via @javax.annotation.Priority. You can even create an instance of it dynamically and add it to your interceptor-implementation class (which is annotated with @Interceptor). Without that your @InterceptorBinding can't work at all.

这篇关于CDI扩展 - 在ProcessAnnotatedType阶段添加拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆