Hi,
I propose you a simple solution based on Spring context, PropertyPlaceholderConfigurer to load and access to properties or parameters in all places of an application.
First, we have a properties’ file (for example) named “myparameters.properties” in the server’s classpath (for example in the “etc” folder of TOMCAT server) containing:
# ################ ENVIRONMENT ################## huo.config.mail.default.domain=huo-huo.huo # ################ MAIL PARAMETERS ################## huo.config.smtp.host=localhost huo.config.smtp.port=25 huo.config.smtp.mailfrom=fromHuseyin@huo-huo.huo huo.config.smtp.mailadministrators=tohuo@huo-huo.huo,topas@huo-huo.com,tofr@huo-huo.huo
Secondly, create a class containing a attribute for each parameter of above file. This class must used the “singleton” design pattern:
public class ConfigMyParameters { // ----------------------------------------------------- PRIVATE ATTRIBUTES /** * Singleton instance */ private static ConfigMyParameters instance = null; /** * Default mail domain */ private String defaultMailDomain = ""; /** * Contains the identity (email) used to send mails. */ private String smtpMailFrom = ""; /** * Smtp server host */ private String smtpMailhost = ""; /** * Smtp server port */ private String smtpMailport = ""; /** * Emails' list of administrators */ private List<String> mailAdministrators = new ArrayList<String>(); // --------------------------------------------------------- PUBLIC METHODS /** * @return an unique instance of the application configuration class. */ public static synchronized ConfigMyParameters getInstance() { if (null == instance) { String SPRING_FILE_NAME = "huo-spring-properties-config.xml"; ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(SPRING_FILE_NAME); instance = (ConfigMyParameters) ctx.getBean("ConfigMyParameters"); } // end-if return instance; } // ------------------------------------------------------ GETTERS & SETTERS // .... }
And lastly, we need to create a spring context in order to load the properties’ file “myparameters.properties” with the singleton “ConfigMyParameters.java”. In our example, this file is named “huo-spring-properties-config.xml” (cf the “ConfigMyParameters.java” class.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-autowire="no" default-lazy-init="false" default-dependency-check="none"> <!-- | Retrieve the common configuration datasource, constants,... --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:myparameters.properties</value> </property> </bean> <!-- | This bean is only used to determine in which environment this business is executed --> <bean id="ConfigMyParameters" class="huo.ConfigMyParameters"> <!-- | ENVIRONMENT --> <property name="defaultMailDomain" value="${huo.config.mail.default.domain}" /> <!-- | SMTP Parameters --> <property name="smtpMailhost" value="${huo.config.smtp.host}" /> <property name="smtpMailport" value="${huo.config.smtp.port}" /> <property name="smtpMailFrom" value="${huo.config.smtp.mailfrom}" /> <property name="mailAdministrators" value="${huo.config.smtp.mailadministrators}" /> </bean> </beans>
So, all application’s properties or parameters are accessible in all places of the application due to the “ConfigMyParameters” class.
Example: ConfigMyParameters.getInstance().getMailAdministratorsAsArray()
To add a new parameter, it is necessary to:
- add a line in the file “myparameters.properties” the form “KEY = VALUE”,
- add a new private attribute in the class “ConfigMyParameters” with getter/setter,
- change the Spring context file “huo-spring-properties-config.xml” to perform the mapping between the new parameter in properties’ file and class “ConfigMyParameters”.