Announcement Announcement Module
Collapse
No announcement yet.
Passing properties as a constructor argument Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing properties as a constructor argument

    Hello,
    I am looking for a way to convert a properties file into a java Properties class and pass it to a constructor, or to a setter (property). How do I go about it? Most places I have searched talk of PropertyPlaceholderConfigurer, and use something like
    Code:
    value="${<some_key>}"
    or variations thereof.

    There are two references I have found on the web which roughly do what I want, by extending thePropertyPlaceholderConfigurer class, which may be found here and here.

    I am wondering if there is a built-in and/or cleaner way to achieve this.

  • #2
    Hi,

    I need a little more information to understand the problem you're trying to solve. What, specifically, does the following approach not address for you? Under the hood, Spring is creating a BasicDataSource instance and wiring it into the MyDaoJDBCImpl class via a constructor. It can be wired into other classes using setter or constructor injection, which sounds like what you're trying to achieve. Where does this fall short so I can try to help you come up with a solution?

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${driver}" />
    <property name="username" value="${username}" />
    <property name="password" value="${password}" />
    <property name="url" value="${url}" />
    </bean>

    <bean class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="location" value="file:datasource.properties" />
    </bean>

    <bean id="myDataAccessObject" class="org.company.dao.MyDaoJDBCImpl">
    <constructor-arg ref="myDataSource" />
    </bean>


    Thanks,
    Travis

    Comment


    • #3
      Have you read the reference guide? There is a sample in there which does exactly that.

      Comment

      Working...
      X