有没有办法在不在代码中硬编码属性键的情况下使用属性文件?

huangapple go评论64阅读模式
英文:

Are there way to use properties files without hard coding property keys in code?

问题

我尝试使用属性文件,但在我的Java文件中键是硬编码的。

我曾尝试编写不同的属性类,但在getter方法中我仍然需要使用属性的键,而键仍然是硬编码的。

英文:

I have tried using property files, but in the keys are hardcoded in my java file.

I had tried to write different property class but in getter method i need to use key of property and still keys are harcoded.

答案1

得分: 3

思考一下:你在这里有两个方面。

一方面,你有你的代码,它应该“处理”某些元素。另一方面,你有包含“附加信息”的属性文件。

现在:需要一种方式来“连接”这两个方面。你看,你的代码的目的是“我想要做X,为此我需要使用Y”。从概念上讲,你的代码“知道”某些东西。

因此,你需要一种方式在另一方面也表示这些东西。直接的方法是,代码知道在属性文件中使用的。为了隔离这种知识,你可以遵循最佳实践,比如:将键作为代码中的常量,并确保它们只在一个类中定义(最理想情况下:只使用一次)。

除此之外,当然你可以进行“元编程”。意思是:你可以编写一个GUI程序,给一个标签元素命名为label-foo...然后你可以添加一些魔法,查看属性文件,并查看该文件中的所有键。然后“自动”确定属性键label-foo-text应该用于GUI标签元素label-foo

所以,答案是:不完全是。当你的信息分布在多个地方时,你需要一种机制来将语义上属于一起的内容联系起来。

英文:

Think about it: you have two sides here.

On the one hand side, you have your code that is supposed to "deal" with certain elements. On the other side, you have your property files, that contain "additional information".

Now: there needs to be a way to "connect" these two sides. You see, the point of your code is "I want to do X, and for that I need to work with Y". Conceptually, your code "knows" certain things.

Therefore you need a way to denote those things also on the other side. The straight forward way is that the code knows the keys that are used in the property file. In order to isolate that knowledge, you can follow best practices, such as: making the keys a constant in code, and making sure that they are only defined (and ideally: used) only in one class.

Beyond that, of course you can do "meta programming". Meaning: you could program a GUI, and name a label element label-foo ... and then you add some magic that looks into property files, and looks at all the keys in that file. And then "automatically" figures that the property key label-foo-text should be used for the GUI label element label-foo.

So, the answer is: not really. When your information sits in multiple places, then you need some sort of mechanism to bring together what semantically belongs together.

答案2

得分: 2

作为一般的编程规则,如果你不希望A知道B,你可以始终引入C来管理它们之间的关系(一层间接)。所以,与其是A --> B,你可以有A <-- C --> B。你无法避免某个组件必须了解关系的参数这一事实,但你可以控制这种知识存在的位置。在企业应用程序中,管理属性通常是依赖注入容器的责任。

英文:

As a general programming rule, if you don't want A to know about B, you can always introduce C to manage the relationship between them (a layer of indirection).

So rather than A --&gt; B you have A &lt;-- C --&gt; B.

You can't avoid the fact that some component must understand the parameters of the relationship, but you can control where that knowledge lives. In enterprise applications, managing properties is often the responsibility of a dependency injection container.

huangapple
  • 本文由 发表于 2020年8月7日 20:24:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63301840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定