Wednesday, 26 October 2016

3. Hibernate Spring Properties File Configuration

In previous tutorials, we covered how to integrate Spring with Hibernate and perform few of very basic operations for CRUD. We created DataSource and SessionFactory using 'org.springframework.jdbc.datasource.DriverManagerDataSource' and 'org.springframework.orm.hibernate4.LocalSessionFactoryBean' respectively.

However there was issue that all required properties to create these beans were hard coded in Spring config XML file. We do not this hard coding and want to move these values to properties file so we can better manage them.

In this tutorial, we will see how we can do this.

To start with, create properties file with name 'hibernate.properties' in src/main/resources folder.

hibernate.properties

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/rmsystem
jdbc.user=postgres
jdbc.pass=password
 
hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
hibernate.current_session_context_class=thread
hibernate.cache.provider_class=org.hibernate.cache.internal.NoCacheProvider
hibernate.autocommit=false
hibernate.format_sql=true

We moved all properties from Spring.xml file to hibernate.properties file as above.

Now next task is to configure Spring to read this properties file and give keys of property instead of values directly while creating beans. Add below in your spring.xml file to configure properties file in Spring.

<context:property-placeholder location="classpath:hibernate.properties" />

as hibernate.properties file is on classpath (everything we put inside resources folder by default is on classpath) we mentioned location as "classpath:hibernate.properties"

Once that's done, we need to replace all hard coded properties with keys from properties file. We mention this like ${KEY} and spring takes care of replacing those with respective values at Spring context start up.

Spring.xml

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="UTF-8"?>
<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:jee="http://www.springframework.org/schema/jee" xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

 <context:component-scan base-package="in.blogspot.ashish4java.employeesystem" />

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

 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="packagesToScan" value="in.blogspot.ashish4java.employeesystem.model"></property>
  <property name="dataSource" ref="myDataSource" />
  <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>
    <prop key="current_session_context_class">${hibernate.current_session_context_class}</prop>
    <prop key="cache.provider_class">${hibernate.cache.provider_class}</prop>
    <prop key="hibernate.autocommit">${hibernate.autocommit}</prop>
    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
   </props>
  </property>
 </bean>

 <context:property-placeholder location="classpath:hibernate.properties" />

</beans>

Run MainClient class and you will see property values are picked up properly by Spring and there shouldn't be any difference during runtime.

Another issue which you must have noticed with using DriverManagerDataSource is you need to provide driver class name, database url along with username/password. Its not idea to store store password in properties file also as it has security risk and also issue like what if database password changes. Do we need to redeploy complete application again with changed password?

SO this DriverManagerDataSource is not very convenient to use. Spring provides option of using datasource configured in your application server using JNDI lookup with help of 'org.springframework.jndi.JndiObjectFactoryBean'.

We will see how to do this in future tutorial.

No comments:

Post a Comment