其实原理很简单,struts2负责处理页面和逻辑,位于上层,hibernate3负责持久层和数据交互,位于下层,而spring3穿插于从上至下,负责管理和配置。由于配置的地方太多,所以显得难,以前也一直没有全部拉通过,现在将大概的流程都写上来:
首先,我们要将spring的xml文件建立好,让它管理hibernate的持久类,也管理service,action等
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://192.168.86.55:3306/ihelpdesk</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>584920</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="myDataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.default_schema"></prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.search.autoregister_listeners">false</prop>
<prop key="hibernate.max_fetch_depth">6</prop>
<prop key="hibernate.jdbc.fetch.size">300</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/po/User.hbm.xml</value>
<value>com/po/OrderHeader.hbm.xml</value>
</list>
</property>
</bean>
<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<aop:config>
<aop:pointcut id="allServiceMethod"
expression="execution(* com.synnex.cloud..*.service.*Service*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceMethod"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="new*" propagation="REQUIRED" rollback-for="Throwable"/>
</tx:attributes>
</tx:advice>
<!-- access local DB bean -->
<import resource="applicationContext-action.xml"/>
<import resource="applicationContext-dao.xml"/>
<import resource="applicationContext-service.xml"/>
<!--
<import resource="applicationContext-security.xml"/>
-->
</beans>
其中这三条尤为重要,
<import resource="applicationContext-action.xml"/>
<import resource="applicationContext-dao.xml"/>
<import resource="applicationContext-service.xml"/>
从后缀名上可以看出,这几个xml的作用。