Skip to content

www.rolfje.com

Spring SystemPropertyInitializingBean

Posted on 2008-07-23 By rolfje 10 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 (10) 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
  9. plinko idle clicker says:
    2026-09-20 at 05:17

    Wait, is that Spring bean initialization actually supposed to happen at startup or just on first access? Seems like it could break things if the context hasn’t fully loaded yet.

    Reply
    1. rolfje says:
      2026-10-21 at 17:47

      It is a post from 2008, but yes I can assure you that this worked in a couple of my projects. You could still be correct of course, but as far as I know this has worked fine. It might not in more modern versions of Spring, I don’t know.

      Reply

Leave a Reply Cancel reply

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

         
@rolfje.com
www.youtube.com/watch?v=ycGh...
15 hours ago
@rolfje.com
Vandaag gaat #degrootstekerstboom aan! www.degrootstekerstboom.nl
18 hours ago
Reposted by @rolfje.com
@shadowmakersdr.bsky.social
Ik ben blij dat de overheid zo consequent is in z'n campagnes. Iedereen fulltime werken, maar wel buiten de spits naar je werk en gebruik de stroom van je zonnepanelen overdag (als je niet thuis bent want op je werk) terwijl je mantelzorgt èn fsyieke winkels bezoekt om 'de winkelstraat' te redden.
3 days ago

Recent Comments

  • Monica Hall on Het belastinghuiswerk nagekeken
  • rolfje on Spring SystemPropertyInitializingBean
  • plinko idle clicker on Spring SystemPropertyInitializingBean
  • rolfje on The podcast problem (fixed)
  • SirenMix on The podcast problem (fixed)

Tags

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

Categories

  • Apple (107)
  • Divorce (1)
  • Electronics (5)
  • Fun (58)
  • Games (8)
  • Hardware (74)
  • Microsoft (18)
  • Racing (16)
  • Software (144)
  • Uncategorized (69)
  • Workshop (23)

Archives

Brought to you without adds, banners, cookies or tracking. This one is on me. Yes, life can be this good. Pay it forward.
AI policy - Privacy policy

Copyright © 2026 www.rolfje.com.

Powered by PressBook WordPress theme