本文介绍Spring与Hibernate 、Struts2等框架的整合
一、Spring整合Hibernate
为什么要整合Hibernate?
1.使用Spring的IOC功能管理Hibernate的SessionFactory对象
对应的Bean:LocalSessionFactoryBean
配置方法如下:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
2.使用Spring管理Hibernate的Session对象
对应的Bean:HibernateTemplate
配置方法如下:
<bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
3.使用Spring的功能实现声明式的事务管理
<bean id="tm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"> <ref bean="sessionFactory"/> </property>
</bean>
<bean id="ti" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager"><ref bean="tm"/></property>
<property name="transactionAttributes">
<props>
<!-- key:方法名称 -->
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
4.Spring的AOP配置:
<aop:config>
<aop:pointcut id="txMethod" expression="execution(* org.zhy.demo.dao.impl.*DaoImpl.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txMethod"/>
</aop:config>
<!-- AOP切面声明事务管理 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" /> <!-- 支持当前事务,如果执行到save开头的任何方法时没有事务则开启一个事务 这是最常见的方式-->
<tx:method name="update*" propagation="REQUIRED" /><!-- 支持当前事务,如果执行到update开头的任何方法时没有事务则开启一个事务 这是最常见的方式-->
<tx:method name="add*" propagation="REQUIRED" /><!-- 支持当前事务,如果执行到add开头的任何方法时没有事务则开启一个事务 这是最常见的方式-->
<tx:method name="delete*" propagation="REQUIRED" /><!-- 支持当前事务,如果执行到delete开头的任何方法时没有事务则开启一个事务 这是最常见的方式-->
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <!-- 支持当前事务,如果当前没有事务,就以非事务方式执行。只读 -->
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/><!-- 支持当前事务,如果当前没有事务,就以非事务方式执行。只读 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
以上是典型的Spring的AOP的配置。
二、Spring与Struts2的整合
注意:除了要导入相应的框架的jar包,还要导入commons-logging和struts2-spring-plugin包。
1.web.xml的配置
关于struts2的配置:(既然有Struts2,核心拦截器是必不可少的)
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.通过配置ContextLoaderListener监听器,使容器启动时,自动加载applicationContext的配置(spring).
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
配置contextConfigLocation参数改变配置文件的路径
<context-param>
<param-name>contextConfigLocation</param-name>
//指定要加载的spring配置文件的路径
<param-value>WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
3.编写struts2的配置文件
首先,要告知struts2运行时要使用spring来创建对象
在struts2中加入以下配置。
<constant name='"struts.objectFactory" value="spring" />
其次,在Spring配置文件中添加Action对应的Bean,并为它注入相应的Service对应的Bean
再次,在struts的配置文件中配置对应的Action,包括ActionName、ActionClass及对应的Result
至此,Spring整合Hibernate和Struts2整合Spring完毕。