Spring config different datasource based on different environment(dev, test, prod) Two ways now
2012-12-14 06:56
507 查看
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
2. ${target_env}.configuration.properties like: test.configuration.properties ( you have to give an value for target_env)
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: /article/9072516.html
--------------------------------------------------------------------------------------------------------------------------------------
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:
We run the jar as before.
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: /article/9072516.html
--------------------------------------------------------------------------------------------------------------------------------------
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.
相关文章推荐
- springboot多环境(dev、test、prod)配置
- [Javascript] Different ways to create an new array/object based on existing array/object
- Sequential Task Process based on Spring Event Framework
- 24 You have enabled backup optimization for the RMAN environment. Identify two criteria on which RMA
- PlatformTransactionManager spring config.xml datasource
- Known issue about configuring BAM Portal and WSS based on different IIS versions
- Tracing to the Network Attack Based on Large Data Environment
- 【构造】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) A. Bear and Different Names
- [org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser] are only available on
- spring低版本报错:java.lang.IllegalStateException: Context namespace element ‘annotation-config’ and its parser class [*] are only available on
- Flash Movie Authoring Environment based on State Diagram
- Disable testSuite and testCase on some environment
- spring cloud config实现datasource的热部署
- Java Config 下的Spring Test方式
- Two useful post about SYSVOL when restoring DC to a test environment
- Liferay two dev environment set up
- Java Config下的Spring Test几种方式实例详解
- org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method service() cannot be found on com.my.blog.springboot.thymeleaf.util.MethodTest type
- Different ways to install kernel hooks under Windows NT based systems
- Android Dev Intro - An OpenGL ES renderer based on the GLSurfaceView rendering framework