简体   繁体   English

配置应该放在哪里?

[英]Where should configuration be placed?

I have an application structured as follows: 我的申请结构如下:

  • dao
  • domain
  • main 主要
  • services 服务
  • utils utils的

I've made a class that reads the application configuration from an XML file. 我创建了一个从XML文件中读取应用程序配置的类。 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. 通过反射,我将它放在实用程序中但实用程序类具有静态方法并且是无状态的,而此类使用Apache Commons XMLConfiguration的实例。 Should I just adapt the methods so this instance is limited to the scopes of the methods in this class? 我应该只调整方法,以便此实例仅限于此类中方法的范围?

I assume the items are packages, so I'd go with the main package. 我假设这些物品都是包裹,所以我会选择包装。

  • dao
  • domain
  • main contains the application and its configuration readers main 包含应用程序及其配置读取器
    • config 配置
    • log 日志
  • services 服务
  • utils 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. 应用程序的配置,无论是否为XML,以及它是否基于Spring等应用程序框架,都是其主要功能的一部分。 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. 你是对的,utils是关于静态或类似的工具。 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 (eg StringUtil vs. StringUtils vs. IOUtils etc. they all have very similar functionality) 实用程序可以很容易地被另一个相同类型的实用程序替换(例如, StringUtilStringUtils对比IOUtils等,它们都具有非常相似的功能)

This depends on the build system and application type you use ig maven would suggest to place configfiles in src/main/resources 这取决于你使用的构建系统和应用程序类型ig maven建议将配置文件放在src / main / resources中

In WAR file you could place them in WEB-INF or WEB-INF/config 在WAR文件中,您可以将它们放在WEB-INF或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. 根据你的项目结构,我建议引入一个文件夹配置或资源,因为几乎所有人都希望它们存在。

If you are working with Spring, take a look at Configuration Placeholders . 如果您正在使用Spring,请查看配置占位符 You can use a simple java properties file for your configuration properties and place it on your class path (or any other location). 您可以为配置属性使用简单的java属性文件,并将其放在类路径(或任何其他位置)上。 Alsou you could create your own implementation to use a different form of keeping your configuration values (XML, Database etc.) 你可以创建自己的实现来使用不同的形式保持配置值(XML,数据库等)

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. 将配置文件(XML或属性)放入类路径中,并通过Spring使用它来配置bean。

For properties based configuration data the PropertyPlaceholderConfigurer is a good solution. 对于基于属性的配置数据, PropertyPlaceholderConfigurer是一个很好的解决方案。

I urge you to have a look at Spring. 我劝你看看Spring。 Might seem like overkill for you in the first place, but you wil love it. 一开始你可能看起来有点矫枉过正,但你会爱上它。

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

相关问题 BufferedInputStream-BufferedOutputStream应该放在哪里 - Where should be placed the BufferedInputStream - BufferedOutputStream Selenium TestnNg - AssertAll() 应该放在测试中的哪个位置 - Selenium TestnNg - where should AssertAll() be placed in the test 在Maven项目中应将引用的库放置在何处? - Where should referenced libraries be placed in a maven project? JBoss配置:JMS Bridge配置应该在哪里? - JBoss configuration : Where should JMS Bridge configuration be? 没有工厂的情况下应将策略模式置于何处? - Where should be placed switch in strategy pattern without factory? 三层系统中应将业务逻辑放在何处? - Where should be placed business logic in the three-layer system? MVC结构。 语言文件夹应该放在哪里? - MVC Structure. Where should be placed language folder? JSF 2.2资源应该放在哪里:WEB-INF或root - Where should JSF 2.2 resources be placed: WEB-INF or root 在Java中,应该在包/源层次结构中的何处放置资源? - In Java, where in the package/source hierarchy should resources be placed? 在LINUX下应该放置应用程序初始化和支持文件? - Under LINUX where should application initialization and support files be placed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM