写这些只是为了上班的时候拿来用。
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配置大概就是这些,可能还有一些不足,希望可以共同探讨,有错请指出,共同进步,谢谢