I have spring application and using property file want to read the values from the PropertyPlaceholderConfigurer. Here datasource is given as Id.
I want to read the values of the datasource property jdbc.driverClassName value using java code.
Scenario should be: 1st bean will be executed. It will load the data from the jdbc.property file. All the values in the datasource should be read from the java code.
How to read the values from the Java code for PropertyPlaceholderConfigurer(datasource)?
Given the executed scenario below :
Create a properties file (database.properties), include your database details, put it into your project class path.
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mkyongjava
jdbc.username=root
jdbc.password=password
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>database.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="{jdbc.url}" />
<property name="username" value="{jdbc.username}" />
<property name="password" value="{jdbc.password}" />
</bean>
Please help me out using java code to read the data from the given datasource?
Or any useful link from where i can find out the resolution.
There are two simple options:
Get a reference to the application context (if you instantiated it yourself, that should be easy- if you initialized it using say
web.xml, you might be able to retrieve it usingorg.springframework.web.context.support.WebApplicationContextUtils). Then obtain the bean usingBeanContext<T>.T getBean(String name, Class<T> requiredType), retrieve the properties usingDriverManagerDataSource‘s getters.Inject the same values into one of your own beans; Spring will put the same values for you