初始化所有 Bean 属性
我目前正在研究一些包含大量属性的 POJO。我必须确保所有属性都已初始化。是否有一些 util 类可以在运行时为我处理这个问题?
一个极好的功能是检查空值并初始化为默认值。
I'm working at the moment on some POJOs which are containing a huge bunch of properties. I have to ensure that all properties are initialized. Are there some util classes which could handle this for me at runtime?
A superb feature would be to check for null value and init to default value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 Apache Commons BeanUtils 来执行此操作。假设您的 POJO 遵循属性和 getter/setter 方法的 Java Bean 命名约定,如果您的属性值位于
Map
中,您可以使用BeanUtils.populate
用一组预定义的属性值填充 beans。如果要将值从一个 bean 复制到另一个 bean,请使用BeanUtils.copyProperties
。You can use Apache Commons BeanUtils to do this. Assuming that your POJOs follow the Java Bean naming conventions for properties and getter/setter methods, if have your property values in a
Map
you can useBeanUtils.populate
to populate beans with a predefined set of property values. If you want to copy values from one bean to another, useBeanUtils.copyProperties
.