Integrating Business Logic Tier and Integration Tier Components

In the business logic tier web have created business objects, business services and now we are going to integrate them using Spring framework.

Integrating Business Logic Tier and Integration Tier Components

Integrating Business Logic Tier and Integration Tier Components

        

In the business logic tier web have created business objects, business services and now we are going to integrate them using Spring framework.

Business Objects

In this example we have only one business object the User.

Integration tier components

Hibernate maps the business objects to database using XML configuration file. Following file (User.hbm.xml) is used to map User object with the database.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping auto-import="true" default-lazy="false">

<class 
name="net.roseindia.dao.hibernate.User" 
table="users"
>

<id
name="userId"
type="java.lang.Integer"
column="userId"
>
<generator class="increment" />
</id>

<property
name="userName"
type="java.lang.String"
column="userName"
length="20"
/>
<property
name="userPassword"
type="java.lang.String"
column="userPassword"
length="11"
/>

<property
name="userEmail"
type="java.lang.String"
column="userEmail"
length="30"
/>
<property
name="userAddress"
type="java.lang.String"
column="userAddress"
length="30"
/>

<!-- Associations -->


</class>
</hibernate-mapping>

The HibernateSpringDAO is wired with HibernateTemplate by Spring:

<bean id="HibernateSpringDaoTarget" class="net.roseindia.dao.HibernateSpringDAOImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>

Here is the full code of applicationContext-hibernate.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<!--
- Application context definition for MyFaces, Hibernate and Spring Integration application.
-->
<beans>

<!-- ========================= RESOURCE DEFINITIONS ========================= -->

<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>/WEB-INF/jdbc.properties</value></property>
</bean>

<!-- Local DataSource that works in any environment -->
<!-- Note that DriverManagerDataSource does not pool; it is not intended for production -->

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

</bean>


<!-- JNDI DataSource for J2EE environments -->
<!--
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>java:comp/env/jdbc/roseindiaDB_local</value></property>
</bean>
-->
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>/net/roseindia/dao/hibernate/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>


<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>


<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->

<!--
Data access object: Hibernate implementation.
-->

<bean id="HibernateSpringDaoTarget" class="net.roseindia.dao.HibernateSpringDAOImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>

<!--
- Transactional proxy for Application's central data access object.
-
- Defines specific transaction attributes with "readOnly" markers,
- which is an optimization that is particularly valuable with Hibernate
- (to suppress unnecessary flush attempts for read-only operations).
-
- Note that in a real-life app with multiple transaction proxies,
- you will probably want to use parent and child bean definitions
- as described in the manual, to reduce duplication.
-->

<bean id="SpringHibernateDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="target"><ref local="HibernateSpringDaoTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

</beans>

This application is supported with full free code. In the next section we will show you how you can download and install it on your computer.