This post will explain how to load environment specific[DEV,SIT,UAT,PROD] properties from the single properties file
Step 1: Create simple java project using eclipse
Step 2: Create a abstract class , with under package - com.javaguru.property, i have created with name - PropertyClient.java
Step 3: Create implementation class.
Step 5: Create applicationContext.xml under src- folder
Step 6: Create Test class with name - TestEnvSpecificProperty.java- or call the code which ever place you need to load the properties
Step 8: Required jars
Step 1: Create simple java project using eclipse
Step 2: Create a abstract class , with under package - com.javaguru.property, i have created with name - PropertyClient.java
package com.javaguru.property; public abstract class PropertyClient { protected String hostName; protected String userId; protected String password; public PropertyClient(String hostName, String userId, String password) { super(); this.hostName = hostName; this.userId = userId; this.password = password; } public PropertyClient(){ } public String getHostName() { return hostName; } public void setHostName(String hostName) { this.hostName = hostName; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } //common methods related to functionality }
Step 3: Create implementation class.
package com.javaguru.property; public class PropertyClientImpl extends PropertyClient{ public PropertyClientImpl(String hostName, String userId, String password) { super(hostName, userId, password); } public PropertyClientImpl(){ super(); } //Implementation of abstract methods }Step 4: Create property file name called - application.properties under src folder
[DEV] ftp.dev.hostname=dev.com ftp.dev.username=user ftp.dev.password=pass [SIT] ftp.sit.hostname=sit.net ftp.sit.username=user ftp.sit.password=pass [UAT] ftp.uat.hostname=uat.net ftp.uat.username=user ftp.uat.password=pass [PROD] ftp.prod.hostname=prod.net ftp.prod.username=user ftp.prod.password=pass
Step 5: Create applicationContext.xml under src- folder
application.properties
Step 6: Create Test class with name - TestEnvSpecificProperty.java- or call the code which ever place you need to load the properties
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.javaguru.property.PropertyClient; import com.javaguru.property.PropertyClientImpl; /** * * @author siva * */ public class TestEnvSpecificProperty { public static void main(String[] args) { //Set which environment properties we need to load. System.setProperty("env","dev"); //load the applicationContext.xml file ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); PropertyClient propertyClient= (PropertyClientImpl)context.getBean("propertyClient"); //print property details System.out.println("Host Name: ["+propertyClient.getHostName()+"]"); System.out.println("User ID: ["+propertyClient.getUserId()+"]"); System.out.println("Password: ["+propertyClient.getPassword()+"]"); } }Step 7: Output would be like below, if you mention System property as dev
Host Name: [dev.com] User ID: [user] Password: [pass]
Step 8: Required jars
commons-codec-1.3.jar commons-collections-3.2.jar commons-logging-1.1.1.jar commons-net-3.3.jar spring-asm-3.0.5.RELEASE.jar spring-beans-3.0.4.RELEASE.jar spring-context-3.0.5.RELEASE.jar spring-core-3.0.4.RELEASE.jar spring-expression-3.2.1.release.jar
I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Salesforce, kindly contact us http://www.maxmunus.com/contact
ReplyDeleteMaxMunus Offer World Class Virtual Instructor led training on Apache Kafka. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
For Demo Contact us.
Nitesh Kumar
MaxMunus
E-mail: nitesh@maxmunus.com
Skype id: nitesh_maxmunus
Ph:(+91) 8553912023
http://www.maxmunus.com/
Thanks Nitesh for your appreciation..I will let you know.. If some one required any training...
Delete