简体   繁体   English

何时将配置放在file.properties或Jndi中

[英]When to put configuration in file.properties or Jndi

For a long time in many IT services, I see some complex process to manage Java EE application configuration depending of the environments: - custom tools, with Database or not, to manage replacement in the properties file (unzip war, replace, zip war...) - Externalize properties file in obscure directory in the server (and some process to update it some time) and some time with a JNDI configuration... - maven profile and lot of big properties files 在很多IT服务中,我看到一些复杂的过程来管理Java EE应用程序配置,具体取决于环境: - 自定义工具,数据库与否,以管理属性文件中的替换(解压缩,替换,压缩战争。 ..) - 外部化服务器中的obscure目录中的属性文件(以及一些时间更新它的过程)和一段时间使用JNDI配置... - maven配置文件和许多大属性文件

But for database connection everybody use jndi datasource. 但是对于数据库连接,每个人都使用jndi数据源。

Why this is not generalized for all configurations that depend of environment ? 为什么对于依赖于环境的所有配置而言,这不是一般化的?

Update : I want deal with other variable than datasource, there is no question about datasource : it's in configured in JNDI for Java EE application. 更新:我想要处理除数据源之外的其他变量,对数据源没有任何疑问:它是在JNDI中为Java EE应用程序配置的。 After if you want hack JNDI... 如果你想要破解JNDI ...

Setting up database connectivity (like user name, password, URL, driver etc.) somewhere in the application server has several advantages over doing it yourself in the WAR: 在应用程序服务器中的某处设置数据库连接(如用户名,密码,URL,驱动程序等)与在WAR中自己完成相比有几个优点:

  • The app server can be a central point where the DB is configured, and you might have several WARs running on that server sharing a DB. 应用服务器可以是配置数据库的中心点,您可能在该服务器上运行多个共享数据库的WAR。 So you need to set it up only once. 所以你只需要设置一次。
  • The DB settings, especially the credentials (username, password) are stored somewhere in the app server instead of somewhere in the WAR. 数据库设置,尤其是凭据(用户名,密码)存储在应用服务器中的某个位置,而不是存储在WAR中的某个位置。 That can have security implications (for instance, restricting access to that file is easier done than in a WAR archive). 这可能会产生安全隐患(例如,限制对该文件的访问比在WAR存档中更容易)。
  • You can set up one JNDI path to retrieve a DataSource instance pointing to the DB and do not need to worry about username and password anymore. 您可以设置一个 JNDI路径来检索指向数据库的DataSource实例,而不再需要担心用户名和密码。 If you have multiple app servers (one live system, one test system, several developer machines) with different DB URLs and credentials, then you can just configure that in each app server individually and deploy the WAR files without the need to change DB settings (see below). 如果您有多个具有不同数据库URL和凭据的应用服务器(一个实时系统,一个测试系统,多个开发人员机器),那么您可以单独在每个应用服务器中配置它并部署WAR文件而无需更改数据库设置(见下文)。
  • The server might provide additional services, like connection pools, container managed transactions, etc. So again, you don't have to do it on your own in the WAR. 服务器可能会提供其他服务,例如连接池,容器管理事务等。因此,您不必在WAR中自行执行此操作。

This is true for other services provided by the app server as well, for example JavaMail. 对于app服务器提供的其他服务也是如此,例如JavaMail。

There are other cases where it you want to configure something that is specific to one web application and does not rely on the environment (the app server), like logging (although that may be set up in the app server, too). 在其他情况下,您希望配置特定于一个Web应用程序的某些内容,而不依赖于环境(应用程序服务器),例如日志记录(尽管也可以在应用程序服务器中设置)。 In those cases you might prefer using static config files, for instance log4j.properties . 在这些情况下,您可能更喜欢使用静态配置文件,例如log4j.properties


I want to illustrate the third bullet point a bit further ... 我想进一步说明第三个要点......
Suppose you have one WAR in three app servers (developer machine, test server, live server). 假设您在三个应用服务器(开发人员计算机,测试服务器,实时服务器)中有一个WAR。

Option 1 (DB setup in WAR) 选项1(WAR中的DB设置)

Create a database.properties : 创建database.properties

db.url=jdbc:mysql://localhost:3306/localdb
db.user=myusername
db.pass=mysecretpassword

#db.url=jdbc:mysql://10.1.2.3:3306/testdb
#db.user=myusername
#db.pass=mysecretpassword

#db.url=jdbc:mysql://10.2.3.4:3306/livedb
#db.user=myusername
#db.pass=mysecretpassword

Before you deploy it somewhere, you need to check if your settings are pointing to the right DB! 在将其部署到某个地方之前,您需要检查您的设置是否指向正确的数据库!

Also, if you check this file in to some version control system, then you might not want to publish your DB username/password to your local machine. 此外,如果您将此文件检入某个版本控制系统,那么您可能不希望将您的数据库用户名/密码发布到本地计算机。

Option 2 (DB setup in App Server) 选项2(App Server中的数据库设置)

Imagine you have configured the three servers with their individual DB settings, and each of them registers the DB with the JNDI path java:database/mydb . 想象一下,您已使用各自的数据库设置配置了三台服务器,并且每台服务器都使用JNDI路径java:database/mydb注册java:database/mydb

Then you can retrieve the DataSource like so: 然后您可以像这样检索DataSource

Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:database/mydb");

This is working on every app server instance and you can deploy your WAR without the need to modify anything. 这适用于每个应用服务器实例,您可以部署WAR而无需修改任何内容。


Conclusion 结论

By moving the configuration to the app server you'll have the advantage of separating settings depending on the environment from your app code. 通过将配置移动到应用服务器,您可以根据应用代码中的环境分离设置。 I would prefer this whenever you have settings involving IP addresses, credentials, etc. 每当你有涉及IP地址,凭证等的设置时,我都会更喜欢这个。

Using a static .properties file on the other hand is simpler to manage. 另一方面,使用静态.properties文件更易于管理。 I would prefer this option when dealing with settings that have no dependencies to the environment or are app specific. 在处理不依赖于环境特定于应用程序的设置时,我更喜欢此选项。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM