本帖最后由 桉树 于 2013-6-5 09:11 编辑
先上代码:- <div>public class IntroSepctorDemo {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>public static void main(String[] args) throws Exception {</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>用内省方式获取JavaBean中的属性。</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Person person = new Person("李四","男",14) ; </div><div>
- </div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>获取person 对象中name 属性的值</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println( getProperty(person,"name"));</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>设置person 对象中name 属性的值</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>setProperty(person, "name", "张三") ;</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(person.getName());</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>通过BeanUtils 工具包</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>BeanUtils.setProperty(person, "name", "张三") ;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(BeanUtils.getProperty(person,"name"));</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>通过BeanUtils 设置brithday 属性。birthday 是一个复合属性。</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>BeanUtils.setProperty(person, "birthday.time", "111") ; </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(BeanUtils.getProperty(person, "birthday.time"));</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>private static Object getProperty(Object obj , String propertyName) throws Exception{</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass()) ;</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>获取Obj中的get方法</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Method methodGet = pd.getReadMethod() ; </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return methodGet.invoke(obj) ; </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>private static void setProperty(Object obj , String propertyName,Object value) throws Exception {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass()) ;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Method methodSet = pd.getWriteMethod() ; </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>methodSet.invoke(obj, value) ; </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>} </div><div>}</div>
复制代码 BeanUtils.setProperty(person, "birthday.time", "111") ;
System.out.println(BeanUtils.getProperty(person, "birthday.time"));
为什么 这里运行会报错?,
还有就是JavaBean 中的既然有get和set方法,为什么我们还要通过内省的方式来get和set JavaBean的对象呢?{:soso_e176:}
|