简体   繁体   English

有效地使用大量的吸气剂和吸气剂填充POJO

[英]Efficiently populating a POJO with a large amount of getters and setters

I have a POJO which represents all the properties of my application. 我有一个POJO,它代表应用程序的所有属性。 It contains a huge amount of Strings ints and booleans (class variables, and their getters/setters). 它包含大量的String int和boolean(类变量,以及它们的getter / setter)。

The problem is that every time a new property gets added, i have to add the variable, the getter, setter, the code that sets it from loading the property file and the code that uses the getter. 问题在于,每次添加新属性时,我都必须添加变量,getter,setter,从加载属性文件进行设置的代码以及使用getter的代码。

My Idea was to refactor this into a getString(String stringToGet) , getInt and getBoolean method that pulls from 3 hashmaps. 我的想法是将其重构为从3个哈希图中提取的getString(String stringToGet)getIntgetBoolean方法。 The problem with doing it this way is that I can no longer use EL to get properties eg. 这样做的问题是我不能再使用EL来获取属性,例如。 ${Properties.telephoneNumber}

Anyone have any other ideas? 还有其他想法吗?

If you have a class that you are absolutely sure you'll have get/set for every variable, I would just make the variables public. 如果您有一个绝对可以确定每个变量都可以获取/设置的类,那么我只是将变量公开。 Usually the reason for not doing this is if it's possible you'll want to change the implementation or change variable names or something like that. 通常不这样做的原因是,是否有可能您想要更改实现或更改变量名称或类似的名称。 If you KNOW it's not going to happen, I would go ahead and change the access to public. 如果您知道这不会发生,我将继续进行,并更改对公众的访问权限。

Also, if you're using Eclipse (probably other IDEs, but I'm not familiar with them), it can automagically create get/set methods for you, so that shouldn't be a hassle. 另外,如果您使用的是Eclipse(可能是其他IDE,但我对它们不熟悉),它可以自动为您创建get / set方法,因此这并不麻烦。

I wouldn't use the solution you suggested. 我不会使用您建议的解决方案。 It would eliminate the convenience of auto-completion, which is especially needed when there are a lot of methods/variables. 这将消除自动完成的便利性,这在存在许多方法/变量时尤其需要。

从JSF 2.0开始,您可以在EL中使用参数调用方法:

#{properties.getString('telephoneNumber')}

使用您的IDE生成所有这些setter和getter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM