简体   繁体   English

Java EE7:@DatasourceDefinition,如何参数化值?

[英]Java EE7: @DatasourceDefinition, how to parameterize values?

I am learning how to use @DataSourceDefinition . 我正在学习如何使用@DataSourceDefinition The documentation gives a specific example as 文档给出了一个具体的示例,例如

 @DataSourceDefinition(name="java:global/MyApp/MyDataSource",
      className="com.foobar.MyDataSource",
      portNumber=6689,
      serverName="myserver.com",
      user="lance",
      password="secret"
   )

Question
How can I substitute these values for environment variables? 如何用这些值代替环境变量? like DB_HOST , DB_URL DB_HOSTDB_URL

The reason is when I deploy my application to providers like OPENSHIFT , all I get the values to use to connect to database. 原因是当我将应用程序部署到OPENSHIFT提供程序时,所有获得的值都用于连接数据库。

I would not like to hardcode them in my codebase. 我不想在我的代码库中对它们进行硬编码。 This adds coupling to particular database and can not be switched at runtime 这会增加与特定数据库的耦合,并且无法在运行时进行切换

Any ideas? 有任何想法吗?

If you want to deploy a Java EE application in OPENSHIFT as well as in your LOCAL JBoss then it's recommended to use the JBoss configuration file to configure the datasource properties and use that datasouce JNDI name in your persistence.xml . 如果要在OPENSHIFT以及本地JBoss中部署Java EE应用程序,则建议使用JBoss配置文件来配置数据源属性,并在persistence.xml中使用该数据源JNDI名称

Refer : https://help.openshift.com/hc/en-us/articles/202964690-Default-database-connectivity-configurations-for-JBoss-Applications 请参阅: https : //help.openshift.com/hc/zh-cn/articles/202964690-Default-database-connectivity-configurations-for-JBoss-Applications

If you really don't want to use the application server (like JBoss) specific configurations. 如果你真的不想使用应用服务器(如JBoss)的具体配置。 Then you can write a data source wrapper for @DataSourceDefinition annotated class and change the value from of the data source using a external property file. 然后,您可以为@DataSourceDefinition注释类编写数据源包装器 ,并使用外部属性文件更改数据源的值。

Refer : http://jdevelopment.nl/switching-data-sources-datasourcedefinition 请参阅: http : //jdevelopment.nl/switching-data-sources-datasourcedefinition

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

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