寫這些隻是為了上班的時候拿來用。
S2SH 3者整合的用意是希望将Struts2 Action和Hibernate SessionFactory的對象建立工作交給Spring來管理,因為Spring對所有對象進行管理後,我們可以利用Spring AOP特性為程式增加新的行為
1.struts2和spring的整合
Struts2 與 Spring 整合的步驟:
a. web.xml 配置struts2核心過濾器
<pre name="code" class="html"><filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
b.在web.xml中增加spring的監聽器配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
c. 建立src/struts.xml檔案
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.objectFactory" value="spring"></constant>
<!--去spring中擷取struts的對象,如果使用s2s的話這句應該是必須要的-->
<package name="" extends="struts-default" namespace="/">
</package>
</struts>
d.建立src/applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
</beans>
2.關于action的使用問題有兩種方式(我自己知道的,至于還有木有我也不清楚)
- 注解:@controller或者是@Component(這個是所有受Spring管理元件的通用形式,即service也可以使用它)
-
配置:在applicationContext檔案中定義 SampleAction
<bean name="sampleAction" class="com.lyh.s2sh.action.SampleAction"></bean>
- 在struts.xml中增加配置,但是action中class不再是具體類名了,而是spring bean的name (sampleAction)
3.Hibernate 與Spring 整合的步驟
a.在applicationContext中增加dataSource配置
<bean name="ds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="****"></property>
<property name="password" value="****"></property>
</bean>
b. 在applicationContext檔案中,增加sessionFactory對象的配置
<bean name="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<pre name="code" class="html"> <!--可以添加自己需要的功能,如hibernate自動生成表-->
</props> </property> <property name="mappingResources"> <list> <!-- 這裡書寫需要引用的hbm.xml <value>com/lyh/s2sh/entity/customer.hbm.xml</value> <value>com/lyh/s2sh/entity/customer1.hbm.xml</value> <value>com/lyh/s2sh/entity/customer2.hbm.xml</value> --> </list> </property> </bean>
c.對service和dao的操作也是上面的兩種方法 注解@service和@Repository,配置dao的話一定要加上sessionFactory的引用
如:
<bean name="cDAO" class="com.lyh.s2sh.dao.CustomerDAO">
<property name="sessionFactory" ref="sf"></property>
</bean>
注:使用注解一定要在applicationContext.xml中配置
<context:component-scan base-package="com.lyh.lesta"></context:component-scan>
這句話是掃描包下所有可被IOC容器裝載的類(注解@Service @Controller @Compnent @Repository)
d.配置聲明式事務
bean name="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sf"></property>
</bean>
<!--未使用注解-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="del*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txpc" expression="execution(* com.lyh.s2sh.service.*Service.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txpc"/>
</aop:config>
<!-- 事務管理器 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds"></property>
</bean>
<!-- 通知Spring使用注解方式配置聲明式事務 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
s2sh配置大概就是這些,可能還有一些不足,希望可以共同探讨,有錯請指出,共同進步,謝謝