如果bean中的所有对象都实现了Serializable接口,那么会BeanUtils.cloneBean()进行深层复制吗?
我试图将属性从一个bean复制到另一个bean.这是两个bean的签名:
SearchContent:
public class SearchContent implements Serializable {
private static final long serialVersionUID = -4500094586165758427L;
private Integer id;
private String docName;
private String docType;
private String docTitle;
private String docAuthor;
private String securityGroup;
private String docAccount;
private Integer revLabel;
private String profile;
private LabelValueBean<String> workflowStage;
private Date createDate;
private Date inDate;
private String originalName;
private String format;
private String extension;
private Long fileSize;
private String author;
private LabelValueBean<String> entity;
private LabelValueBean<String> brand;
private LabelValueBean<String> product;
private LabelValueBean<String> collection;
private …Run Code Online (Sandbox Code Playgroud) 我有一个获取POJO作为参数的方法.现在我想以编程方式获取POJO的所有属性(因为我的代码可能不知道运行时它的所有属性是什么),并且还需要获取属性的值.最后,我将形成POJO的字符串表示.
我可以使用ToStringBuilder,但我希望以特定于我的要求的特定格式构建输出字符串.
是否有可能在Beanutils中这样做?如果是,任何指向方法名称的指针?如果不是,我应该编写自己的反射代码吗?
使用Commons beanUtils我想知道如何询问任何转换器说Dateconverter忽略空值并使用null作为默认值.作为一个例子考虑公共课,
public class X {
private Date date1;
private String string1;
//add public getters and setters
}
Run Code Online (Sandbox Code Playgroud)
和我的转换为,
public class Apache {
@Test
public void testSimple() throws Exception {
X x1 = new X(), x2 = new X();
x1.setString1("X");
x1.setDate1(null);
org.apache.commons.beanutils.BeanUtils.copyProperties(x2, x1);
//throws ConversionException
System.out.println(x2.getString1());
System.out.println(x2.getDate1());
}
}
Run Code Online (Sandbox Code Playgroud)
由于日期恰好为null,因此抛出NPE.对于我来说,这看起来是一个非常原始的场景,应该默认处理(例如,我希望x2对date1有空值).doco告诉我,我可以要求转换器这样做.有人能指出我这样做的最佳方式吗?
我不想得到转换器和isUseDefault()是真的,因为那时我必须为所有日期,枚举和许多其他转换器做!
我正在使用BeanUtils来操作通过JAXB创建的Java对象,我遇到了一个有趣的问题.有时,JAXB会创建一个这样的Java对象:
public class Bean {
protected Boolean happy;
public Boolean isHappy() {
return happy;
}
public void setHappy(Boolean happy) {
this.happy = happy;
}
}
Run Code Online (Sandbox Code Playgroud)
以下代码工作得很好:
Bean bean = new Bean();
BeanUtils.setProperty(bean, "happy", true);
Run Code Online (Sandbox Code Playgroud)
但是,试图获得这样的happy属性:
Bean bean = new Bean();
BeanUtils.getProperty(bean, "happy");
Run Code Online (Sandbox Code Playgroud)
此例外的结果:
Exception in thread "main" java.lang.NoSuchMethodException: Property 'happy' has no getter method in class 'class Bean'
Run Code Online (Sandbox Code Playgroud)
将所有内容更改为原语boolean允许set和get调用工作.但是,我没有这个选项,因为这些是生成的类.我假设发生这种情况是因为is<name>如果返回类型是基元boolean,而不是包装器类型,Java Bean库只考虑表示属性的方法Boolean.有没有人建议如何通过BeanUtils访问这些属性?我可以使用某种解决方法吗?
是否有一个版本的BeanUtils.describe(客户)以'customer'的复杂属性递归调用describe()方法.
class Customer {
String id;
Address address;
}
Run Code Online (Sandbox Code Playgroud)
在这里,我想使用describe方法来检索address属性的内容.
目前,我所有人都可以看到该类的名称如下:
{id=123, address=com.test.entities.Address@2a340e}
Run Code Online (Sandbox Code Playgroud) Spring BeanUtils.copyProperties()提供了在复制bean时忽略特定属性的选项:
public static void copyProperties(Object source,
Object target,
String[] ignoreProperties) throws BeansException
Run Code Online (Sandbox Code Playgroud)
Apache Commons BeanUtils是否提供类似的功能?
使用Spring时也可以忽略空值BeanUtils.copyProperties(),我在Commons BeanUtils中看到这个功能:
Date defaultValue = null;
DateConverter converter = new DateConverter(defaultValue);
ConvertUtils.register(converter, Date.class);
Run Code Online (Sandbox Code Playgroud)
我可以用Spring的BeanUtils实现同样的目标吗?
List<Question> questions = new ArrayList<Question>();
questions.addAll(getAllQuestions()); //returns a set of Questions
Collections.sort(questions, new BeanComparator("questionId")); //org.apache.commons.beanutils.BeanComparator
Run Code Online (Sandbox Code Playgroud)
在Java 1.5下,上述工作正常,只是'new BeanComparator("questionId")'生成一个未经检查的警告.我不喜欢警告.有没有办法可以为BeanComparator提供一个类型,还是我必须使用@SuppressWarnings("unchecked")?
是否有现成的例程来检查bean是否具有字符串给出的特定属性名称的getter?
将公共集合从3.2.2升级到4.1后,我遇到了比较BeanPredicate和EqualsPredicate集合的问题.BeanPredicate来自commons-beanutils jar,而EqualsPredicate来自commons-collection.jar.
BeanPredicate namePredicate = new BeanPredicate(propertyName, new EqualPredicate("someString"));
Run Code Online (Sandbox Code Playgroud)
根据最新的commons-collection jar 4.1,EqualsPredicate Constructor接受参数化类型
public EqualPredicate(T object)
{
this(object, null);
}
Run Code Online (Sandbox Code Playgroud)
不是3.2.2中的对象
public EqualPredicate(Object object)
{
this.iValue = object;
}
Run Code Online (Sandbox Code Playgroud)
BeanPredicate没有参数化类型,就像<T>它的构造函数那样,为什么我得到编译问题而且无法在这两个谓词之间建立关系.我看到最新的commons-beanUtils 1.9.3 BeanPredicate jar也有BeanPredicate构造函数没有参数化.我尝试给类型,但它不工作.如何解决这个问题任何帮助将不胜感激.
java ×10
javabeans ×3
reflection ×2
comparator ×1
deep-copy ×1
generics ×1
jaxb ×1
mapping ×1
properties ×1
spring ×1