配置应该放在哪里?

发布于 2024-09-01 12:43:32 字数 265 浏览 3 评论 0原文

我的应用程序结构如下:

  • dao
  • main
  • services
  • utils

我创建了一个从 XML 文件读取应用程序配置的类。问题是应该放在哪里?

我本能地把它放在实用程序中,但实用程序类具有静态方法并且是无状态的,而此类使用 Apache Commons XMLConfiguration 的实例。我是否应该调整这些方法,以便将该实例限制在此类中的方法的范围内?

I have an application structured as follows:

  • dao
  • domain
  • main
  • services
  • utils

I've made a class that reads the application configuration from an XML file. The question is where should it be placed?

By reflex, I'd have placed it in utilities but utility classes have static methods and are stateless whereas this class uses an instance of Apache Commons XMLConfiguration. Should I just adapt the methods so this instance is limited to the scopes of the methods in this class?

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

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

发布评论

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

评论(5

弥繁 2024-09-08 12:43:32

我假设这些项目是包,所以我会选择ma​​in包。

  • dao
  • ma​​in 包含应用程序及其配置读取器
    • 配置
    • 记录
  • services
  • utils

为什么? 应用程序的配置,无论是否采用 XML 格式,也无论是否基于 Spring 等应用程序框架,都是其主要部分的一部分。功能。启动应用程序是应用程序的主要职责。它提供的所有业务功能、所有闪亮特性都在域和服务层中实现。

你是对的,utils 都是关于静态或类似工具的。由于应用程序的配置非常重要,因此我不会将其声明为实用程序。实用程序可以很容易地被相同类型的另一个实用程序替换(例如,StringUtilStringUtilsIOUtils等。它们都有非常相似的功能)

I assume the items are packages, so I'd go with the main package.

  • dao
  • domain
  • main contains the application and its configuration readers
    • config
    • log
  • services
  • utils

Why? The configuration of an application, whether it be in XML or not and whether it is based on an application framework such as Spring or not, is part of its main functionality. Booting up an application is the main responsibility of the application. All the business functionality, all the shiny features it provides are implemented in the domain and service layers.

You're right, utils is all about static or similar tools. As the configuration of an application is very important, I wouldn't declare it a utility. A utility is something which can be easily replaced by another utility of same type (e.g. StringUtil vs. StringUtils vs. IOUtils etc. they all have very similar functionality)

枯叶蝶 2024-09-08 12:43:32

这取决于您使用的构建系统和应用程序类型 ig maven 建议将配置文件放置在 src/main/resources

在 WAR 文件中,您可以将它们放在 WEB-INF 或 WEB-INF/config 中

根据您的项目结构,我建议引入文件夹 config 或 resources ,因为几乎每个人都会期待他们在那里。

This depends on the build system and application type you use i.g. maven would suggest to place configfiles in src/main/resources

In WAR file you could place them in WEB-INF or WEB-INF/config

According to your project structure I would suggest to introduce a folder config or resources, since almost everybody would expect them there.

姜生凉生 2024-09-08 12:43:32

如果您正在使用 Spring,请查看 配置占位符。您可以使用一个简单的 java 属性文件作为配置属性,并将其放置在类路径(或任何其他位置)上。您还可以创建自己的实现,以使用不同的形式来保存您的配置值(XML、数据库等)。

If you are working with Spring, take a look at Configuration Placeholders. You can use a simple java properties file for your configuration properties and place it on your class path (or any other location). Alsou you could create your own implementation to use a different form of keeping your configuration values (XML, Database etc.)

两仪 2024-09-08 12:43:32

由于配置是一个横切方面,因此它并不专门映射到这些层之一。将配置文件(XML 或属性)放入类路径中,并通过 Spring 使用它来配置您的 bean。

对于基于属性的配置数据 PropertyPlaceholderConfigurer 是一个很好的解决方案。

As configuration is a cross-cutting aspect it doesn't map exclusively to one of these layers. Place the configuration files (XML or properties) into the classpath and use it via Spring to configure your beans.

For properties based configuration data the PropertyPlaceholderConfigurer is a good solution.

罪歌 2024-09-08 12:43:32

我强烈建议你看看 Spring。一开始对你来说可能有点大材小用,但你会喜欢它的。

I urge you to have a look at Spring. Might seem like overkill for you in the first place, but you wil love it.

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