2

当我使用 Zend Framework 时,我曾经有.ini配置文件,我可以在其中执行以下操作:

[production]
setting1 = abc
setting2 = def

[development : production]
setting1 = ghi

当我打开development应用程序配置文件时,setting1它的值为ghi,而在生产模式下它是abc。是否可以在 Apache Commons 配置中定义此类继承设置?

我知道我可以定义多个配置文件并从中创建一个CompositeConfiuration,但是如何告诉应用程序它应该只包含不同模式下的特定文件?我也不知道如何设置这些应用程序配置文件,但另一个名为的设置PROFILE应该为我工作。

如果可能,我不想将设置拆分为多个文件。

我找不到有关如何在 Java 中完成此操作的任何信息。

4

1 回答 1

0

Apache Configuration reads the config options from a file into a memory data structure. Besides variable expansion, there is no further post processing by default. So your options are:

  • Put all options in a default config and then use individual "delta" configs that overwrite the defaults. Merge these individual files with CompositeConfiuration.

    This design follows Java's inheritance model: Base type which you extend to overwrite some value and add new ones.

  • Create a post processor that takes the huge config and turns that into a new config with your preferred merging rules applied.

  • Create a helper object to look up values by key in the config. That would allow you do the merging at lookup time.

于 2013-02-15T12:42:21.637 回答