Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java cglib代理类中缺少字段批注_Java_Cglib - Fatal编程技术网

Java cglib代理类中缺少字段批注

Java cglib代理类中缺少字段批注,java,cglib,Java,Cglib,在这种情况下,此代码很好。但当我在方法run中添加@Transactional时,@DynamicReference将丢失 DynamicReference dynamicRefrence = filed.getAnnotation(DynamicReference.class); -->NOT NULL 如何在cglib代理类中获取字段注释@DynamicReference 这是get字段代码: DynamicReference dynamicRefrence = filed.getA

在这种情况下,此代码很好。但当我在方法run中添加
@Transactional
时,
@DynamicReference
将丢失

DynamicReference dynamicRefrence = filed.getAnnotation(DynamicReference.class);
-->NOT NULL
如何在cglib代理类中获取字段注释
@DynamicReference

这是get字段代码:

DynamicReference dynamicRefrence = filed.getAnnotation(DynamicReference.class);
-->NULL
objecto=this.applicationContext.getBean(beanName);
Class clazz=o.getClass();
for(字段字段:clazz.getDeclaredFields()){
DynamicReference DynamicReference=field.getAnnotation(DynamicReference.class);
}

来自
Class.getDeclaredFields()

返回字段对象数组,该数组反映由该类对象表示的类或接口声明的所有字段。这包括公共、受保护、默认(包)访问和私有字段,但不包括继承的字段


在您的例子中,一旦您从cglib获得了一个基于子类的代理,该字段将只存在于超类中。根据您的用例,您可能希望收集继承链中具有自定义注释的所有字段

示例代码:

Object o = this.applicationContext.getBean(beanName);
Class<?> clazz = o.getClass();

for (Field filed : clazz.getDeclaredFields()) {
    DynamicReference dynamicRefrence = filed.getAnnotation(DynamicReference.class);
}
代理应该将方法调用委托给包装的实例。也许你甚至想保护这个方法

另一种方法是获取代理的回调字段并在该实例上设置该字段,但上面的方法似乎更干净(有些人可能会说,magic field injection是邪恶的,您应该始终使用setter/constructor injection来实现干净的oop方法)


编辑2:如果你真的想重新发明DI框架并利用现有的DI框架的底层功能,也许你也可以重新考虑一下。使用@Qualifier或一些定制的注入解析器会让人想到。参见eg

@DynamicReference is custem Annotation您还可以显示您在哪里获得已添加的
字段
(或者将其命名为
字段
)。clazz.getDeclaredFields()找不到TestProvider取决于您的用例,您可能希望迭代所有超类,它应该适用于cglib代理(请参阅)明白了。谢谢,现在我可以在super类中获取字段,然后我使用field.set()设置值,看起来已经设置好了,但在cglib代理类中它仍然为空。我可以做什么来设置代理类字段的值?请参见我的编辑,您可能需要根据您的具体问题对其进行调整。
DynamicReference dynamicRefrence = filed.getAnnotation(DynamicReference.class);
-->NULL
Object o = this.applicationContext.getBean(beanName);
Class<?> clazz = o.getClass();

for (Field filed : clazz.getDeclaredFields()) {
    DynamicReference dynamicRefrence = filed.getAnnotation(DynamicReference.class);
}
    Collection<Field> fieldsWithAnnotation = new ArrayList<>();

    Class<?> clazz = // your class

    while(clazz != null) {
        for (Field field : clazz.getDeclaredFields()) {
            DynamicReference dynamicRefrence = field.getAnnotation(DynamicReference.class);
            if(dynamicRefrence != null)
                fieldsWithAnnotation.add(field);
        }
        clazz = clazz.getSuperclass();
    }
// in TestService
private ITestProvider testProvider;

@DynamicReference
public void setTestProvider(ITestProvider testProvider) { ... }

// Getting the method
while(clazz != null) {
    for (Method method : clazz.getDeclaredMethods()) {
        DynamicReference dynamicRefrence = method.getAnnotation(DynamicReference.class);
        if(dynamicRefrence != null)
            methodsWithAnnotation.add(method);
    }
    clazz = clazz.getSuperclass();
}

// invoking it
method.invoke(proxyInstance, dependencyInstanceYouWantToSet);