访问应用程序内部设置的首选方式是什么?

发布于 2024-10-20 03:38:33 字数 309 浏览 8 评论 0原文

目前,我有一个单例(服务定位器),我可以通过诸如 Services.getSettings().get("my.setting"); 之类的方式获取设置。像这样的调用广泛地分布在代码中任何可能需要设置值的地方。它工作得很好,但我忍不住觉得这不是执行此操作的“正确”方法(最令人烦恼的一点是所有内容都耦合到这个全局对象)。在整个应用程序中访问设置的更好(从“最佳实践”的意义上来说)有哪些方法?如果有帮助的话,我不是在谈论网络应用程序。我说的是独立应用程序,无论是服务器端的桌面。哦,它是用 Java 编写的,因为我知道你会问……但这并不重要,因为我正在寻找概念而不是具体的实现。

Currently I have a singleton (service locator) thing going on where I get my settings via something like Services.getSettings().get("my.setting");. Calls like these are sprinkled liberally around the code in whatever place the values of settings may be needed. It works great, but I can't help but get the feeling that it's not the "correct" way of doing this (most nagging point being the fact that everything is coupled to this global object). What would be some better (in the sense of "best practices") ways of accessing settings throughout an application? In case it helps, I'm not talking about web applications. I'm talking about standalone apps, be they desktop of server side. Ohh, and it's in Java, since I know you're going to ask... It shouldn't matter, though, because I'm looking for concepts rather than specific implementations.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

枕梦 2024-10-27 03:38:33

对于 java 桌面应用程序
使用

或使用以下文件保存某些平台独立路径的设置 (${user.home})

  • 属性文件

  • xml 文件

for java Desktop apps
Use

Or use following file to hold settings at some platform independent path (${user.home})

  • Properties file

  • xml file

飞烟轻若梦 2024-10-27 03:38:33

过去,我在 XML 文件中将我的(java)应用程序中的各种类的设置外部化,并且我使用了“控制反转(IoC)容器”(Spring Framework) 来注入它们。我发现这种方法非常有用,因为它也有助于外部化对象依赖关系。

In the past I have externalized settings for various classes in my (java) application in an XML file, and I used an "Inversion of Control (IoC) Container" (Spring Framework) to inject them. I find this approach very useful, because it helps in externalizing object dependencies too.

迷你仙 2024-10-27 03:38:33

如果使用依赖注入,您可以将值注入目标对象中。否则这是一个很好的方法。

If using dependency injection, you can inject the values in the target objects. Otherwise this is a fine way to do it.

够钟 2024-10-27 03:38:33

如果只要应用程序运行,my.setting 的值就保持不变,我希望将该值存储在常量中。

private static final String MY_SETTING = 
    Services.getSettings().get("my.setting");

并在该类中的任何地方使用 MY_SETTING 。您可能需要相应地更改访问修饰符。

If the value of my.setting is going to remain constant as long as the application is running, I would want to store the value in a constant.

private static final String MY_SETTING = 
    Services.getSettings().get("my.setting");

And use MY_SETTING everywhere in that class. You might want to change the access modifier accordingly.

太傻旳人生 2024-10-27 03:38:33

在 Java 中,有 首选项 API。这本质上是设置服务的标准化版本,它从各个合理的位置加载配置。这可能是对你目前做事方式的改进,但它并没有真正改变架构,这似乎是你不满意的地方。

从架构的角度来看,您可能需要的是依赖注入;关于这个话题已经写了很多文章,我似乎无法描述它。这个想法是,一些外部力量承担了加载设置并将其传递给您的类的工作,而不是您的代码必须伸出援手并将它们拉入。依赖注入通常与 Web 应用程序相关联,但完全可以使用它在独立应用程序中。我认为使用 PicoContainer 甚至非常简单;我对春天一无所知。

In Java, there's the Preferences API. That's essentially a standardized version of your settings service, which loads the configuration from various sensible places. That might be an improvement over how you do things at the moment, but it doesn't really change the architecture, which seems to be what you're dissatisfied with.

In architectural terms, what you might be after is dependency injection; that's a topic about which so much has been written that it seems futile for me to describe it. The idea would be that some external force takes on the job of loading settings and handing them to your classes, rather than your code having to reach out and pull them in. Dependency injection is usually associated with web apps, but it's perfectly possible to use it in standalone apps. I think it's even pretty simple with PicoContainer; I have no idea about Spring.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文