Spring config different datasource based on different environment(dev, test, prod) Two ways now
来源:互联网 发布:软件测试转正心得体会 编辑:程序博客网 时间:2024/04/30 11:38
The key is to use Spring PropertyPlaceholderConfigurer. Example time...
1. context.xml file
<beanclass="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.SQLServerDriverjdbc.url=jdbc:sqlserver://10.10.10.10:1433;databaseName=mydatabasejdbc.username=myusernamejdbc.password=mypassword
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.
- Spring config different datasource based on different environment(dev, test, prod) Two ways now
- Tow different ways of accessing Datasource in Alphablox
- different
- Different ways of implementing factories
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- different-ways-to-add-parentheses
- Different Ways to Add Parentheses
- OSX: Mac 10.8.2关机慢的真正原因分析和临时解决
- Redis系列-存储篇list主要操作函数小结
- 第九章:颜色
- 黑马程序员之数据结构学习笔记:快速排序
- Maven generates jar file with main class setting and dependency libs copy
- Spring config different datasource based on different environment(dev, test, prod) Two ways now
- 利用Camera实现手电筒功能
- 关于getchar()函数_1
- 直接删除数据文件后无法进入系统的解决方案
- 关于随机数_5
- Config log4j in standalone app
- 353. Penny wise, pound foolish. 贪小便宜吃大亏
- java里面的Annotation的使用
- HBase源代码调试(1)