My entire carrier is linked to Java. I have been working in Java for 15 years and I still love it. I currently develop NextReports application My Personal Blog My NextReports Blog NextReports Site Mihai has posted 35 posts at DZone. You can read more from them at their website. View Full User Profile

Spring 3 and Application Settings

12.13.2011
| 7147 views |
  • submit to reddit

If you use Spring in your applications, you may need to pass some application settings to a spring bean. Your settings can be defined in properties files, in system properties or inside an internal storage.

Let's say we want to use a property inside a spring bean like the following:

<bean id="myBean" class="com.mypackage.MyClass">
        <property name="myProperty">
            <value>${propertyValue}</value>
        </property>
</bean> 

 If propertyValue is defined inside a properties file we should define a propertyConfigurer bean:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:myfile.properties</value>
        </property>
</bean>

 If we want propertyValue to be overridden by system properties we can specify  systemPropertiesModeName :

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:myfile.properties</value>
        </property>
        <property name="systemPropertiesModeName">
            <value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
        </property>
</bean>

If our settings are defined inside a storage system (database, content repository), spring 3 will help us a lot with its new expression language SpEL. This allows for calling methods from a defined spring bean.

Lets say we have a settings bean like the following, where storageService  can be any storage you need:

<bean id="settings" class="com.mypackage.SettingsBean">
        <property name="storageService" ref="storageService"></property>
</bean>

 SettingBean will offer us the propertyValue we need :

public class SettingsBean {
    
    private StorageService storageService;    
    
    public Settings getSettings() {
        return storageService.getSettings();
    }    

    @Required
    public void setStorageService(StorageService storageService) {
        this.storageService = storageService;        
    }
    
    // helper methods used in Spring with SpEL    
    public String getPropertyValue() {
        Settings settings = getSettings();
        return settings.getPropertyValue();
    }
}

 With SpEL we can use the propertyValue in our bean like this:

<bean id="myBean" class="com.mypackage.MyClass">
        <property name="myProperty" value="#{settings.getPropertyValue()}"/>
</bean>

If the bean, which needs some settings from a storage, is a bean from the spring api, then it's very easy to set the properties using SpEL . Otherwise we would have had to extend that class to inject our storage. For example a Spring ThreadPoolTaskExecutor can be defined like this :

<bean id="schedulingTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="threadNamePrefix" value="Scheduler-"/>
        <property name="corePoolSize" value="#{settings.getSchedulerCorePoolSize()}"/>
        <property name="maxPoolSize" value="#{settings.getSchedulerMaxPoolSize()}"/>
        <property name="queueCapacity" value="#{settings.getSchedulerQueueCapacity()}"/>
</bean>

 

Published at DZone with permission of its author, Mihai Dinca - Panaitescu.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Bruce Baron replied on Wed, 2011/12/14 - 6:22pm

I think you can use #{settings.propertyValue} instead of #{setting.getPropertyValue()}

Mihai Dinca - P... replied on Thu, 2011/12/15 - 2:33am in response to: Bruce Baron

Yes, you can use #{settings.propertyValue}, if you have propertyValue as a property inside Settings bean.

The ideea with application settings saved in some storage (at least in my case) is that they can be modified anytime from user interface. So you must not keep the actual value in your bean, but allways read it from storage. You may have a cache for application, so the read of properties be  fast.

So , I think using  #{settings.propertyValue} is suitable only if your propertyValue cannot be modified from your application.

John David replied on Thu, 2012/01/26 - 3:08am

I think using  #{settings.propertyValue} is suitable for encapsulation purposes. Java code should have declared this variable as private and it should provide interface to set and get its values.

In such cases accessing this parameter in above way is much suitable.

Java Eclipse

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.