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

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

    Hi,

    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.
    Code:
    @Configuration
    @ImportResource("spring-config.xml")
    public class AppConfig
    {
    	
    	private @Value("${slnp.serviceEndpointUrl}") String slnpServiceEndpointUrl;
    }
    app.properties
    Code:
    slnp.serviceEndpointUrl=http://url.com
    spring-config.xml
    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           	http://www.springframework.org/schema/context
           	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
           	
           	<context:property-placeholder location="/WEB-INF/properties/app.properties"/>
    </beans>
    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?

    Comment


    • #3
      Hi,

      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.

      Comment

      Working...
      X