lis*_*sak 4 java spring javabeans
有没有人有一个简单的方法来打印出bean属性值?没有复杂的instrospection构造通过获取propertyDescriptors等我在谈论测试和检查所有属性是否具有正确的值,在开发过程中.
PropertyDescriptors是可行的方法,但是如果你使用BeanWrapper接口,Spring会更容易使用它们.
这是一个愚蠢的测试类:
public class Thingy{
private final String foo = "hey";
private final int bar = 123;
private final List<String> grr = Arrays.asList("1", "2", "3");
public String getFoo(){
return this.foo;
}
public int getBar(){
return this.bar;
}
public List<String> getGrr(){
return this.grr;
}
}
Run Code Online (Sandbox Code Playgroud)
这是检查它的实例的主要方法:
public static void main(final String[] args) throws Exception{
final Thingy thingy = new Thingy();
final BeanWrapper wrapper = new BeanWrapperImpl(thingy);
for(final PropertyDescriptor descriptor : wrapper.getPropertyDescriptors()){
System.out.println(descriptor.getName() + ":"
+ descriptor.getReadMethod().invoke(thingy));
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
bar:123
class:class com.mypackage.Thingy
foo:hey
grr:[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
阅读本文以供参考:
| 归档时间: |
|
| 查看次数: |
8104 次 |
| 最近记录: |