Tuesday, 31 January 2012

Autowiring Using @Value and Optional Properties

Last October I wrote a blog entitled Autowiring Property Values into Spring Beans describing how to use Spring’s @Value annotation to inject property values into your beans from both a property file and Java system properties. Last week, I came across this little gotcha whilst helping out on another project....

This project was quite rightly re-using a module written as part of a different project. One of the module's tasks was to process a query string and it had a root url property that could be optionally injected to help with various bits of this processing. The original project that used this module was able to supply the handy root url property, whilst the project that re-used the module couldn’t. This meant that that when the web-server started up an exception similar to the one below was thrown...

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'autowiredFakaSource': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String miscillaneous.property_placeholder.AutowiredFakaSource.optional; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'optional'
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
 at …. shortened for readability.

It turns out that, when autowiring properties using @Value, one of the things that you can’t do is to specify the injected property as optional. This seems like a bit of an over-sight by the Guys at Spring as you can make autowired injected objects optional using:

  @Autowired(required = false)
private MyPojo anotherObject;

There are couple of work arounds for this problem. The first is to ensure that your project’s .properties file contains an empty place holder property. For example one of your Spring components contains something like this:

private String optionalPropertyValue;

...then your .properties file should contain an empty entry like this:


In this case, Spring will inject an empty string into your component and your code should run.

The second work around is to make use of the 'default value' functionality. For example, suppose that you wanted to make life easy for the folks that are going to use your application. One of the things you can do is to code in default values for your properties so that your users don't have to bother. For example, the following:

private String defaultValue;

...will inject ThisValue into defaultValue if a property called useDefault cannot be found. Taking this one step further, then specifying:

private String emptyValue;

...will inject an empty string into emptyValue.

If there’s a Spring suggestion box for minor, trivial improvements, then I’d like to suggest that you should be able to make properties optional, perhaps using something like this...

  @Value("${optional}" required=false)
private String optionalPropertyValue;

...as a convenience feature that might save someone some heart-ache and time in the future.


Michael Shkolnik said...

Thank you for this blog. I have posted an additional, arguably simpler solution on my blog

Lee said...

Thanks, Just what I needed to know! It would be nice if they would provide the optional option, I would prefer that the value defaulted to null but, I guess it kind of trivial.

Lee said...

Thanks, Just what I needed to know! It would be nice if they would provide the optional option, I would prefer that the value defaulted to null but, I guess it kind of trivial.