Announcement Announcement Module
Collapse
No announcement yet.
Help with PropertyPlaceholderConfigurer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with PropertyPlaceholderConfigurer

    I don't seem to have access to properties through PropertyPlaceholderConfigurer. I'm working with spring-1.2.4.jar and from the example in chapter 8 of Better, Faster, Lighter Java. (With some modifications as, for example, XmlBeanFactory wouldn't take an InputStream.)

    (While I've looked at a fair number of examples and articles, this is the first time I've tried to write anything using Spring. So please be gentle.)

    I have:
    Code:
     daoContext.xml:
            <bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location">
                <value>classpath&#58;u2.properties</value>
            </property>
        </bean>
    
        <bean id="uniSession"
            class="net.wsmoak.dao.spring.UniSessionBean">
            <property name="userName"><value>$&#123;USER_NAME&#125;</value></property>
        </bean>
    
    u2.properties&#58;
    USER_NAME=wsmoak
    Then this code prints ${USER_NAME} rather than wsmoak:

    Code:
            ClassPathResource resource = new ClassPathResource&#40; "daoContext.xml" &#41;;
            XmlBeanFactory beanFactory = new XmlBeanFactory&#40; resource &#41;;
            UniSessionBean uSession = &#40;UniSessionBean&#41; beanFactory.getBean&#40;"uniSession"&#41;;
            System.out.println&#40; uSession.getUserName&#40;&#41; &#41;;
    So it is "working" in that it's creating the UniSessionBean instance and calling setUserName, but it's not using the property value.

    The u2.properties file is sitting in target/classes right beside daoContext.xml, so if it can see one, it should be able to see the other.

    What am I missing? Thanks in advance for your help.

    --
    Wendy

  • #2
    Re: Help with PropertyPlaceholderConfigurer

    Hi,

    Originally posted by wsmoak
    So please be gentle.
    OK - :-).

    Concerning your problem: a XmlBeanFactory is not powerful enough for your purpose. You need (for example) a ClassPathXmlApplicationContext - see below.

    Code:
    ApplicationContext context = new ClassPathXmlApplicationContext&#40;"daoContext.xml"&#41;;
    UniSessionBean uSession = &#40;UniSessionBean&#41; context.getBean&#40;"uniSession"&#41;;
    System.out.println&#40;uSession.getUserName&#40;&#41;&#41;;
    And now it is magic...

    Erik

    Comment


    • #3
      Re: Help with PropertyPlaceholderConfigurer

      Originally posted by ErikFK
      And now it is magic...
      Magic, indeed. That just took a 150 line class down to... two lines.

      Well, not quite. There is some failover logic in the original class that I'll have to figure out how to accomplish, but all in good time.

      Thanks for your help!

      Comment

      Working...
      X