Where should the configuration be located?

I have an application structured as follows:

  • tao
  • domain
  • Main
  • services
  • Utils

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

By reflection, I would place it in utilities, but utility classes have static methods and have no state, while this class uses an instance of Apache Commons XMLConfiguration. Should I just adapt the methods so that this instance is limited to the scope of the methods in this class?

+4
source share
5 answers

I assume the elements are packages, so I would go with the main package.

  • dao
  • domain
  • main contains the application and its configuration readers
    • configuration
    • Journal
  • services
  • Utils

Why? Application configuration, whether in XML or not, and whether it is based on an application structure such as Spring or not, is part of its core functions. Downloading the application is the main task of the application. All the functionality of the business, all the brilliant functions that it provides, are implemented at the domain and service level.

You are right, utils are static or similar tools. Since the configuration of the application is very important, I would not declare it a utility. A utility is something that can be easily replaced with another utility of the same type (for example, StringUtil vs. StringUtils vs. IOUtils , etc. They all have very similar functionality)

+3
source

It depends on the build system and the type of application you are using, for example. maven will suggest placing configuration files in src / main / resources

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

In accordance with your project structure, I would suggest introducing the configuration or folder resources, since almost everyone expected them there.

+2
source

If you are working with Spring, see Configuration Repositories . You can use a simple java properties file for your configuration properties and put it on your class path (or anywhere else). Alsou, you could create your own implementation to use a different form of storing your configuration values ​​(XML, Database, etc.).

+2
source

Since configuration is a cross-cutting aspect, it is not tied exclusively to one of these layers. Put the configuration files (XML or properties) in the classpath and use it with Spring to customize the beans.

For configuration data based on properties, PropertyPlaceholderConfigurer is a good solution.

+2
source

I urge you to take a look at Spring. It may seem redundant to you, but you will love it.

+1
source

Source: https://habr.com/ru/post/1309899/


All Articles