Results 1 to 3 of 3

Thread: Passing properties as a constructor argument

  1. #1
    Join Date
    Feb 2012
    Posts
    2

    Default 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. #2

    Default

    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

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Have you read the reference guide? There is a sample in there which does exactly that.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

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