问题描述
propertyDescriptor.setPropertyEditorClass(DatePropertyEditor.class);
PropertyEditor propertyEditor = propertyDescriptor.createPropertyEditor(user); // 返回null
解决方法——外部类
自定义的属性编辑器DatePropertyEditor没有用public修饰,给DatePropertyEditor加上public。
解决方法——内部类
对于内部类不仅需要使用public,还只能使用静态内部类。
源码分析
propertyDescriptor.createPropertyEditor(user)
使用反射创建DatePropertyEditor实例。
可以看到,cls.newInstance()无论是外部类还是内部类都需要public修饰,并且内部类还需要是静态内部类。