struts2+spring4.0+hibernate4.0
4.x版本与3.x版本有较大区别,要配置方法须要注意,用到的jar包如下
文件结构
src/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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 事务管理器,将委托给HibernateTransactionManager进行管理// --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 事务处理的AOP配置 所有服务层bean声明都要继承此bean ,在proxy里没有定义target属性,所以一定要在bean里加上 abstract="true" // --> <bean id="TransactionProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="transactionManager"/> <property name="transactionAttributes"> <props> <!-- 为了保证服务层统一的事务处理。服务层接口,类的方法必须以下面的方法为开头 --> <!--spring 捕获到RuntimeException和其他一些异常时才会回滚,不是所有异常都会回滚,-Exception 设置 为任何异常都回滚 --> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="user*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="up*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="mod*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="del*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="create*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="execute*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="do*">PROPAGATION_REQUIRED,-Exception</prop> </props> </property> </bean> <!-- 提供普java类获取spring上下文 通过上下文获取具体bean,调用其中的方法 --> <bean id="springApplicationContextUtil" class="com.soyann.common.util.SpringApplicationContextUtil"></bean> <!-- 系统初始化的时候读取数据库配置文件配置的数据库类型 --> <bean class="com.soyann.common.util.DBPropertiesUtil" lazy-init="false" init-method="init"/> <!-- 根据MySQL数据库identity获取NEXTVAL --> <bean id="mySqlKeyGenerator" class="org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer"> <property name="dataSource" ref="sysDataSource"/> <property name="incrementerName"> <value>sy_key_sequence</value> </property> <property name="columnName"> <value>ID</value> </property> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="propertyConfigure" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:resource/properties/dbconfig.properties</value> </list> </property> </bean> <!-- 指定连接数据库的驱动 --> <bean id="sysDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!--The driver class name --> <property name="driverClass"> <value>${jdbc.driverClassName}</value> </property> <!-- 指定连接数据库的URL --> <property name="jdbcUrl"> <value>${jdbc.url}</value> </property> <!-- 指定连接数据库的用户名 --> <property name="user"> <value>${jdbc.username}</value> </property> <!-- 指定连接数据库的密码 --> <property name="password"> <value>${jdbc.password}</value> </property> <!-- 指定连接池的初始化连接数 取值应在minPoolSize 与 maxPoolSize 之间.Default:3 --> <property name="initialPoolSize"> <value>${jdbc.initialPoolSize}</value> </property> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime"> <value>60</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="sysDataSource"/> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop> <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop> <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop> <prop key="hibernate.connection.url">jdbc:mysql://192.168.8.100:3306/soyann</prop> <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop> </props> </property> <!-- 文件夹映射 --> <property name="mappingDirectoryLocations"> <list> <value>WEB-INF/classes/com/soyann</value> </list> </property> <!-- 注解方式映射 --> <property name="packagesToScan"> <list> <value>com.soyann.*</value> </list> </property> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>sshFrame</display-name> <context-param> <param-name>webAppRootKey</param-name> <param-value>sshFrame.root</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:resource/spring/**/app*.xml </param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/resource/properties/log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter-mapping> <filter-name>cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <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> </web-app>