A Simpler Custom Property in Spring
Join the DZone community and get the full member experience.
Join For FreeI 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 Rob Gordon, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments