Spring @Value not resolving

Spent many hours trying to figure out why my @Value annotations were not being resolved. My setup was :

  • application-context.xml
  • app-servlet.xml

In application-context.xml I was reading a properties file using:

<context:property-placeholder location="..., classpath:application.properties, ..." />

Then I was attempting to set a variable using @Value.
After hours of searching, finally found some answers on Stack Overflow. The core issue seems to be scoping issues between root web context & servlet context.

actual processing of the @Value annotation is performed by a BeanPostProcessor….

BeanPostProcessor interfaces are scoped per-container. This is only relevant if you are using container hierarchies. If you define a BeanPostProcessor in one container, it will only do its work on the beans in that container. Beans that are defined in one container are not post-processed by a BeanPostProcessor in another container, even if both containers are part of the same hierarchy.

Also take note of the ‘order’ property, the lower the number the higher priority a PPC has. #1 is the first, #2 the second, etc… By default it has Integer.MAX_VALUE (it will be invoke last / the lowest possible priority).

Posted in Java, Spring

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: