I am trying to load properties from a database. I have succeeded in this. But now the problem is that for the dataSource bean I want to use placeholders. See applicationProperties.xml, then you can get a view:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driverClassName}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </bean> <bean class="PropFromDB.PropFromDB.PropertiesUtil" > <property name="propertiesArray"> <list> <ref bean="propertiesFromDB" /> </list> </property> </bean> <bean id="propertiesFromDB" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="properties" ref="commonsConfigurationFactoryBean" /> </bean> <bean id="commonsConfigurationFactoryBean" class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean"> <constructor-arg ref="databaseConfiguration"></constructor-arg> </bean> <bean name="databaseConfiguration" class="org.apache.commons.configuration.DatabaseConfiguration"> <constructor-arg index="0" ref="dataSource" /> <constructor-arg index="1" value="properties" /> <constructor-arg index="2" value="key" /> <constructor-arg index="3" value="value" /> </bean>
The above code is designed to load properties from the database. now, as you can see for the dataSource bean, some placeholders are used. So I included this line at the top:
<context:property-placeholder location="classpath:databaseForConfiguration.properties"/>
databaseForConfiguration.properties contains all the necessary properties in the classpath :
driverClassName=org.postgresql.Driver url=jdbc:postgresql://localhost:5432/mydb username=user password=pass
But when I try to execute, I get the following exception:
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${driverClassName}] at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:108) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:62) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1489) ... 60 more
and similar for {url} , {username} , {password} .
As you can easily understand, to initialize a propertyUtil bean, you must first initialize the dataSource bean. And for the dataSource bean there must be local property placeholders. Which in this case does not receive.
I want both of these things , loading placeholders from local files and also from the database .
Someone please help me solve this problem.
early.
spring
Jaydeep
source share