Spring config different datasource based on different environment(dev, test, prod) Two ways now

Spring is very powerful when I use Spring more and more, it provides a lot of fancy functions. In my standalone app, I config datasource in context.xml, jdbc.driver.name, url, username, password, all of them are hard-coded. When I tried to deliver it to client, problem came out. The datasource setting is different, I have to make it configurable. Since it's a standalone app, I need to use command line to run it. Previously, I have no idea. I searched online, finally I got a perfect solution 大笑.

The key is to use Spring PropertyPlaceholderConfigurer. Example time...

1. context.xml file

<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<value>${target_env}.configuration.properties</value>
				<value>file:${target_env}</value>
			</list>
		</property>
	</bean>

	<bean id="myAccessDataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.drivername}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

2. ${target_env}.configuration.properties like: test.configuration.properties ( you have to give an value for target_env)

jdbc.drivername=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://10.10.10.10:1433;databaseName=mydatabase
jdbc.username=myusername
jdbc.password=mypassword


3. Pass target_env value via command

3.1 Use external properties file, it'll match second location

E:\Liferay Developer Studio\workspace\myproject\target>java -Dtarget_env=E:\Tina\test.configuration.properties -jar project.jar

3.2 Use internal properties file which is packed inside project.jar, assume has test.configuration.properties

E:\Liferay Developer Studio\workspace\myproject\target>java -Dtarget_env=test -jar project.jar


I tried to set a default value by using System Properties, but it doesn't work on my case, anyone knows how to do that, please show me an example微笑

If have problems on building jars, see my another blog: http://blog.csdn.net/smile_juan/article/details/8293229

--------------------------------------------------------------------------------------------------------------------------------------

In case, we only want one properties file, but we config like this way:

local.jdbc.drivername=com.microsoft.sqlserver.jdbc.SQLServerDriver
local.jdbc.url=****
local.jdbc.username=****
local.jdbc.password=****
dev.jdbc.drivername=com.microsoft.sqlserver.jdbc.SQLServerDriver
dev.jdbc.url=
dev.jdbc.username=
dev.jdbc.password=
prod.jdbc.drivername=com.microsoft.sqlserver.jdbc.SQLServerDriver
prod.jdbc.url=
prod.jdbc.username=
prod.jdbc.password=


Through the command line, we want to pass like -Dtarget_env=local, then it will use all settings start with local. Spring is very powerful, it provides the way to handle the case. What we only need to do is config in configuration.xml like this:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	<property name="location" value="classpath:datasource.properties" />
	</bean>
 
	<bean id="myAccessDataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${${target_env}.jdbc.drivername}" />
		<property name="url" value="${${target_env}.jdbc.url}" />
		<property name="username" value="${${target_env}.jdbc.username}" />
		<property name="password" value="${${target_env}.jdbc.password}" />
	</bean>

We run the jar as before.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值