Announcement Announcement Module
Collapse
No announcement yet.
Confusion on how environment/property sources work in 3.1 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Confusion on how environment/property sources work in 3.1

    I'm trying to get up to speed on the new concepts of environment and propertysources introduced in spring 3.1.

    I've been working on a small sample application to walk through the process and I'm getting some behavior I don't expect.

    I thought that the default standard enviornment that is used will register two property sources into the environment -- one to look at the system environment and one to to look at java properties.

    I *thought* this means that I can define an application context bean def file that doesn't define any PropertyPlaceHolderConfigurers or PropertySources, and any properties references in my bean file will get resolved against the standard environment. This doesn't seem to be the case, since I start tomcat with the referenced system property defined yet it does not get resolved.

    Here's my quick example --

    I have a simple applicationContext.xml file with one bean definition --

    <bean id="generator" class="sample.GreetingsGeneratorImpl">
    <property name="greeting" value="${sample.greeting}"/>
    </bean>

    I have a very basic web.xml --

    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>

    I've written a simple spring mvc controller that dependency injects this bean and uses it to write a simple message.

    I make sure to startup tomcat with -Dsample.greeting set to something.

    However, the message that gets displayed is:

    hello from ${sample.greeting}

    It seems the only way I can get the standardEnvironment to be queried during property resolve time is to register another bean using this convention:


    <contextroperty-placeholder location="classpath://dummy.properties"/>

    My dummy.properties file is empty.

    When I run with this application, the sample.greeting is properly resolved (from system property property source), and my message becomes:

    hello from system properties

    In summary, I guess my assumption was that the user would not have to define any bean-post processors in their bean xml files and in that case, they would defer to whatever property sources were defined in the environment -- either the standard default environment, or what might have been done to add additional property sources in the web.xml.

    Since it doesn't appear to work this way, it seems my assumption is incorrect? Or is there some step I'm missing to accomplish my goal?

    Thanks.

    Doug

  • #2
    Your assumption is indeed incorrect. However instead of using placeholders you could use SpEL to replace the properties or simply define a <contextroperty-placeholder /> (without the location attribute). Spring still needs a beanfactorypostprocessor to replace the values that still hasn't changed.

    Comment

    Working...
    X