I have a utility class which has common configurations related to email-sender, the configurations changes based on the environment like Staging
and Production
. Now how can I dynamically choose the configurations based on the environment?
here is my code,
EmailUtility.java
package com.housecar.common;
public class EmailUtility {
//For staging
public static final String FROM_EMAIL_ID = "[email protected]";
public static final String FROM_NAME = "xyz";
static final String SMTP_USERNAME = "[email protected]";
static final String SMTP_PASSWORD = "15sss67$";
public static final String REPLY_EMAIL_ID = "[email protected]";
public static final String MAIL_SMTP_PORT = "587";
public static final String MAIL_SMTP_SOCKET_FACTORY_PORT = "587";
public static final String SMTP_HOST = "smtp.gmail.com";
public static final String MAIL_SMTP_SOCKETFACTORY_CLASS = "javax.net.ssl.SSLSocketFactory";
//for production
/*public static final String FROM_EMAIL_ID = "[email protected]";
public static final String FROM_NAME = "xyz";
static final String SMTP_USERNAME = "AKYeeeELEQAGAA"; // Replace with
// your SMTP
// username.
static final String SMTP_PASSWORD = "gvwwwwwbgpGm/C/LmUYUK5HosQar7mTSwjl5MFeBRR";
public static final String REPLY_EMAIL_ID = "[email protected]";
public static final String MAIL_SMTP_PORT = "587";
public static final String MAIL_SMTP_SOCKET_FACTORY_PORT = "587";
public static final String SMTP_HOST = "email-smtp.us-east-1.amazonaws.com";
public static final String MAIL_SMTP_SOCKETFACTORY_CLASS = "javax.net.ssl.SSLSocketFactory";*/
}
In my code I'm manually commenting out the configuration!
You can use the properties file to solve your problem, like u can have two property files depending on the environment.
1) config_staging.properties 2) config_production.properties
and move your staging email configuration to config_staging.properties, move your production configuration to config_production.properties.
which will be configured while running the app.
for example,
config_staging.properties
smtp.username = "[email protected]";
smtp.password = "15sss67$";
config_production.properties
smtp.username = "AKYeeeELEQAGAA";
smtp.password = "gvwwwwwbgpGm/C/LmUYUK5HosQar7mTSwjl5MFeBRR";
then inject them into EmailUtility class,
@Value("${smtp.username}")
private String smtpUsername;
@Value("${smtp.password}")
private String smtpPassword;
You should lookup the system dependend values from a properties store (e.g. a properties file which is handled by Javas Properties
class).
But you should not provide all systems information in the same file.
You should have separate files (with the same name) an have your deployment process copying the file for the target system to the expected location.
This way you enable operations team to manipulate (or preconfigue) a properties file with data you (as a developer) should not know, eg. passwords for high security systems...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With