
本站地址:http://www.bajiao123.com

JSF+Spring+Hibernate的实例讲解
<!-- Hibernate Transaction Manager Definition -->
<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<!-- Cached Catalog Service Definition -->
<bean id="catalogServiceTarget" class="catalog.model.service.impl.CachedCatalogServiceImpl" init-method="init">
<property name="catalogDao"><ref local="catalogDao"/></property>
</bean>
<!-- Transactional proxy for the Catalog Service -->
<bean id="catalogService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="target"><ref local="catalogServiceTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
Spring声明事务管理是在CatalogService. CatalogService 里面设置,它能实现不同CatalogDao。Spring创建并管理单体实例Catalogservice,不需要工厂。
现在,业务逻辑层准备好了,让我们将它与集成层整合。
Integration between Spring and Hibernate:下面是HibernateSessionFactory的配置:
<!-- Hibernate SessionFactory Definition -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>catalog/model/businessobject/Product.hbm.xml</value>
<value>catalog/model/businessobject/Category.hbm.xml</value>
<value>catalog/model/businessobject/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.hibernate.cache.HashtableCacheProvider</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
CatalogDao使用HibernateTemplate集成Hibernate和Spring.下面是HibernateTemplate的配置:
<!-- Hibernate Template Defintion -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate.HibernateTemplate">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
<property name="jdbcExceptionTranslator"><ref bean="jdbcExceptionTranslator"/></property>
</bean>
集成层
Hibernate使用一个XML配置文件去映射业务对象到关系型数据库。在JCatalog项目中,Product.hbm.xml表示Product业务对象的映射。Category.hbm.xml用于业务对象Category。配置文件和相应的业务对象在同样的目录下。下面是Product.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="catalog.model.businessobject">
<class name="Product" table="product">
<id name="id" column="ID" unsaved-value="null">
<generator class="assigned"/>
</id>
<property name="name" column="NAME" unique="true" not-null="true"/>
<property name="price" column="PRICE"/>
<property name="width" column="WIDTH"/>
<property name="height" column="height"/>
<property name="description" column="description"/>
<set name="categoryIds" table="product_category" cascade="all">
<key column="PRODUCT_ID"/>
<element column="CATEGORY_ID" type="string"/>
</set>
</class>
</hibernate-mapping>
CatalogDao通过Spring使用HibernateTemplate连接:
<!-- Catalog DAO Definition: Hibernate implementation -->
<bean id="catalogDao" class="catalog.model.dao.hibernate.CatalogDaoHibernateImpl">
<property name="hibernateTemplate"><ref bean="hibernateTemplate"/></property>
</bean>
结论
这篇文章介绍了怎样将JSF集成到Spring Framework和Hibernate,建立了一个真实的应用程序。这三种技术的联合提供了一个可靠的Web应用程序开发框架。一个多层体系结构应该做为Web应用程序的高级体系结构。JSF很适合MVC设计模式,能够被用于实现表示层。Spring框架能被用于业务逻辑层去管理业务对象,提供声明性事务管理和资源管理。Spring与Hibernate结合的很好。Hibernate是一个强有力的O/R映射框架,能够提供集成层的服务。
通过将Web应用程序划分成不同的层和面向接口编程,每一层的技术可以被取代。例如, 在表示层Struts能取代JSF
本站地址:http://www.bajiao123.com

