Over a million developers have joined DZone.
Platinum Partner

A Simpler Custom Property in Spring

· Java Zone

The Java Zone is brought to you in partnership with AppDynamics. Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems.

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

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

Published at DZone with permission of Rob Gordon , DZone MVB .

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}