Spring SystemPropertyInitializingBean

When using POI in any of your projects, and the application you’re building is a web application, you probably have it running on a Windows machine. If not, you know all about the struggle with the “headless mode” environment setting to tell the JVM how to handle graphics rendering.

I always like to keep my applications as clean as possible to the users. The system administrator is also a user of the software (during installation at least). So I wanted the application to set the environment properties itself, In this case, I built a nice little Spring bean to handle this. The solution is so simple, that it is almost a brilliant display of what Spring can solve for you. Suddenly, all these environment setting problems turned into a simple Spring configuration problem. Here’s how:

In the Spring configuration, the whole “headless mode” problem got reduced to this bean definition:

<bean id="systemproperty_initializer" 
   class="com.rolfje.SystemPropertyInitiliazingBean">
   <property name="systemProperties">
      <map>
         <!-- Set headless mode to true, 
              for POI sheet.autoSizeColumn See 
              http://poi.apache.org/hssf/quick-guide.html 
         -->
         <entry key="java.awt.headless" value="true"/>
     </map>
   </property>
</bean>

The code for the SystemPropertyInitilizingBean is really a simple list iterator which walks though the map and sets everything as a system property:

/**
 * Bean for automatically initializing System 
 * properties from within a Spring context. 
 */
public class SystemPropertyInitializingBean 
       implements InitializingBean {

        /** Properties to be set */
        private Map systemProperties;

        /** Sets the system properties */
        public void afterPropertiesSet() 
               throws Exception {
                if (systemProperties == null || 
                       systemProperties.isEmpty()) {
                        // No properties to initialize
                        return;
                }

                Iterator i = systemProperties.keySet().iterator();
                while (i.hasNext()) {
                        String key = (String) i.next();
                        String value = (String) systemProperties.get(key);

                        System.setProperty(key, value);
                }
        }

        public void setSystemProperties(Map systemProperties) {
                this.systemProperties = systemProperties;
        }
}

In my opinion this is a nice and clean solution, which does not impact any of the application code, hides the setting of obscure system properties from the administrator, is simple to the developer, gives room to add comments (see Spring xml above)and is reusable. All in a few lines of code. You can even easily have it parese settings from a config file, and put that into system environment variables without changing a line of code. All in Spring XML.

Sometimes it’s the small things that make you feel nice :-)

About these ads

5 Responses to Spring SystemPropertyInitializingBean

  1. Craig says:

    This is quite useful – Thanks!

  2. pikbadaluk says:

    perfect! they should add it to spring ;-)

  3. […] This idea is close, but needs to add Java code: Spring SystemPropertyInitializingBean […]

  4. […] This idea is close, but needs to add Java code: Spring SystemPropertyInitializingBean […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 30 other followers