我已经厌倦了所有这些无聊的样板代码来解析应用程序配置,例如数据库连接、工作目录、API 端点等等。 Spring IoC 看起来不错,但这将迫使我的应用程序的用户修改 XML 文件,只是为了编辑数据库 URL 等。这也可能分布在 XML 文件中,我的所有其他接线都在该文件中。
允许最终用户配置服务(不在应用程序服务器内运行)的最佳技术是什么?你们用什么go?
最佳答案
使用 Spring,作为 XML 中的显式连接、自动连接或它们的某种组合,定义“常量”配置,然后将其余部分外部化到属性文件中。数据库凭证是这方面的一个常见示例。
参见 Spring and Ibatis Tutorial有关此的基线示例。简短版本:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:database.properties"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${database.class}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</bean>
使用 database.properties(在类路径中):
database.username=scratch
database.password=scratch
database.class=oracle.jdbc.OracleDriver
database.url=jdbc:oracle:thin:@localhost:1521:XE
关于java - 应用配置(Spring?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1188381/