-
Feb 28th, 2012, 12:42 PM
#1
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.
-
Feb 28th, 2012, 11:23 PM
#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
-
Feb 29th, 2012, 01:43 AM
#3
Have you read the reference guide? There is a sample in there which does exactly that.
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
-
Forum Rules