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 learn all the pieces by heart. So I decided to document the complete chain of beans that are required end to end for an application.

 <?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/>    <tx:annotation-driven /><!– This is a service layer bean that uses a DAO –>
<bean id=”WebETSManager” init-method=”init” class=”com.enthuware.webets.impl.SimpleWebETSManager”>
<constructor-arg index=”0″>
<value>app specific string value</value>
</constructor-arg>
<property name=”webETSDAO” ref=”WebETSDAO”/>
</bean><!– This is a DAO –>
<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>

<!– The following are two ways to configure the data source –>

<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>

<!–bean id=”dataSource” destroy-method=”close” class=”org.apache.commons.dbcp.BasicDataSource”>
<property name=”driverClassName” value=”com.mysql.jdbc.Driver”/>
<property name=”url” value=”jdbc:mysql://localhost/mydb”/>
<property name=”username” value=”userid”/>
<property name=”password” value=”pwd”/>
<property name=”maxIdle” value=”5″/>
<property name=”maxActive” value=”5″/>
<property name=”maxWait” value=”10000″/>
<property name=”validationQuery” value=”select 1″/>
<property name=”testOnBorrow” value=”false”/>
<property name=”testWhileIdle” value=”true”/>
<property name=”timeBetweenEvictionRunsMillis” value=”1200000″/>
<property name=”minEvictableIdleTimeMillis” value=”1800000″/>
<property name=”numTestsPerEvictionRun” value=”5″/>
<property name=”defaultAutoCommit” value=”false”/>
</bean–>

</beans>

The beans are self explanatory.

Advertisement

February 22, 2011 - Posted by | Java

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.