Skip to content

www.rolfje.com

Spring SystemPropertyInitializingBean

Posted on 2008-07-23 By rolfje 8 Comments on 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 🙂

Software Tags:Java, programming, Spring

Post navigation

Previous Post: Ibatis 2.2 Ignores XML encoding
Next Post: The Manualist

Comments (8) on “Spring SystemPropertyInitializingBean”

  1. Craig says:
    2009-02-12 at 01:08

    This is quite useful – Thanks!

    Reply
  2. pikbadaluk says:
    2009-03-19 at 14:39

    perfect! they should add it to spring 😉

    Reply
  3. Pingback: Set System Property With Spring Configuration File | The Largest Forum Archive
  4. Pingback: Set System Property With Spring Configuration File
  5. Pingback: Set System Property With Spring Configuration File | Ask Programming & Technology
  6. Pingback: Set System Property With Spring Configuration File - DexPage
  7. Pingback: Set System Property With Spring Configuration File - PhotoLens
  8. Pingback: Set System Property With Spring Configuration File

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

           

Recent Comments

  • rolfje on Methode Buijs uitgelegd
  • LinkedIn is at Peak Enshittifaction – Will Chatham's Blog on Linked-In not really Opt-in?
  • Hans j on 1N4148 diode as RF switch
  • Roaming Rhonda on DLNA on OSX, done right
  • Frans on How to fix a Krups XN2001 Nespresso machine

Tags

Anonimatron Apple backup design DIY DRM eclipse environment Fun gmail google hacking hamradio Hardware helicopter iphone ipod iTunes Java Keynote maven modelling motorcycle music news opinion oracle osx photo photography programming repair review security Software Steve Jobs T-Mobile technology Time Machine Ubuntu usability Utilities vacation windows Workshop

Categories

  • Apple (105)
  • Divorce (1)
  • Electronics (3)
  • Fun (57)
  • Games (7)
  • Hardware (72)
  • Microsoft (18)
  • Racing (14)
  • Software (134)
  • Uncategorized (65)
  • Workshop (20)

Archives

Copyright © 2025 www.rolfje.com.

Powered by PressBook WordPress theme