豆
@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。
暂无答案!
目前还没有任何答案,快来回答吧!