JavaBlog.fr / Java.lu DEVELOPMENT,Java,Spring Spring: access to properties or parameters in all places of the application

Spring: access to properties or parameters in all places of the application

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 ##################
2huo.config.mail.default.domain=huo-huo.huo
3 
4# ################ MAIL PARAMETERS ##################
5huo.config.smtp.host=localhost
6huo.config.smtp.port=25
7huo.config.smtp.mailfrom=fromHuseyin@huo-huo.huo
8huo.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:

01public class ConfigMyParameters {
02 
03    // ----------------------------------------------------- PRIVATE ATTRIBUTES
04    /**
05     * Singleton instance
06     */
07    private static ConfigMyParameters instance = null;
08 
09    /**
10     * Default mail domain
11     */
12    private String defaultMailDomain = "";
13 
14    /**
15     * Contains the identity (email) used to send mails.
16     */
17    private String smtpMailFrom = "";
18 
19    /**
20     * Smtp server host
21     */
22    private String smtpMailhost = "";
23     
24    /**
25     * Smtp server port
26     */
27    private String smtpMailport = "";
28      
29    /**
30     * Emails' list of administrators
31     */
32    private List<String> mailAdministrators = new ArrayList<String>();
33 
34    // --------------------------------------------------------- PUBLIC METHODS
35    /**
36     * @return an unique instance of the application configuration class.
37     */
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");
43        } // end-if
44        return instance;
45    }
46     
47    // ------------------------------------------------------ GETTERS & SETTERS
48         // ....
49}

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"?>
06    xsi:schemaLocation="
13    default-autowire="no"
14    default-lazy-init="false"
15    default-dependency-check="none">
16 
17    <!--
18     | Retrieve the common configuration datasource, constants,...
19     -->
20    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
21        <property name="locations">
22            <value>classpath:myparameters.properties</value>
23        </property>
24    </bean>
25     
26 
27    <!--
28     | This bean is only used to determine in which environment this business is executed
29     -->
30    <bean id="ConfigMyParameters" class="huo.ConfigMyParameters">
31        <!--
32         | ENVIRONMENT
33         -->
34        <property name="defaultMailDomain" value="${huo.config.mail.default.domain}" />
35 
36        <!--
37         | SMTP Parameters
38         -->
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}" />
43    </bean>
44</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”.

SpringLoadAndAccessProperties.zip

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post