1 struts.xml源码:
2 <?xml version="1.0" encoding="UTF-8"?>
3 <!DOCTYPE struts PUBLIC
4 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
5 "http://struts.apache.org/dtds/struts-2.0.dtd">
6 <struts>
7 <!-- struts2委托spring管理 -->
8 <constant name="struts.objectFactory" value="spring"/>
9 <!-- /crm/emp/add.action -->
10 <package name="crm_employee" extends="struts-default" namespace="/emp">
11 <action name="add" class="addBean" method="add">
12 <result>add.action</result>
13 <result>/emp/add_suc.jsp</result>
14 </action>
15 <action name="list" class="listBean" method="list">
16 <result>/emp/list.jsp</result>
17 </action>
18 <action name="delete" class="deleteBean" method="delete">
19 <result>delete.action</result>
20 <result>/emp/delete_suc.jsp</result>
21 </action>
22 <action name="update" class="updateBean" method="update">
23 <result>update.action</result>
24 <result>/emp/edit_suc.jsp</result>
25 </action>
26 <action name="edit" class="editBean" method="edit">
27 <result>/emp/edit.jsp</result>
28 </action>
29 <!-- Add actions here -->
30 </package>
31 </struts>
1 web.xml源码:
2 <?xml version="1.0" encoding="UTF-8"?>
3 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
7 <!-- 配置spring的监听器 -->
8 <context-param>
9 <param-name>contextConfigLocation</param-name>
10 <param-value>/WEB-INF/applicationContext*.xml</param-value>
11 </context-param>
12 <!-- 开启监听 -->
13 <listener>
14 <listener-class>
15 org.springframework.web.context.ContextLoaderListener
16 </listener-class>
17 </listener>
18 <!-- 配置OpenSessionInViewFilter,必须在struts2监听之前 -->
19 <filter>
20 <filter-name>lazyLoadingFilter</filter-name>
21 <filter-class>
22 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
23 </filter-class>
24 </filter>
25 <!-- 设置监听加载上下文 -->
26 <filter>
27 <filter-name>struts2</filter-name>
28 <filter-class>
29 org.apache.struts2.dispatcher.FilterDispatcher
30 </filter-class>
31 </filter>
32 <filter-mapping>
33 <filter-name>lazyLoadingFilter</filter-name>
34 <url-pattern>*.action</url-pattern>
35 </filter-mapping>
36 <filter-mapping>
37 <filter-name>struts2</filter-name>
38 <url-pattern>/*</url-pattern>
39 </filter-mapping>
40 <welcome-file-list>
41 <welcome-file>index.jsp</welcome-file>
42 </welcome-file-list>
43 </web-app>
1 applicationContext.xml源码:
2 <?xml version="1.0" encoding="UTF-8"?>
3 <beans xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:aop="http://www.springframework.org/schema/aop"
6 xmlns:tx="http://www.springframework.org/schema/tx"
7 xsi:schemaLocation="
8 http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
10 http://www.springframework.org/schema/tx
11 http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
12 http://www.springframework.org/schema/aop
13 http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
14 <!-- 配置Hibernate支持 -->
15 <bean id="dataSource"
16 class="org.apache.commons.dbcp.BasicDataSource">
17 <property name="driverClassName"
18 value="com.mysql.jdbc.Driver">
19 </property>
20 <property name="url"
21 value="jdbc:mysql://localhost:3306/tables">
22 </property>
23 <property name="username" value="root"></property>
24 <property name="password" value="hicc"></property>
25 </bean>
26 <bean id="sessionFactory"
27 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
28 <property name="dataSource">
29 <ref bean="dataSource" />
30 </property>
31 <property name="hibernateProperties">
32 <props>
33 <prop key="hibernate.dialect">
34 org.hibernate.dialect.MySQLDialect
35 </prop>
36 <prop key="hibernate.show_sql">true</prop>
37 </props>
38 </property>
39 <property name="mappingResources">
40 <list>
41 <value>com/sy/crm/model/Employee.hbm.xml</value>
42 </list>
43 </property>
44 </bean>
45 <bean id="employeeDao"
46 class="com.sy.crm.dao.hibernate.EmployeeDaoHibernate">
47 <property name="sessionFactory">
48 <ref bean="sessionFactory" />
49 </property>
50 </bean>
51 <bean id="employeeManager"
52 class="com.sy.crm.service.impl.EmployeeManagerImpl">
53 <property name="employeeDao">
54 <ref bean="employeeDao" />
55 </property>
56 </bean>
57
58 <bean id="addBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
59 <property name="employeeManager">
60 <ref bean="employeeManager" />
61 </property>
62 </bean>
63 <bean id="listBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
64 <property name="employeeManager">
65 <ref bean="employeeManager" />
66 </property>
67 </bean>
68 <bean id="deleteBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
69 <property name="employeeManager">
70 <ref bean="employeeManager" />
71 </property>
72 </bean>
73 <bean id="updateBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
74 <property name="employeeManager">
75 <ref bean="employeeManager" />
76 </property>
77 </bean>
78 <bean id="editBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
79 <property name="employeeManager">
80 <ref bean="employeeManager" />
81 </property>
82 </bean>
83 <!-- 事务管理器 -->
84 <bean id="transactionManager"
85 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
86 <property name="sessionFactory">
87 <ref local="sessionFactory"/>
88 </property>
89 </bean>
90 <!-- 配置事务特性,配置add,delete,update开始的方法,事务传播特性为required -->
91 <tx:advice id="txAdvice" transaction-manager="transactionManager">
92 <tx:attributes>
93 <tx:method name="add*" propagation="REQUIRED"/>
94 <tx:method name="delete*" propagation="REQUIRED"/>
95 <tx:method name="update*" propagation="REQUIRED"/>
96 <tx:method name="*" read-only="true"/>
97 </tx:attributes>
98 </tx:advice>
99 <!-- 配置那些类的方法进行事务管理,当前com.sy.crm.service包中的子包,
100 类中所有方法需要,还需要参考tx:advice的设置 -->
101 <aop:config>
102 <aop:pointcut id="allManagerMethod" expression="execution(*
103 com.sy.crm.service.*.*(..))"/>
104 <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
105 </aop:config>
106 </beans>
Spring中常用事务类型:
PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED--如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。