Richie
Richie

Reputation: 5199

spring trying to load environment variable

I have a question about trying to inject an environment variable into my spring mvc controller.

I have an environment variable as follows...

POS_MANAGER_SERVER_REPORTING=myserver

In my spring application context I have ...

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:systemPropertiesModeName="SYSTEM_PROPERTIES_MODE_OVERRIDE"
    p:searchSystemEnvironment="true"/>

<bean name="posManagerController"
    class="com.mycompany.reporting.controller.PosManagerController"
    p:posManagerServer="${POS_MANAGER_SERVER_REPORTING}" />     

When I do not define the system variable in websphere the application does not start up properly. When I add the variable it does. But for some reason the value being injected into my controller is null.

Can someone help me with what might be going wrong here? i.e. Why the environment variable is not loaded properly?

Just to give you some more information, the Controller is loaded in the child context (-servlet.xml) using the @Controller annotation but in the parent context (applicationContext.xml) I also have the controller defined as appeared above. My understanding is that spring is smart enough to be able to handle this. I've listed this information just in case this might be the cause of the issue.

thanks

Upvotes: 0

Views: 312

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

Quote from documentation:

Also, BeanFactoryPostProcessors are scoped per-container. This is only relevant if you are using container hierarchies. If you define a BeanFactoryPostProcessor in one container, it will only be applied to the bean definitions in that container. Bean definitions in one container will not be post-processed by BeanFactoryPostProcessors in another container, even if both containers are part of the same hierarchy.

So, you need a PropertyPlaceholderConfigurer in -servlet.xml file to make it work.

Upvotes: 1

Related Questions