丢失代理类的类自定义注释 [英] Losing Class Custom Annotation For Proxy Classes

查看:61
本文介绍了丢失代理类的类自定义注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Seam 使用 @In 注释将 bean 注入我的控制器.注入的类有自定义注解,调用injectedClass.getClass().getAnnotation(annotationClass)时返回null.

I am using Seam to inject beans to my controller using @In annotation. The injected class has a custom annotation, when calling injectedClass.getClass().getAnnotation(annotationClass) it returns null.

调试时,我发现 Seam 传递了一个代理实例,因此 getClass() 返回 InjectedClass_$$_javassist_seam_5,它没有我的自定义注释.

When debug I found that Seam passes a proxy instance so getClass() returns InjectedClass_$$_javassist_seam_5 which doesn't have my custom annotation.

如何从代理类获取自定义注释?

How I can get my custom annotation from the proxy class?

这是我的课程的样子:

@CustomAnnotation(value="myvalue")
@Name("myAnnotatedClass")
public class MyAnnotatedClass extends SuperClass {...}

@Scope(ScopeType.SESSION)
@Name("myController")
public class MyController {
     @In("#{myAnnotatedClass}")
     private MyAnnotatedClass myAnnotatedClass;

     public void actionMethod(){
         //call another class which call myAnnotatedClass.getClass().getAnnotation(CustomAnnotation.class)
         //then do some reflection for MyAnnotatedClass fields 
     }
}

推荐答案

好问题.

使用Seam调用方法时,会被代理拦截.而这个启用@In 或@Out-jection.但是这个规则有一个例外:当你调用内部方法时它不起作用

When you call a method by using Seam, it is intercepted by a proxy. And this one enables @In or @Out-jection. But There is an exception to this rule: it does not work when you call an internal method

试试这个代码

@Name
public class Service {

    @In
    private MyAnnotatedClass myAnnotatedClass;


    public void myInterceptedMethod() {
        // internal method bypass interceptor
        // So @In or @Out-jection is not enabled
        internalMethod();
    }

    private void internalMethod() {
        System.out.println(myAnnotatedClass.getClass().getAnnotation(annotationClass));
    }

}

添加到原始答案

您想从 bean 中检索注释.但是,由于方法拦截器,myAnnotatedClass.getClass() 返回一个代理对象,而不是 bean 类本身.

You want to retrieve an annotation from your bean. But, because of method interceptor, myAnnotatedClass.getClass() returns a proxy object, not the bean class itself.

对于每个 bean 类,Seam 创建一个组件定义,其中存储在应用程序上下文中.属性的名称遵循以下模式:组件名称 加上 .component.所以如果你有一个这样的豆子

For each bean class, Seam creates a Component definition, in which is stored in the application context. The name of the attribute follows this pattern: component name plus .component. So if you have a bean like this one

@Name("myBean")
public class MyBean {

}

它的组件定义存储在属性myBean.component

Its Componet definition is stored in the attribute myBean.component

所以在你的方法中,你可以使用

So inside your method, you can use

Component myBeanComponentDefinition = (Component) Context.getApplicationContext().get("myBean.component");

现在你可以打电话了

myBeanComponentDefinition.getBeanClass().getAnnotation(CustomAnnotation.class);

问候,

这篇关于丢失代理类的类自定义注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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