本人接触SSH2项目时间短暂,所以在整合方面遇到了一些麻烦,下面将分享一下我的整合方案。
首先说一下Spring2 与 Hibernate3:
原理是省掉hibernate自带的sessionFactory 和dataSource 让spring来接管
因些步骤如下:
1.去掉hibernate的hibernate.hbm.xml文件
2.在spring的ApplicationContext.xml中加入如下代码,例如:
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <property name="url">
- <value>jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8</value>
- </property>
- <property name="username">
- <value>root</value>
- </property>
- <property name="password">
- <value>demo</value>
- </property>
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref local="dataSource" />
- </property>
- <property name="mappingResources">
- <list>
- <value>com/demo/ssh2/hibernate/beans/Person.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.cache.use_query_cache">true</prop>
- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
- </props>
- </property>
- </bean>
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref local="sessionFactory" />
- </property>
- </bean>
最后可以再加上spring的声明式事务
- <bean id="PersonDAOProxy"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- <property name="transactionManager">
- <ref bean="transactionManager" />
- </property>
- <property name="target">
- <ref local="personDAO" />
- </property>
- <property name="transactionAttributes">
- <props>
- <prop key="insert*">PROPAGATION_REQUIRED</prop>
- <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
- </props>
- </property>
- </bean>
还可以设置不同的TransactionInterceptor来得到更多的管理细节。如:
- <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <property name="transactionManager" ref="transactionManager"></property>
- <property name="transactionAttributes">
- <props>
- <prop key="save*">PROPAGATION_REQUIRED</prop>
- <prop key="add*">PROPAGATION_REQUIRED</prop>
- <prop key="set*">PROPAGATION_REQUIRED</prop>
- <prop key="update*">PROPAGATION_REQUIRED</prop>
- <prop key="delete*">PROPAGATION_REQUIRED</prop>
- <prop key="remove">PROPAGATION_REQUIRED</prop>
- <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
- </props>
- </property>
- </bean>
而bean id="PersonDAOProxy" 中可以将:
<property name="transactionAttributes">换成
ok,spring2与hibernate3整合完成.
spring2整合struts2在书上见有多种方法,说一下我用过的两种方法
1.将struts2-spring-plugin-2.0.11.2.jar加入到lib目录下。
2.修改web.xml文件加入spring的监听器
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<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>
3.修改struts.xml
- <package name="newsViewDemo" extends="struts-default.xml" namespace="/view">
- <action name="newsView!*" class="newsViewAction" method="{1}">
- <result name="input">list.jsp</result>
- <result name="detail">detail.jsp</result>
- <result name="classnewslist">classnewslist.jsp</result>
- </action>
- </package>
同时在ApplicationContext.xml要有:
- <bean id="newsViewAction" class="com.test.news.action.NewsViewAction"
- abstract="false" lazy-init="default" autowire="default"
- dependency-check="default">
- <property name="newsListManager">
- <ref bean="newsListManager" />
- </property>
- </bean>
这里要注意的是action的class属性值要与bean的id值相同
这样spring就可以管理action了,但是有没有发觉,把action同时在两个地方进行配置,如果以后action越来越多的话,会变的非常冗长,可不可以只写一边呢,答案是可以的方法如下:
现在呢,省去spring中action的配置利用spring的自动装配功能来实现
把struts.xml文件中action的class属值写成实际的class, 然后加上
至此struts2与spring2也整合完成了。