一、基于Spring与Hibernate整合(Spring 2.x + Hibernate 3.x)
1、配置数据源:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="initialSize" value="10" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="10" />
<property name="minIdle" value="10" />
</bean>
2、配置SessionFactory
说明:
不使用hibernate.cfg.xml配置文件
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>pojo/User.hbm.xml</value>
</list>
</property>
</bean>
使用hibernate.cfg.xml配置文件,在类路径下:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
3、配置事务管理器:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
4、配置事务的传播特性
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" propagation="NOT_SUPPORTED" />
</tx:attributes>
-->
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
5、配置参与事务的方法
<aop:config>
<aop:pointcut id="txMethods" expression="execution(* 包名..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txMethods" />
</aop:config>
二、基于Spring与Struts整合(Spring 2.x + Struts 1.x)
1、向应用程序作用域添加Spring配置文件,多个文件用逗号分隔
在类路径中
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
在WEB-INF目录下
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml</param-value>
</context-param>
以上两种情况需要添加监听器:
向WEB应用添加Spring提供的监听器,用来实例化Spring容器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2、第一种方案:
采用依赖查找:
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
3、第二种方案:
采用依赖注入
配置Struts-config文件的第一种方式:
在struts-config.xml文件中配置Action
<action>标签中的type属性修改为org.springframework.web.struts.DelegatingActionProxy代理类
在Spring容器中配置name为<action>标签中的path路径的值,class为Action的类全名
配置Struts-config文件的第二种方式:
将<action>标签中的type删除,或属性值不变,同时添加一个控制器:
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" />
</controller>
在Spring配置Action实例
<bean>标签中使用name属性,name属性值为struts-config.xml文件中<action>标签的path属性值,将scope设置为prototype
配置编码集的过滤器:
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
配置Session的状态,允许使用lazy策略
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>