Over a million developers have joined DZone.

A Simpler Custom Property in Spring

· Java Zone

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

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. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}