How to get values of username and password in database properties file and not hibernate.cfg.xml Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to get values of username and password in database properties file and not hibernate.cfg.xml without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Hi I am new to spring and hibernate I would like to use use the values of username and password from my database.properties file and not from my hibernate.cfg.xml. I do not know maybe I am doing it wrong somewhere. In my hibernate.cfg.xml file I have commented out username and password because it works when I supply it. I have also put my database.properties file in a different location from my project

database.driver=org.postgresql.Driver
database.url=jdbc:postgresql://localhost:5432/mzanzi-fm
database.user=postgres
database.password=1234
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update

This is my hibernate.cfg.xml file

<hibernate-configuration>
 <session-factory>
      <property  name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
      <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
      <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/mzanzi-fm</property>
      <property name="show_sql">true</property>
      <property name= "hbm2ddl.auto">update</property>

      <mapping class="com.mzanzi.admin.model.User"/>
      <mapping class="com.mzanzi.admin.model.Genres"/>
      <mapping class="com.mzanzi.admin.model.Album"/>
      <mapping class="com.mzanzi.admin.model.Artists"/>
      <mapping class="com.mzanzi.admin.model.Songs"/>
      <mapping class="com.mzanzi.admin.model.Usergroups"/>


   </session-factory>
 </hibernate-configuration>

This is my context file

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/mvc 
  http://www.springframework.org/schema/mvc/spring-mvc.xsd">


<context:property-placeholder location="file:C:UsersTumi KomaDocumentsresourcesdatabase.properties" />
<context:component-scan base-package="com.mzanzi.admin" />

<mvc:annotation-driven />
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix">
       <value>/WEB-INF/pages/</value> 
    </property>
    <property name="suffix">
       <value>.jsp</value>
    </property>
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}"/> 
    <property name="password" value="${database.password}"/>

</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />

    <property name="annotatedClasses">
        <list>
            <value>com.mzanzi.admin.model.User</value>
            <value>com.mzanzi.admin.model.Genres</value>
            <value>com.mzanzi.admin.model.Album</value>
            <value>com.mzanzi.admin.model.Artists</value>
            <value>com.mzanzi.admin.model.Songs</value>
            <value>com.mzanzi.admin.model.Usergroups</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>             
        </props>
    </property>
</bean>

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- max upload size in bytes -->
    <property name="maxUploadSize" value="20971520" /> <!-- 20MB -->

    <!-- max size of file in memory (in bytes) -->
    <property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->

</bean>

<bean id="hibernateTransactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

Any help will be greatly appreciated thank you

Answer

You can use property place holder to achieve this. Here is how your context file will look like

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="${app.jdbc.driverClassName}"
    p:url="${app.jdbc.url}" p:username="${app.jdbc.username}" p:password="${app.jdbc.password}"
    p:validationQuery="SELECT 1" />

And here is your datasource.properties file

app.jdbc.driverClassName=com.mysql.jdbc.Driver
app.jdbc.url=jdbc:mysql://localhost:3306/testdb
app.jdbc.username=root
app.jdbc.password=password

To achieve this add this in your config file (Assuming you have the file in the classpath)

        <context:property-placeholder
    location="classpath:datasource.properties,classpath:mailsender.properties,classpath:repository.properties" />

datasource.properties file corresponds to your database.properties file.

And this post might help you.

We are here to answer your question about How to get values of username and password in database properties file and not hibernate.cfg.xml - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji