反射集bean字段

0s7z1bwu  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(165)

@Service
public class BeanClass extends Something implements Something1, Something2 {
    private String myField;
}

使用

public static void doSomething(ApplicationContext applicationContext){
    Object bean = applicationContext.getBean("beanClass");
    set(bean, AopUtils.getTargetClass(object).getFields[0], "hello")
}

public static void set(Object bean, Field field, Object value){
    if(bean == null || field == null){throw new RuntimeException();}
    field.setAccessible(true);
    field.set(bean, value);
}

上面抛出了异常
由java.lang.illegalargumentexception引起:无法将java.lang.string字段my.package.beanclass.myfield设置为空值
如何设置 Field 使用 Reflect ?
我可以确认没有一个参数是正确的 null ... 这无论如何都不会影响它,因为我应该能够将引用类型设置为null。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题