[英]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中自己完成相比有几个优点:
DataSource
instance pointing to the DB and do not need to worry about username and password anymore. 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). 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。
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. 此外,如果您将此文件检入某个版本控制系统,那么您可能不希望将您的数据库用户名/密码发布到本地计算机。
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而无需修改任何内容。
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.