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:
1 | # ################ ENVIRONMENT ################## |
2 | huo.config.mail.default.domain=huo-huo.huo |
4 | # ################ MAIL PARAMETERS ################## |
5 | huo.config.smtp.host=localhost |
7 | huo.config.smtp.mailfrom=fromHuseyin@huo-huo.huo |
8 | 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:
01 | public class ConfigMyParameters { |
07 | private static ConfigMyParameters instance = null ; |
12 | private String defaultMailDomain = "" ; |
15 | * Contains the identity (email) used to send mails. |
17 | private String smtpMailFrom = "" ; |
22 | private String smtpMailhost = "" ; |
27 | private String smtpMailport = "" ; |
30 | * Emails' list of administrators |
32 | private List<String> mailAdministrators = new ArrayList<String>(); |
36 | * @return an unique instance of the application configuration class. |
38 | public static synchronized ConfigMyParameters getInstance() { |
39 | if ( null == instance) { |
40 | String SPRING_FILE_NAME = "huo-spring-properties-config.xml" ; |
41 | ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(SPRING_FILE_NAME); |
42 | instance = (ConfigMyParameters) ctx.getBean( "ConfigMyParameters" ); |
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.
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
14 | default-lazy-init = "false" |
15 | default-dependency-check = "none" > |
20 | < bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > |
21 | < property name = "locations" > |
22 | < value >classpath:myparameters.properties</ value > |
30 | < bean id = "ConfigMyParameters" class = "huo.ConfigMyParameters" > |
34 | < property name = "defaultMailDomain" value = "${huo.config.mail.default.domain}" /> |
39 | < property name = "smtpMailhost" value = "${huo.config.smtp.host}" /> |
40 | < property name = "smtpMailport" value = "${huo.config.smtp.port}" /> |
41 | < property name = "smtpMailFrom" value = "${huo.config.smtp.mailfrom}" /> |
42 | < property name = "mailAdministrators" value = "${huo.config.smtp.mailadministrators}" /> |
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”.
SpringLoadAndAccessProperties.zip
Related