java - how can i get database password at runtime before connecting to the database -


in applicationcontext.xml have :

       <bean id="datasource"     class="org.springframework.jdbc.datasource.drivermanagerdatasource">     <property name="driverclassname" value="com.mysql.jdbc.driver" />     <property name="url" value="jdbc:mysql://localhost:3306/dashboardsupervisor" />     <property name="username" value="root" />     <property name="password" value="1234" /> </bean> 

here connecting database :

              applicationcontext ctx = new classpathxmlapplicationcontext(         "applicationcontext.xml");         mysqlrdbhelper rdbhelper = (mysqlrdbhelper)                       ctx.getbean("managersupervisor"); 

what want not read password "1234" applicationcontext.xml , read properties file in local drive . running on different machines , every 1 have different passwords.

can achieve .

thanks

yes, can, , key springs propertyplaceholderconfigurer.

for example, create file on file system called database.properties, containing password (note, can add more settings file, username, jdbc url, etc).

jdbc.password=1234 

next, need declare propertyplaceholderconfigurer bean , point database.properties file:

<bean class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">     <property name="location">         <value>path/to/database.properties</value>     </property> </bean> 

note path interpreted classpath resource, unless prefixed file:.

finally, replace configuration of datasource bean: replace

<property name="password" value="1234" /> 

with

<property name="password" value="${jdbc.password}" /> 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -