本帖最后由 toShareBeauty 于 2013-7-22 15:10 编辑
- package cn.itcast.reflect;
- import org.apache.commons.beanutils.BeanUtils;
- /**
- * @class: IntrospectorDemo
- * @package: cn.itcast.reflect
- * @description: TODO
- * @author: vivianZhao
- * @date: 2013-7-22 上午9:15:36
- * @version: 1.0
- */
- public class IntrospectorDemo {
- /**
- * @method: main
- * @description: TODO
- * @param args
- * @return: void
- * @author: vivianZhao
- * @date: 2013-7-22 上午9:15:36
- * @version: 1.0
- */
- public static void main(String[] args) throws Exception {
- Point point = new Point(2, 5);
- String propertieName = "x";
- BeanUtils.setProperty(point, propertieName, "8");
- System.out.println(point.getX());
- System.out.println(BeanUtils.getProperty(point, propertieName));
- System.out.println(BeanUtils.getProperty(point, propertieName).getClass()
- .getName());
- BeanUtils.setProperty(point, propertieName, 8);
- System.out.println(BeanUtils.getProperty(point, propertieName).getClass()
- .getName());
- // 我们看到虽然属性x的类型是Integer,但是我们设置的时候无论是Integer还是String,BeanUtils的内部
- // 都是当成String来处理的。
- }
- }
- class Point {
- private Integer x;
- private Integer y;
- public Point() {
- }
- public Point(Integer x, Integer y) {
- super();
- this.x = x;
- this.y = y;
- }
- public Integer getX() {
- return x;
- }
- public void setX(Integer x) {
- this.x = x;
- }
- public Integer getY() {
- return y;
- }
- public void setY(Integer y) {
- this.y = y;
- }
- }
复制代码 上面是代码,很简单的 BeanUtils 包的应用,但是却报一下异常:
这是我添加的工具包,新鲜出炉的,在 apache 网站下载的:
本人查找了很久,认为我的 setter 和 getter 都有,请问一下大侠,为什么上面报这个 setter 找不到,真的莫名其妙哦?
|
|