Over a million developers have joined DZone.

Autowiring Using @Value and Optional Properties

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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:


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


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


optional=


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:

  @Value("${useDefault:ThisValue}")
  private String defaultValue;

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

  @Value("${emptyDefault:}")
  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.

 

From http://www.captaindebug.com/2012/01/autowiring-using-value-and-optional.html

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
java ,spring ,beans

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 }}