Results 1 to 3 of 3

Thread: Help with PropertyPlaceholderConfigurer

  1. #1
    Join Date
    Jul 2005
    Posts
    2

    Default 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. #2
    Join Date
    Sep 2005
    Location
    Vienna
    Posts
    44

    Default Re: Help with PropertyPlaceholderConfigurer

    Hi,

    Quote 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

  3. #3
    Join Date
    Jul 2005
    Posts
    2

    Default Re: Help with PropertyPlaceholderConfigurer

    Quote 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!
    Wendy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •