Hibernate4 Struts2 Spring3整合

<!--版本采用hibernate-release-4.0.0.CR5  spring3.1.0 RC1  struts-2.2.3.1-->

jdbc.properties 放在src下

<!--JDBC资源配置文件-->
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/webdb
jdbc.username=webdb_admin
jdbc.password=123456


c3p0.acquireIncrement=10
c3p0.initialPoolSize=15
c3p0.minPoolSize=30
c3p0.maxPoolSize=120
c3p0.maxIdleTime=30
c3p0.idleConnectionTestPeriod=30
c3p0.maxStatements=300
c3p0.numHelperThreads=100
c3p0.checkoutTimeout=0
c3p0.validate=true


<!--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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName" default-lazy-init="true">

<!--加载JDBC资源(根据实际情况加载具体的jdbc.properties)-->
<util:properties id="jdbcProperties" location="classpath:jdbc.properties"/>


<!-- 数据源定义,使用c3p0 连接池 -->
<bean id="dataSource"
  class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
  <property name="driverClass">
   <value>${jdbc.driverClassName}</value>
  </property>
  <property name="jdbcUrl">
   <value>${jdbc.url}</value>
  </property>
  <property name="user">
   <value>${jdbc.username}</value>
  </property>
  <property name="password">
   <value>${jdbc.password}</value>
  </property>
  <property name="acquireIncrement">
   <value>${c3p0.acquireIncrement}</value>
  </property>
  <property name="initialPoolSize">
   <value>${c3p0.initialPoolSize}</value>
  </property>
  <property name="minPoolSize">
   <value>${c3p0.minPoolSize}</value>
  </property>
  <property name="maxPoolSize">
   <value>${c3p0.maxPoolSize}</value>
  </property>
  <property name="maxIdleTime">
   <value>${c3p0.maxIdleTime}</value>
  </property>
  <property name="idleConnectionTestPeriod">
   <value>${c3p0.idleConnectionTestPeriod}</value>
  </property>
  <property name="maxStatements">
   <value>${c3p0.maxStatements}</value>
  </property>
  <property name="numHelperThreads">
   <value>${c3p0.numHelperThreads}</value>
  </property>
  <property name="checkoutTimeout">
   <value>${c3p0.checkoutTimeout}</value>
  </property>
</bean>


<!--定义SessionFactory 并定义相关实体-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="annotatedClasses">
   <list><!--配实体层,也可定义个文件  把相应实体层放到相应文件中--></list>
  </property>
 
 
  <!--配置Hibernate相关信息-->
  <property name="properties">
   <value>
         hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
         hibernate.show_sql=false
         hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
         hibernate.release_mode=auto
         hibernate.connection.isolation=1
         hibernate.cache.use_query_cache=true
         hibernate.cache.use_second_level_cache=true
         hibernate.jdbc.batch_size=25
         hibernate.jdbc.fetch_size=25
   </value>
  </property>
 
</bean>

<!--Hibernate事务管理(3)-->
<bean id="transactionManager"
  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory"
   ref="sessionFactory" />
</bean>



<!--配置Spring AOP -->
<tx:advice id="transacAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="get*" read-only="true"/>
   <tx:method name="*list" read-only="true"/>
   <tx:method name="query*" read-only="true"/>
   <tx:method name="*"/>
  </tx:attributes>
</tx:advice>

<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="com.hanhn"/>

<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>


<context:annotation-config /> 

<aop:config> 
  <aop:pointcut id="executOption" 
   expression="execution(public * com.hanhn.*.*.*(..))" /> 
  <aop:advisor advice-ref="transacAdvice" pointcut-ref="executOption" /> 
</aop:config>

</beans>



<!--Web.xml配置-->

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>webdb</display-name>
<welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
</welcome-file-list>


<!--初始化加载applationContext.xml-->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>


<!--配置spring 监听器-->
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!--Servlet 2.4+ listener that exposes the request to the current thread, through both LocaleContextHolder and RequestContextHolder-->
<listener>
  <listener-class>
         org.springframework.web.context.request.RequestContextListener
  </listener-class>
</listener>

<!-- Spring 刷新Introspector防止内存泄露  -->
<!-- Listener that flushes the JDK's JavaBeans Introspector cache on web app shutdown  -->
<listener>
  <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>


<!--配置Struts2的过滤器-->

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<!--释放资源-->
<filter>
  <filter-name>struts-cleanup</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>

<filter-mapping>
  <filter-name> struts2 </filter-name>
  <url-pattern>/*</url-pattern>
  <filter-name>struts-cleanup</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>



<!--过滤编码-->
<filter>
  <filter-name>EncodingFilter</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>
</filter>

<!--Servlet 2.3 Filter that binds a Hibernate Session to the thread for the entire processing of the request-->
<filter>
  <filter-name>OpenSessionInViewFilter</filter-name>
  <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>

</web-app>

jar包地址:http://www.kuaipan.cn/index.php?ac=file&oid=25394217475907681

猜你喜欢

转载自mentien.iteye.com/blog/1255375