Platinum Partner
java,tips and tricks

A Simpler Custom Property in Spring

I previously presented a CustomPropertyConfgurer that allows properties outside of Spring to be accessed from within Spring. The article was syndicated by DZone where a reader noted that in fact the Spring class PropertySourcesPlaceholderConfigurer has a setProperties method, and so for my particular example, the CustomPropertyConfigurer was redundant.

For the record, my previous example reduces to this:

public static void main(String... args) {
 
    char[] password = System.console().readPassword("Password: ");
 
    Properties properties = new Properties();
    properties.setProperty("jdbc.password", new String(password));
 
    ConfigurableApplicationContext context =
            new ClassPathXmlApplicationContext(
                new String[] { "rob/MyQuery.xml" },
                false);
 
    PropertySourcesPlaceholderConfigurer configurer =
                new PropertySourcesPlaceholderConfigurer();
 
    configurer.setProperties(properties);
    configurer.setIgnoreUnresolvablePlaceholders(true);
 
    context.addBeanFactoryPostProcessor(configurer);       
 
    context.refresh();
 
    MyQuery myQuery = context.getBean(MyQuery.class);
 
    myQuery.run();
 
    context.close();
}

However the motivation behind my foray into Spring property configuration is to improve Oddjob’s integration with Spring. My CustomPropertyConfigurer has now become an OddjobPropertyConfigurer

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}