Over a million developers have joined DZone.

Autowiring Using @Value and Optional Properties

DZone's Guide to

Autowiring Using @Value and Optional Properties

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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.


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

Download Modern Java EE Design Patterns: Building Scalable Architecture for Sustainable Enterprise Development.  Brought to you in partnership with Red Hat

java ,spring ,beans

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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


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

{{ parent.tldr }}

{{ parent.urlSource.name }}