This phase utilizes the infra we built before and implements the profile pattern.
Step 2.1 - create a properties interface
Create an interface for the configuration data you have.
In our case, the interface will provide access to the four configuration data items.
so it would look something like:
public interface SystemStrings { String getJdbcUrl(); String getDBUsername(); String getDBPassword(); Boolean getHibernateShowSQL(); //.....Step 2.2 - create a class for each profile
Example for a development profile:
@Dev //Notice the dev annotation @Component("systemStrings") public class SystemStringsDevImpl extends AbstractSystemStrings implements SystemStrings{ public SystemStringsDevImpl() throws IOException { //indication on the relevant properties file super("/properties/my_company_dev.properties"); } }Example for a production profile:
@Prouction //Notice the production annotation @Component("systemStrings") public class SystemStringsProductionImpl extends AbstractSystemStrings implements SystemStrings{ public SystemStringsProductionImpl() throws IOException { //indication on the relevant properties file super("/properties/my_company_production.properties"); } }
The two classes above are where the binding between the properties file and the related environment occur.
You've probably noticed that the classes extend an abstract class. This technique is useful so we won't need to define each getter for each Profile, this would not be manageable in the long run, and really, there is no point of doing it.
The sweet and honey lies in the next step, where the abstract class is defined.
Step 2.3 - create an abstract file which holds the entire data
public abstract class AbstractSystemStrings implements SystemStrings{ //Variables as in configuration properties file private String jdbcUrl; private String dBUsername; private String dBPassword; private boolean hibernateShowSQL; public AbstractSystemStrings(String activePropertiesFile) throws IOException { //option to override project configuration from externalFile loadConfigurationFromExternalFile();//optional.. //load relevant properties loadProjectConfigurationPerEnvironment(activePropertiesFile); } private void loadProjectConfigurationPerEnvironment(String activePropertiesFile) throws IOException { Resource[] resources = new ClassPathResource[ ] { new ClassPathResource( activePropertiesFile ) }; Properties props = null; props = PropertiesLoaderUtils.loadProperties(resources[0]); jdbcUrl = props.getProperty("jdbc.url"); dBUsername = props.getProperty("db.username"); dBPassword = props.getProperty("db.password"); hibernateShowSQL = new Boolean(props.getProperty("hibernate.show_sql")); } //here should come the interface getters....
No comments :
Post a Comment