afunms

My Software,My Dream—Forge a more perfect NMS product.

Dynamic Bean

     学习Struts的过程中认识了DynaBean,一时不理解何为动态Bean。看了以下的例子,
就很容易明白了。 

 

import java.util.Date;

import org.apache.commons.beanutils.BasicDynaClass;

import org.apache.commons.beanutils.DynaBean;

import org.apache.commons.beanutils.DynaClass;

import org.apache.commons.beanutils.DynaProperty;

public class TestService {

public static void main(String[] hey) throws Exception{

DynaProperty[] pro = new DynaProperty[]{//准备一个属性,动态定义类依靠这些属性来决定类的构成。

new DynaProperty("title", String.class),

new DynaProperty("content", String.class),

new DynaProperty("createdTime", Date.class),

new DynaProperty("id", Integer.class),

};

DynaClass articleClass = new BasicDynaClass("Article",null,pro);//定义了一个类

DynaBean article = articleClass.newInstance();//声明了一个ArticleClass的对象

article.set("title","this is a test");//对该对象进行操作

article.set("content","oh my god");

article.set("createdTime",new Date());

article.set("id",new Integer(1));

System.out.println(article.get("title"));

System.out.println(article.get("content"));

System.out.println(article.get("createdTime"));

System.out.println(article.get("id"));

}

}

posted on 2007-10-09 20:28 afunms 阅读(702) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

My Links

News

留言簿(18)

随笔档案

相册

搜索

最新评论

阅读排行榜