1、web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!--配置ContextLoaderListener:默认在WEB-INF下查找applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 通过 context-param节点指定查找的位置 contextConfigLocation 是固定写法-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- sturts2核心过滤器 -->
<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>
<!-- OpenSessionInViewFilter -->
<filter>
<filter-name>osivFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>osivFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2、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: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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置dataSource -->
<bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource">
<!-- driverClassName -->
<property name = "driverClassName" value = "com.mysql.jdbc.Driver"></property>
<!-- url -->
<property name = "url" value = "jdbc:mysql://localhost:3306/ssh"></property>
<!-- username -->
<property name = "username" value = "root"></property>
<!-- password -->
<property name = "password" value = "root"></property>
</bean>
<!-- 配置sessionFactory-->
<bean id = "sessionFactory" class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入dataSource -->
<property name = "dataSource" ref = "dateSource"></property>
<!-- hibernate映射文件 -->
<property name = "mappingResources">
<list>
<value>com/sxt/ssh/entity/Userinfo.hbm.xml</value>
<value>com/sxt/ssh/entity/Prodduct.hbm.xml</value>
</list>
</property>
<!-- hibernate属性 -->
<property name = "hibernateProperties">
<props>
<prop key = "hibernate.show_sql">true</prop>
<prop key = "hibernate.format_sql">true</prop>
<prop key = "hibernate.dialect">MySQL5Dialect</prop>
</props>
</property>
</bean>
<!-- 配置Dao,并注入sessionFactory -->
<bean id = "userinfoDao" class = "com.sxt.ssh.dao.impl.UserinfoDaoImpl">
<property name = "sessionFactory" ref = "sessionFactory"></property>
</bean>
<!-- 配置service,并注入Dao-->
<bean id = "userinfoService" class = "com.sxt.ssh.service.impl.UserinfoServiceImpl">
<property name = "userinfoDao" ref = "userinfoDao"></property>
</bean>
<!-- 配置Action, 并注入service -->
<bean id = "userinfoAction" class = "com.sxt.ssh.action.UserinfoAction" scope = "prototype">
<property name = "userinfoService" ref = "userinfoService"></property>
</bean>
<!-- 配置事务管理器,注入sessionFactory -->
<bean id = "transactionManager" class = "org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name = "sessionFactory" ref = "sessionFactory"></property>
</bean>
<!-- 事务通知 -->
<tx:advice id = "txAdvice" transaction-manager = "transactionManager">
<tx:attributes>
<tx:method name = "add*" propagation = "REQUIRED"></tx:method>
<tx:method name = "update*" propagation = "REQUIRED"></tx:method>
<tx:method name = "delete*" propagation = "REQUIRED"></tx:method>
<tx:method name = "*" propagation = "SUPPORTS"></tx:method>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 定义切入点 -->
<aop:pointcut expression = "excution(* com.sxt.ssh.service.impl.UserinfoService.*(..))" id = "mypointcut"></aop:pointcut>
<!-- 通知者 -->
<aop:advisor advice-ref = "txAdvice" pointcut-ref = "mypointcut"><aop:advisor>
</aop:config>
</bean>