You can specify your variable attributes in the properties file and define environment properties files such as local.properties, production.propertied, etc.
Now, based on the environment, one of these properties files can be read in one of the listeners called at startup, for example, ServletContextListener.
The property file will contain special values ββfor various keys.
Example "local.propeties"
db.logsDataSource.url=jdbc:mysql://localhost:3306/logs db.logsDataSource.username=root db.logsDataSource.password=root db.dataSource.url=jdbc:mysql://localhost:3306/main db.dataSource.username=root db.dataSource.password=root
Example "production.properties"
db.logsDataSource.url=jdbc:mariadb://111.111.111.111:3306/logs db.logsDataSource.username=admin db.logsDataSource.password=xyzqer db.dataSource.url=jdbc:mysql://111.111.111.111:3306/carsinfo db.dataSource.username=admin db.dataSource.password=safasf@mn
To use these property files, you can use a REsource as follows
PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer(); ResourceLoader resourceLoader = new DefaultResourceLoader(); Resource resource = resourceLoader.getResource("classpath:"+System.getenv("SERVER_TYPE")+"DB.properties"); configurer.setLocation(resource); configurer.postProcessBeanFactory(beanFactory);
SERVER_TYPE can be defined as an environment variable with corresponding values ββfor the local and production environments.
With these changes, appplicationContext.xml will have the following changes
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="${db.dataSource.url}" /> <property name="username" value="${db.dataSource.username}" /> <property name="password" value="${db.dataSource.password}" />
Hope this helps.
Sunil Dec 18 '15 at 4:35 2015-12-18 04:35
source share