在java中处理特定于环境的配置的最佳方法是什么?

时间:2022-09-11 15:03:14

I have an application running in tomcat that has a bunch of configuration files that are different for each environment it runs in (dev, testing, and production). But not every line in a config file will be different between environments so there's invariably duplicated information that doesn't get updated if something changes.

我有一个在tomcat中运行的应用程序,它有一堆配置文件,它们运行的​​每个环境都不同(开发,测试和生产)。但是,并非配置文件中的每一行在不同环境之间都会有所不同,因此,如果发生变化,则总会出现重复的信息。

Is there a good framework/library that collapses the separate files into one with environment specific blocks? Or some other way of dealing with this?

是否有一个好的框架/库将单独的文件折叠成一个环境特定的块?或者其他一些处理方式?

5 个解决方案

#1


2  

  1. Assign reasonable default values for all properties in the properties files distributed within your .war file.
  2. 为.war文件中分发的属性文件中的所有属性分配合理的默认值。

  3. Assign environment-specific values for the appropriate properties in webapp context (e.g. conf/server.xml or conf/Catalina/localhost/yourapp.xml)
  4. 为webapp上下文中的相应属性分配特定于环境的值(例如conf / server.xml或conf / Catalina / localhost / yourapp.xml)

  5. Have your application check the context first (for the environment-specific values), and fall back on the default values in the app's properties values if no override is found.
  6. 让应用程序首先检查上下文(针对特定于环境的值),如果未找到覆盖,则返回应用程序属性值中的默认值。

#2


1  

A Properties file is what I've always used. It's editable by hand as well as in in your software and the Properties object can read itself in and write itself out to the filesystem. Here's the javadoc page:

属性文件是我一直使用的。它可以手动编辑,也可以在软件中编辑,并且Properties对象可以自行读取并将自身写入文件系统。这是javadoc页面:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html

#3


1  

If you use maven, you can use it's resource filtering abilities, along with profiles to generate a properties file for each environment you're deploying into.

如果您使用maven,则可以使用它的资源过滤功能以及配置文件为您要部署的每个环境生成属性文件。

As an added bonus, maven can also deploy your web app for you.

作为额外的奖励,maven还可以为您部署您的Web应用程序。

#4


0  

The duplication is not really a problem, having a central config file the the other files 'extend' is likely to casue more of a headache in the long term.

重复并不是一个真正的问题,拥有一个*配置文件,其他文件的“扩展”可能会长期让人头疼。

My advice is to use ant to load (copy and move) the appropriate file(s) into place and then launch the app (bundle into war?). Just have a different task for each environment. So you will have three config files (dev.config, test.config and production.config) which will be moved and overwrite the config in the /WEB-INF folder depending on the task that you are running.

我的建议是使用ant将相应的文件加载(复制和移动)到位,然后启动应用程序(捆绑到战争?)。只为每个环境执行不同的任务。因此,您将有三个配置文件(dev.config,test.config和production.config)将被移动并覆盖/ WEB-INF文件夹中的配置,具体取决于您正在运行的任务。

#5


0  

I would suggest to have a separate config file for environment parameters alone if you want to avoid cluttering. Then you will have one more config file to manage. This is a trade off between number of config files vs complexity of each config file.

如果你想避免混乱,我建议单独为环境参数配置一个配置文件。然后你将有另外一个配置文件来管理。这是配置文件数量与每个配置文件的复杂性之间的权衡。

#1


2  

  1. Assign reasonable default values for all properties in the properties files distributed within your .war file.
  2. 为.war文件中分发的属性文件中的所有属性分配合理的默认值。

  3. Assign environment-specific values for the appropriate properties in webapp context (e.g. conf/server.xml or conf/Catalina/localhost/yourapp.xml)
  4. 为webapp上下文中的相应属性分配特定于环境的值(例如conf / server.xml或conf / Catalina / localhost / yourapp.xml)

  5. Have your application check the context first (for the environment-specific values), and fall back on the default values in the app's properties values if no override is found.
  6. 让应用程序首先检查上下文(针对特定于环境的值),如果未找到覆盖,则返回应用程序属性值中的默认值。

#2


1  

A Properties file is what I've always used. It's editable by hand as well as in in your software and the Properties object can read itself in and write itself out to the filesystem. Here's the javadoc page:

属性文件是我一直使用的。它可以手动编辑,也可以在软件中编辑,并且Properties对象可以自行读取并将自身写入文件系统。这是javadoc页面:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html

#3


1  

If you use maven, you can use it's resource filtering abilities, along with profiles to generate a properties file for each environment you're deploying into.

如果您使用maven,则可以使用它的资源过滤功能以及配置文件为您要部署的每个环境生成属性文件。

As an added bonus, maven can also deploy your web app for you.

作为额外的奖励,maven还可以为您部署您的Web应用程序。

#4


0  

The duplication is not really a problem, having a central config file the the other files 'extend' is likely to casue more of a headache in the long term.

重复并不是一个真正的问题,拥有一个*配置文件,其他文件的“扩展”可能会长期让人头疼。

My advice is to use ant to load (copy and move) the appropriate file(s) into place and then launch the app (bundle into war?). Just have a different task for each environment. So you will have three config files (dev.config, test.config and production.config) which will be moved and overwrite the config in the /WEB-INF folder depending on the task that you are running.

我的建议是使用ant将相应的文件加载(复制和移动)到位,然后启动应用程序(捆绑到战争?)。只为每个环境执行不同的任务。因此,您将有三个配置文件(dev.config,test.config和production.config)将被移动并覆盖/ WEB-INF文件夹中的配置,具体取决于您正在运行的任务。

#5


0  

I would suggest to have a separate config file for environment parameters alone if you want to avoid cluttering. Then you will have one more config file to manage. This is a trade off between number of config files vs complexity of each config file.

如果你想避免混乱,我建议单独为环境参数配置一个配置文件。然后你将有另外一个配置文件来管理。这是配置文件数量与每个配置文件的复杂性之间的权衡。