应用程序配置(春季?) [英] Application Configuration (Spring?)

查看:83
本文介绍了应用程序配置(春季?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经厌倦了所有这些无聊的样板代码来解析应用程序配置,例如数据库连接,工作目录,API端点等等. Spring IoC看起来不错,但是这将迫使应用程序的用户修改XML文件,只是为了编辑数据库URL,依此类推.这也可能非常分散在我所有其他接线所在的XML文件中.

I'm getting tired of all this boring boilerplate code to parse application configuration like database connections, working directories, API endpoints and whatnot. Spring IoC looks nice, but this will force the user of my application to modify the XML file just to edit the database URL and so on. This might also be very distributed in the XML file where all my other wiring ocours.

允许最终用户配置服务(不在应用程序服务器内部运行)的最佳技术是什么?你们用什么?

What is the best technique to allow end-users to configurate services (which do not run inside an application server)? What go you guys use?

推荐答案

使用Spring(在XML中进行显式连接,自动装配或它们的某种组合)来定义常量"配置,然后将其余部分外部化到属性文件中.数据库凭证是这种情况的常见示例.

Use Spring, being explicit wiring in XML, auto-wiring or some combination thereof, to define "constant" config and then externalize the rest in properties files. Database credentials are a common example of this.

有关基准,请参见 Spring和Ibatis教程这个例子.简短版本:

See Spring and Ibatis Tutorial for a baseline example of this. Short version:

<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>

with database.properties(在类路径中):

with database.properties (in the classpath):

database.username=scratch
database.password=scratch
database.class=oracle.jdbc.OracleDriver
database.url=jdbc:oracle:thin:@localhost:1521:XE

这篇关于应用程序配置(春季?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆