Hello guys I’m quite new at Web Development and I’m stuck on a problem here. I want my java program to connect to a database using Jdbs connector but instead of hard coding the database variables I want my program to read an xml file where the values are stored in beans. The lines I want to be read are stored in applicationContext.xml file as follows:
<bean id="ObjectMapperFactory" scope="singleton" class="someClass"/>
<bean id="UgcDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="username"><someUser</value></property>
<property name="password"><somePassword</value></property>
<property name="url"><value>jdbc:mysql://localhost:3306/dbName?autoReconnect=true</value></property>
<property name="defaultAutoCommit"><value>false</value></property>
<property name="maxActive"><value>20</value></property>
<property name="maxIdle"><value>3</value></property>
</bean>
And after reading it I want these variables to be stored in strings as such:
String url = "jdbc:mysql://localhost:3306/dbName"
String username = "someUser"
String password = "somePassword"
So what I’m looking for is a way to extract those 3 values, any suggestions? Much appreciated!
With Spring you can use the
@ValueAnnotation like so