Case Study

Spring Configuration

Previous Next
The case study is wired together using three Spring configuration files and one properties file. hibernate.properties defines values needed to create a MySQL data source. hibernate.properties
## MySQL
hibernate.dialect org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://hostname/jpetstore
hibernate.connection.username username
hibernate.connection.password password
domainContext.xml configures a data source, a Hibernate session factory, and a transaction manager. domainContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
	"-//SPRING//DTD BEAN//EN"
	"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="classpath:hibernate.properties"/>
	</bean>
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName">
			<value>${hibernate.connection.driver_class}</value>
		</property>
		<property name="url">
			<value>${hibernate.connection.url}</value>
		</property>
		<property name="username">
			<value>${hibernate.connection.username}</value>
		</property>
		<property name="password">
			<value>${hibernate.connection.password}</value>
		</property>
	</bean>
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="mappingResources">
			<list>
				<value>Account.hbm.xml</value>
				<value>Category.hbm.xml</value>
				<value>Item.hbm.xml</value>
				<value>Order.hbm.xml</value>
				<value>Product.hbm.xml</value>
				<value>LineItem.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="max_fetch_depth">3</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.cache.use_second_level_cache">true</prop>
				<prop key="hibernate.cache.use_query_cache">true</prop>
				<prop key="hibernate.show_sql">false</prop>
			</props>
		</property>
		<property name="dataSource">
			<ref local="dataSource"/>
		</property>
	</bean>

	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>
	
</beans>
daoContext.xml configures each of the DAO classes with a session factory and Hibernate template. daoContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<import resource="classpath:domainContext.xml"/>
	
	<bean id="accountDao" class="org.springunit.framework.samples.jpetstore.dao.hibernate.AccountDaoHibernate">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
		<property name="hibernateTemplate">
			<bean class="org.springframework.orm.hibernate3.HibernateTemplate">
				<property name="sessionFactory">
					<ref bean="sessionFactory"/>
				</property>
			</bean>
		</property>
	</bean>
	
	<bean id="categoryDao" class="org.springunit.framework.samples.jpetstore.dao.hibernate.CategoryDaoHibernate">
		<!-- like accountDao -->
	</bean>
	
	<bean id="itemDao" class="org.springunit.framework.samples.jpetstore.dao.hibernate.ItemDaoHibernate">
		<!-- like accountDao -->
	</bean>
	
	<bean id="orderDao" class="org.springunit.framework.samples.jpetstore.dao.hibernate.OrderDaoHibernate">
		<!-- like accountDao -->
	</bean>
	
	<bean id="productDao" class="org.springunit.framework.samples.jpetstore.dao.hibernate.ProductDaoHibernate">
		<!-- like accountDao -->
	</bean>
	
</beans>

Finally, serviceContext.xml wires DAO classes to the petStoreService and administrationService, and uses Spring's AOP capability to equip the service methods with transactional behavior.

serviceContext.xml

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

	<import resource="classpath:daoContext.xml"/>
	
	<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager">
			<ref bean="transactionManager"/>
		</property>
		<property name="transactionAttributeSource">
			<value>
				org.springunit.framework.samples.jpetstore.domain.logic.AdministrationService.create*=PROPAGATION_REQUIRED
				org.springunit.framework.samples.jpetstore.domain.logic.AdministrationService.delete*=PROPAGATION_REQUIRED
				org.springunit.framework.samples.jpetstore.domain.logic.OrderService.get*=PROPAGATION_SUPPORTS,readOnly
				org.springunit.framework.samples.jpetstore.domain.logic.PetStoreFacade.insert*=PROPAGATION_REQUIRED
				org.springunit.framework.samples.jpetstore.domain.logic.PetStoreFacade.update*=PROPAGATION_REQUIRED
				org.springunit.framework.samples.jpetstore.domain.logic.PetStoreFacade.get*=PROPAGATION_SUPPORTS,readOnly
				org.springunit.framework.samples.jpetstore.domain.logic.PetStoreFacade.is*=PROPAGATION_SUPPORTS,readOnly
				org.springunit.framework.samples.jpetstore.domain.logic.PetStoreFacade.search*=PROPAGATION_SUPPORTS,readOnly
			</value>
		</property>
	</bean>
	
	<bean id="administrationService" class="org.springunit.framework.samples.jpetstore.domain.logic.AdministrationServiceImpl">
		<property name="accountDao">
			<ref bean="accountDao"/>
		</property>
		<property name="categoryDao">
			<ref bean="categoryDao"/>
		</property>
		<property name="itemDao">
			<ref bean="itemDao"/>
		</property>
		<property name="orderDao">
			<ref bean="orderDao"/>
		</property>
		<property name="productDao">
			<ref bean="productDao"/>
		</property>
	</bean>

	<bean id="petStoreService" class="org.springunit.framework.samples.jpetstore.domain.logic.PetStoreImpl">
		<property name="accountDao">
			<ref bean="accountDao"/>
		</property>
		<property name="categoryDao">
			<ref bean="categoryDao"/>
		</property>
		<property name="itemDao">
			<ref bean="itemDao"/>
		</property>
		<property name="orderDao">
			<ref bean="orderDao"/>
		</property>
		<property name="productDao">
			<ref bean="productDao"/>
		</property>
	</bean>

	<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="interceptorNames">
			<value>transactionInterceptor</value>
		</property>
		<property name="beanNames">
			<list>
				<idref local="administrationService"/>
				<idref local="petStoreService"/>
			</list>
		</property>
	</bean>
	
</beans>

Previous Next