Announcement Announcement Module
No announcement yet.
@Value always null. Does not evaluate to property value Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Value always null. Does not evaluate to property value


    I am setting up a webapp with a annotation based spring configuration.
    Component scanning is initialized through AnnotationConfigWebApplicationContext.

    Everything seems to work just fine except for one thing. I am not able to have some field values set to a property value defined in some file.
    I followed the example in the documentation Combining Java and XML configuration.

    My properties file is found and it seems to be read as well. When I reference a property name not defined in my file an exception is thrown. So far I know my properties are found. But the value is not assigned to the fields in the configuration classes I wrote. It always evaluates to null.
    public class AppConfig
    	private @Value("${slnp.serviceEndpointUrl}") String slnpServiceEndpointUrl;
    <?xml version="1.0" encoding="UTF-8" ?>
    <beans xmlns=""
           	<context:property-placeholder location="/WEB-INF/properties/"/>
    Does someone know what I am missing here?

  • #2
    Hi everybody,

    I have a more precise picture of what is going on.
    Spring initializes the application context in 2 steps. First all defined beans are created and cached. If I understand this correctly this is done to fulfill dependencies. During this phase properties annotated with @Value are not set yet.

    In a second step the values are set and those beans are created again now with the evaluated property values.

    My problem is now that some of my beans depend on constructor parameters or properties which are validated during instantiation or injection into another bean. Since @Value is not processed in the first step exceptions are thrown and the context fails to initialize.

    Is there a way to solve this except for catching those exceptions?


    • #3

      I figured out how to make things work. If I instantiate my configuration classes with preset field values that meet validation requirements context initialization runs smoothly. And all annotated fields seem to be reliably overwritten.

      I am still a little bit confused by this behavior. But at least it seams to work.