需要帮助bean - 在运行时添加属性??? [英] Need help with beans - adding properties at run time???

查看:59
本文介绍了需要帮助bean - 在运行时添加属性???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我是Java bean的新手。昨晚拿了一本书,

开始读它们。我正在构建一个允许

用户定义具有动态属性的对象的应用程序。类似于

java哈希表的东西。可以在运行时添加新属性并删除它们




我有一个非常喜欢的第三方房产检查员可供我使用

我非常想重复使用。这个检查器使用bean API。

我希望我可以为我的

动态对象构建一组Java bean包装器,以便在这个检查器中可以检查它们。 br />

好​​吧,从我到目前为止所读到的内容来看,我必须在我的包装类中为我想要的每个属性定义一个getXXX和

setXXX方法

检查。我无法定义这些方法,因为我不知道用户可能添加到原始对象的属性是什么。有什么方法可以解决这个难题吗?


任何帮助都表示赞赏。


Nik

-

================================== =====

Nikita A. Visnevski

自适应系统实验室

CRL,麦克马斯特大学

电话: (905)525-9140 x 27282

网址: http:// soma.crl.mcmaster.ca

=============================== ========

Hi everyone,

I am rather new to Java beans. Just picked up a book last night and
started reading about them. I am building an application that allows a
user to define objects with dynamic properties. Something similar to a
java hash table. One can add new properties at runtime and remove them
as well.

I have a really fancy third party property inspector available to me
that I would very much like to reuse. This inspector uses beans API.
My hope was that I could build a set of Java bean wrappers for my
dynamic objects such that they could be inspectable in this inspector.

Well, from what I read so far it seams that I have to have a getXXX and
setXXX method defined in my wrapper class for every property I want to
inspect. I can not define these methods since I have no idea what
properties the user might add to my original object. Is there any way
to work around this difficulty?

Any help is appreciated.

Nik
--
=======================================
Nikita A. Visnevski
Adaptive Systems Laboratory
CRL, McMaster University
Phone : (905) 525-9140 x 27282
Web : http://soma.crl.mcmaster.ca
=======================================

推荐答案

2004年4月19日星期一07:53:53 -0400,Nikita A. Visnevski

< vi **** @ soma.crl.mcmaster.ca>写道:
On Mon, 19 Apr 2004 07:53:53 -0400, "Nikita A. Visnevski"
<vi****@soma.crl.mcmaster.ca> wrote:
大家好,

我对Java bean很新。昨晚刚拿起一本书,
开始阅读它们。我正在构建一个允许
用户定义具有动态属性的对象的应用程序。类似于java哈希表的东西。可以在运行时添加新属性并删除它们。

我有一个非常喜欢的第三方财产检查员可以使用
我非常想重复使用。这个检查员使用bean API。
我希望我可以为我的
动态对象构建一组Java bean包装器,以便在这个检查器中可以检查它们。

从我到目前为止读到的内容来看,我必须在我的包装类中为我想要检查的每个属性定义一个getXXX和
setXXX方法。我无法定义这些方法,因为我不知道用户可能添加到原始对象的属性。有什么方法可以解决这个难题吗?
Hi everyone,

I am rather new to Java beans. Just picked up a book last night and
started reading about them. I am building an application that allows a
user to define objects with dynamic properties. Something similar to a
java hash table. One can add new properties at runtime and remove them
as well.

I have a really fancy third party property inspector available to me
that I would very much like to reuse. This inspector uses beans API.
My hope was that I could build a set of Java bean wrappers for my
dynamic objects such that they could be inspectable in this inspector.

Well, from what I read so far it seams that I have to have a getXXX and
setXXX method defined in my wrapper class for every property I want to
inspect. I can not define these methods since I have no idea what
properties the user might add to my original object. Is there any way
to work around this difficulty?




我唯一能想到的就是使用java反射和代理

方法......虽然这有点复杂。


我个人认为,我们可以使用散列表或某物来映射物业到

a值。


-

现在有更多的牛铃



The only thing I can think of is using java reflection and proxy
methods... That would be a bit complicated though.

I, personally, would us a hashtable or something to map a property to
a value.

--
now with more cowbell


Nikita A. Visnevski写道:
Nikita A. Visnevski wrote:
好吧,从我到目前为止所读到的内容来看,我必须在我的包装类中为我想要检查的每个属性定义一个getXXX和
setXXX方法。我无法定义这些方法,因为我不知道用户可能添加到原始对象的属性。有什么方法可以解决这个难题吗?
Well, from what I read so far it seams that I have to have a getXXX and
setXXX method defined in my wrapper class for every property I want to
inspect. I can not define these methods since I have no idea what
properties the user might add to my original object. Is there any way
to work around this difficulty?




最后,你必须提供一对accessor / mutator方法

每个属性。你不必把它们叫做setXXX和getXXX就是你不想要b / b
如果它们有另外一种指定这些

方法的方法不同的名字),但你确实需要他们来支付



如果你非常坚定,可能会为你

围绕原始表格生成一个JavaBeans包装类,类似于
对象。你需要做一些Java代码生成,而BCEL将是非常有帮助的。基本上,你想要生成一个类,其对象包含对原始对象的引用,并且
公开一组看起来像JavaBeans方法的方法。这些方法

会将操作传递给原始对象。你需要

来随时生成bean类和对象

检查数据就像它是一个JavaBean一样。


-
www.designacourse.com

培训任何人的最简单方法......无处不在。


Chris Smith - 首席软件开发人员/技术培训师

MindIQ Corporation



In the end, you''ll have to provide a pair of accessor/mutator methods
for each property. You don''t have to call them setXXX and getXXX is you
don''t want to (see BeanInfo for an alternate way to specify these
methods if they have different names), but you do need for them to
exist.

One way around this, if you''re very determined, may be for you to
generate a JavaBeans wrapper class around your original table-like
object. You''d need to do some Java code generation, and BCEL would be
very helpful in accomplishing this. Essentially, you''d want to generate
a class whose objects hold a reference to your original object and
expose a set of methods that look like JavaBeans methods. These methods
would pass their operations through to your original object. You''d need
to generate the bean class and object on the fly whenever you want to
inspect the data as if it were a JavaBean.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation





克里斯,布莱斯,


。代理对象

的想法似乎非常有吸引力,但是当我深入挖掘时,我意识到所有内容都可以归结为使用accessor / mutator方法的界面。 />
必须提供。嗯,这正是我没有的界面。

所以,我想,我的选择是:

1.生成接口或甚至整个BeanInfo类

苍蝇。

2.完全放弃这个想法并编写我自己的检查员来检查类似Java哈希表的对象。


1和2都不是很令人兴奋:(


这是一个问题(因为我之前从未做过类似的事情)。

假设我可以动态生成一个接口到java文件中。我需要编译它。我假设我已经有一个相同的类

名称加载到jvm。我可以使用新类卸载/重新加载/更新它吗?或者我应该创建并加载一个全新的类,并使用新名称和

忘记了关于旧的?


提前致谢。


Nik


Chris Smith写道:


Chris, Bryce,

thanks for your replies. I just arrived at the same conclusion
regarding the on-the-fly code generation. The idea of the Proxy object
seemed very attractive, but when I dug deeper I realized that everything
boils down to the interface with accessor/mutator methods that I would
have to provide. Well, it is exactly the interface that I do not have.
So, I suppose, my choices are:
1. Generate interfaces or perhaps even the entire BeanInfo classes on
the fly.
2. Abandon this idea altogether and write my own inspector for
inspecting Java hashtable-like objects.

Neither 1, nor 2 sound very exciting :(

Here is a question (since I have never done something like that before).
Suppose I can generate an interface on the fly into a java file. I
would have to compile it. Suppose I already have a class with the same
name loaded into jvm. Can I unload/reload/update it with the new class,
or should I just create and load an entirely new class with new name and
forget about the old one?

Thanks in advance.

Nik

Chris Smith wrote:
最后,你会有e为每个属性提供一对accessor / mutator方法。你不必把它们称为setXXX和getXXX就是你不想要的(如果它们有不同的名称,请参阅BeanInfo以指定这些
方法的替代方法),但你确实需要对于他们来说存在。

如果您非常坚定,可以通过这种方式为您生成一个围绕原始表格的JavaBeans包装类对象。你需要做一些Java代码生成,BCEL对完成这个有很大的帮助。从本质上讲,你想要生成一个类,其对象包含对原始对象的引用,并公开一组看起来像JavaBeans方法的方法。这些方法将把操作传递给原始对象。当你想要检查数据时,你需要
生成bean类和对象,就像它是一个JavaBean一样。
In the end, you''ll have to provide a pair of accessor/mutator methods
for each property. You don''t have to call them setXXX and getXXX is you
don''t want to (see BeanInfo for an alternate way to specify these
methods if they have different names), but you do need for them to
exist.

One way around this, if you''re very determined, may be for you to
generate a JavaBeans wrapper class around your original table-like
object. You''d need to do some Java code generation, and BCEL would be
very helpful in accomplishing this. Essentially, you''d want to generate
a class whose objects hold a reference to your original object and
expose a set of methods that look like JavaBeans methods. These methods
would pass their operations through to your original object. You''d need
to generate the bean class and object on the fly whenever you want to
inspect the data as if it were a JavaBean.




-

===================================== ==

Nikita A. Visnevski

自适应系统实验室

CRL,麦克马斯特大学

电话:(905) 525-9140 x 27282

网址: http://soma.crl .mcmaster.ca

================================== =====



--
=======================================
Nikita A. Visnevski
Adaptive Systems Laboratory
CRL, McMaster University
Phone : (905) 525-9140 x 27282
Web : http://soma.crl.mcmaster.ca
=======================================


这篇关于需要帮助bean - 在运行时添加属性???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆