Results 1 to 6 of 6

Thread: Loading a Properties object in the application context

  1. #1

    Default Loading a Properties object in the application context

    I need to inject a Properties object into several classes. Because a Properties object must be "loaded" I don't see a way to do this via the usual Spring applicationContext instantiation and assignment.

    I was thinking of overloading Properties and providing a constructor that called load but as I thought about this I felt like this situation must have been dealt with before.

    Is there a Spring provided way to do this?

    Thanks,
    -=bill

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Doesn't PropertiesFactoryBean do everything you want?
    http://www.springframework.org/docs/...ctoryBean.html

  3. #3

    Default

    Karldmore,

    I looked at the PropertiesFactorBean class before posting and I could not see how it could do what I want. Can you point me to an example?

    What confuses me about PropertiesFactoryBean is that there is no load() method that takes the name of a properties file on the classpath. Does this class load the properties some other way?

    Thanks for the reply,
    -=bill

  4. #4

    Default

    Karldmore,
    Nevermind! I searched for an example with Google and found a good one. I think my basic problem with Spring is that Spring makes everything so simple and I'm always trying to make things more difficult.

    Thanks again for the reply,
    -=bill

  5. #5
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    You just need to inject the location, depending on what you set for this it could be a classpath location.
    http://www.springframework.org/docs/...e.io.Resource)
    http://www.springframework.org/docs/...resources.html

  6. #6
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    If you are using Spring 2.x, you can even use the 'util' namespace as a shortcut for the factory bean, such as:
    Code:
    <util:properties id="emails" location="/WEB-INF/emails.properties"/>
    
    <bean id="mailService" class="xyz.MailSenderImpl">
       <property name="emailMap" ref="emails"/>
    </bean>

Posting Permissions

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