开发环境:
IDE:MyEclipse7.0
SSH:Struts1.2+Hibernate3.2+Spring2.0
整合方法:
1. 建立工程,添加Struts支持,与单独用Struts做开发的配置没什么不同,基本上就是一直“下一步”。
2. 添加Hibernate,当向导执行到配置SessionFactory时可跳过,因为SessionFactory最后由Spring负责生成。
3. 添加Spring,我选择的是2.0,当然也可以选择2.5,配置上大同小异。需要注意的是至少应引入一下四个库:Spring AOP Libraries、Spring Core Libraries、Spring Persistence Libraries和Spring Web Libraries,在接下来的步骤中不要忘了选中 “Create Spring SessionFactory”。
4 .Struts1.2和Spring2.0的整合
1)在web.xml中加入如下代码令服务器自动加载Spring
- < context-param >
- < param-name > contextConfigLocation</ param-name >
- < param-value > /WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</ param-value >
- </ context-param >
- < listener >
- < listener-class > org.springframework.web.context.ContextLoaderListener</ listener-class >
- </ listener >
2)当在struts-config.xml定义action时,需要将action的类型修改为
“org.springframework.web.struts.DelegatingActionProxy”
同时在Action的类中还需要提供所调用的业务对象的setter方法,让spring为其注入依赖的对象。
3)在spring的配置文件中定义action的bean,其中的name属性值必须与struts配置文件的path属性值相一致。
(建议将 action的scope属性值设置成"prototype",这样就避免了一些安全问题)
5. Spring2.0和Hibernate3.2的整合
1)所有的DAO类都必须继承HibernateDaoSupport基类,在这个类中可以获得Hibernate的各种核心接口(如Session、SessionFactory等),同时在配置时需要注入sessionFactory。
注:默认情况下Hibernate只有遇到运行时异常数据库操作才会回滚,普通异常不会回滚。
2)在Spring中配置SessionFactory,默认情况下MyEclipse已经为我们配置好了。
3)配置声明式事务,首先需要配置事务管理器
- < bean id ="transactionManager" class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
- < property name ="sessionFactory" >
- < ref local ="sessionFactory" />
- </ property >
- </ bean >
接下来需要配置事务管理的AOP
- <!--定义事务的传播特性-->
- < tx:advice id ="txAdvice" transaction-manager ="transactionManager" >
- < tx:attributes >
- < tx:method name ="add*" propagation ="REQUIRED" />
- < tx:method name ="delete*" propagation ="REQUIRED" />
- < tx:method name ="update*" propagation ="REQUIRED" />
- < tx:method name ="*" read-only ="true" />
- </ tx:attributes >
- </ tx:advice >
- <!--AOP的配置-->
- < aop:config >
- < aop:pointcut id ="managersMethod" expression ="execution(* com.test.manager.*.*(..))" />
- < aop:advisor advice-ref ="txAdvice" pointcut-ref ="managersMethod" />
- </ aop:config >
需要注意的是,默认情况下MyEclipse没有引入相关的xmlSchema,也没有定义相关的命名空间,所以我们需要手动配置
- xmlns ="http://www.springframework.org/schema/beans"
- xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop ="http://www.springframework.org/schema/aop"
- xmlns:tx ="http://www.springframework.org/schema/tx"
- xsi:schemaLocation ="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
5. OpenSessionInView的处理
Open Session In View提供了一种简便的方法,较好地解决了lazy loading问题。它有两种配置方式OpenSessionInViewInterceptor和OpenSessionInViewFilter(具 体参看SpringSide),功能相同,只是一个在web.xml配置,另一个在application.xml配置而已。
Open Session In View在request把session绑定到当前thread期间一直保持hibernate session在open状态,使session在request的整个期间都可以使用,如在View层里PO也可以lazy loading数据,如 ${ company.employees }。当View 层逻辑完成后,才会通过Filter的doFilter方法或Interceptor的postHandle方法自动关闭session。
其中spring提供了filter的处理方法,配置方法如下:
- < filter >
- < filter-name > hibernateFilter</ filter-name >
- < filter-class > org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</ filter-class >
- </ filter >
- < filter-mapping >
- < filter-name > hibernateFilter</ filter-name >
- < url-pattern > /*</ url-pattern >
- </ filter-mapping >
6. 对表单中中文的处理,spring中为我们提供了Encoding Filter,配置如下
- < filter >
- < filter-name > Spring character encoding filter</ filter-name >
- < filter-class > org.springframework.web.filter.CharacterEncodingFilter</ filter-class >
- < init-param >
- < param-name > encoding</ param-name >
- < param-value > GBK</ param-value >
- </ init-param >
- </ filter >
- < filter-mapping >
- < filter-name > Spring character encoding filter</ filter-name >
- < url-pattern > /*</ url-pattern >
- </ filter-mapping >
问题处理
在配置完成后,进行测试时可能会产生一些问题,如
java.lang.NoClassDefFoundError: org/objectweb/asm/CodeVisitor
该问题的解决方法如下:
去掉类路径上的关于Hibernate的3个lib
asm.jar
asm-attrs.jar
cglib-2.1.3.jar
加入Spring中的以下4个lib
asm-2.2.3.jar
asm-commons-2.2.3.jar
asm-util-2.2.3.jar
cglib-nodep-2.1_3.jar