Hanumant’s Java Workshop

Turbo Charged Java Development!

Configuring DataSource, EntityManager, TransactionManager, DAO, and Service beans in a Spring Config File!

If you are learning JPA, don’t forget to check out this Sample Application

The title of this post looks like a SEO magnet, but this is truely what this post is about. I encounter this kind of configuration requirements often but not as often as would make me remember all the pieces by heart. So I decided to document the complete chain of beans that are required end to end for an application that is not deployed in an EJB Container.

 

<?xml version="1.0" encoding="windows-1252"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/tx
                    http://www.springframework.org/schema/tx/spring-tx.xsd">

        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
        <tx:annotation-driven />
       
        <!-- This is a service layer bean that uses a DAO -->

    <bean id=”WebETSManager” class=”com.enthuware.webets.impl.SimpleWebETSManager”  init-method=”init”>
        <constructor-arg index=”0″>
            <value>some value</value>
        </constructor-arg>
        <property name=”webETSDAO” ref=”WebETSDAO”/>
    </bean>

    <bean id=”WebETSDAO” class=”com.enthuware.webets.impl.SpringJPAWebETSDAO”>
        <property name=”entityManagerFactory” ref=”myEmf”/>
    </bean>

    <bean id=”transactionManager” class=”org.springframework.orm.jpa.JpaTransactionManager”>
        <property name=”entityManagerFactory” ref=”myEmf”/>
        <property name=”dataSource” ref=”dataSource”/>
    </bean>

    <bean id=”myEmf” class=”org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean”>
        <property name=”dataSource” ref=”dataSource”/>

        <property name=”jpaVendorAdapter”>
            <bean class=”org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter”>
                <property name=”showSql” value=”false” />
                <property name=”generateDdl” value=”true” />
                <property name=”databasePlatform” value=”org.hibernate.dialect.MySQLDialect”/>
            </bean>
        </property>
        <property name=”persistenceUnitName” value=”webetsPU”/>
        <property name=”persistenceUnitManager”>
            <bean class=”org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager”>
                <property name=”defaultDataSource” ref=”dataSource”></property>
            </bean>
        </property>
        <property name=”loadTimeWeaver”>
            <bean class=”org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver”/>
        </property>

    </bean>

    
         <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
          <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
          <property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
          <property name="username" value="userid"/>
          <property name="password" value="pwd"/>
          <property name="initialSize" value="2"/>
          <property name="maxActive" value="5"/>
          <property name="testWhileIdle" value="true"/>
          <property name="validationQuery" value="SELECT 1"/>
          <property name="validationInterval" value="120000"/>
          <property name="timeBetweenEvictionRunsMillis" value="120000"/>
        </bean>
  </beans>

 

The beans are self explanatory.

February 22, 2011 Posted by | Java | Leave a comment