7Spring3+Hibernate3

Spring mvc+Spring3.0.5+hibernate3.6整合后的applicationContext配置:
注意default-autowire="byName"必须取值为byName注入,否则启动会报sessionFactory为null的错误。
其他地方都差不多相同。切换到spring3.1也只需要更换spring部分的jar包。


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
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
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/tx          
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/aop  
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byName">


<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 -->
<mvc:annotation-driven />


<!-- (in this case, JDBC related properties) -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>WEB-INF/conf/database/jdbc.properties</value>
</list>
</property>
</bean>


<!-- 配置数据源,使用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="minPoolSize">
<value>5</value>
</property>


<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize">
<value>30</value>
</property>


<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize">
<value>10</value>
</property>


<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime">
<value>60</value>
</property>


<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement">
<value>5</value>
</property>


<!-- JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements">
<value>0</value>
</property>


<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod">
<value>60</value>
</property>


<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
<property name="acquireRetryAttempts">
<value>30</value>
</property>


<!-- 获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 
获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
<property name="breakAfterAcquireFailure">
<value>true</value>
</property>


<!-- 因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 
等方法来提升连接测试的性能。Default: false -->
<property name="testConnectionOnCheckout">
<value>false</value>
</property>
</bean>




<!--Hibernate SessionFatory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
                <value>lifecenter.manage.model</value>
        </list>
        </property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${datasource.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
</bean>




<!-- ===================TransactionManager=================== -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>


<!--================== ASPECT CONFIGURATION================= -->
<aop:config proxy-target-class="false">
<aop:advisor
pointcut="execution(* lifecenter.manage.iface.service.*Service.*(..))"
advice-ref="txAdvice" order="2" />
</aop:config>


<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="SUPPORTS" />
<tx:method name="add*" propagation="REQUIRED" rollback-for="LifecenterException" />
<tx:method name="del*" propagation="REQUIRED" rollback-for="LifecenterException" />
<tx:method name="upd*" propagation="REQUIRED" rollback-for="LifecenterException" />
<tx:method name="*" rollback-for="LifecenterException" />
</tx:attributes>
</tx:advice>




<aop:config>
<aop:aspect id="logAspect" ref="logAdvice" order="1">
<aop:pointcut id="logInsertMethods"
expression="execution(* lifecenter.manage.iface.service.*Service.*(..))" />
<aop:after-throwing method="saveLog" pointcut-ref="logInsertMethods"
throwing="dataAccessEx" />
</aop:aspect>
</aop:config>


<!--==================== BEANS DEFINITIONS =================== -->
<context:annotation-config />
<context:component-scan
base-package="lifecenter.actions,lifecenter.manage,lifecenter.pub"></context:component-scan>




</beans>




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">


<!-- 从类路径加载spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>


<!-- log4j start -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>


<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/conf/log/log4j.properties</param-value>
</context-param>


<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- log4j end -->


<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>


<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>


</web-app>




********************
<mvc:annotation-driven />
相当于注册了DefaultAnnotationHandlerMapping和 AnnotationMethodHandlerAdapter两个bean,是spring MVC为@Controllers分发请求所必须的。还注册了LocalValidatorFactoryBean。
并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。


<mvc:interceptors/> 是一种简写形式。通过看前面的大图,知道,我们可以配置多个HandlerMapping。<mvc:interceptors/>会为每一个HandlerMapping,注入一个拦截器。其实我们也可以手动配置为每个HandlerMapping注入一个拦截器。


<mvc:default-servlet-handler/> 使用默认的Servlet来响应静态文件。


<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/> 匹配URL  /images/**  的URL被当做静态资源,由Spring读出到内存中再响应http。






**********************


DBCP配置  
        <bean id="hospitalDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
        <property name="driverClassName"><value>${jdbcHospital.driverClassName}</value></property>  
        <property name="url"><value>${jdbcHospital.url}</value></property>  
        <property name="username"><value>${jdbcHospital.username}</value></property>  
        <property name="password"><value>${jdbcHospital.password}</value></property>  
        <property name="initialSize" value="10" />  
        <property name="maxActive" value="100" />  
        <property name="maxIdle" value="30" />  
        <property name="minIdle" value="10" />  
        <property name="logAbandoned" value="true" />  
        <property name="removeAbandoned" value="true" />  
        <property name="removeAbandonedTimeout" value="1000" />  
        <property name="maxWait" value="5000" />  
        </bean>  
defaultAutoCommit:设置从数据源中返回的连接是否采用自动提交机制,默认值为 true;   
defaultReadOnly:设置数据源是否仅能执行只读操作, 默认值为 false;   
maxActive:最大连接数据库连接数,设置为0时,表示没有限制;   
maxIdle:最大等待连接中的数量,设置为0时,表示没有限制;   
maxWait:最大等待秒数,单位为毫秒, 超过时间会报出错误信息;   
validationQuery:用于验证连接是否成功的查询SQL语句,SQL语句必须至少要返回一行数据, 如你可以简单地设置为:“select count(*) from user”;   
removeAbandoned:是否自我中断,默认是 false ;   
removeAbandonedTimeout:几秒后数据连接会自动断开,在removeAbandoned为true,提供该值;   
logAbandoned:是否记录中断事件, 默认为 false;  
  
C3P0配置  
        <bean id="hospitalDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
        <property name="driverClass" value="${jdbcHospital.driverClassName}" />  
        <property name="jdbcUrl" value="${jdbcHospital.url}" />  
        <property name="user" value="${jdbcHospital.username}" />  
        <property name="password" value="${jdbcHospital.password}" />  
        <property name="maxPoolSize" value="60" />  
        <property name="minPoolSize" value="10" />  
        <property name="initialPoolSize" value="10" />  
        <property name="breakAfterAcquireFailure" value="true" />   
        <property name="testConnectionOnCheckout" value="true" />  
        <property name="testConnectionOnCheckin" value="true" />          
        </bean>  
C3P0拥有比DBCP更丰富的配置属性,通过这些属性,可以对数据源进行各种有效的控制:   
acquireIncrement:当连接池中的连接用完时,C3P0一次性创建新连接的数目;   
acquireRetryAttempts:定义在从数据库获取新连接失败后重复尝试获取的次数,默认为30;   
acquireRetryDelay:两次连接中间隔时间,单位毫秒,默认为1000;   
autoCommitOnClose:连接关闭时默认将所有未提交的操作回滚。默认为false;   
automaticTestTable: C3P0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数,那么属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将中为C3P0测试所用,默认为null;   
breakAfterAcquireFailure:获取连接失败将会引起所有等待获取连接的线程抛出异常。但是数据源仍有效保留,并在下次调   用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。默认为 false;   
checkoutTimeout:当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒,默认为0;   
connectionTesterClassName:通过实现ConnectionTester或QueryConnectionTester的类来测试连接,类名需设置为全限定名。默认为 com.mchange.v2.C3P0.impl.DefaultConnectionTester;   
idleConnectionTestPeriod:隔多少秒检查所有连接池中的空闲连接,默认为0表示不检查;   
initialPoolSize:初始化时创建的连接数,应在minPoolSize与maxPoolSize之间取值。默认为3;   
maxIdleTime:最大空闲时间,超过空闲时间的连接将被丢弃。为0或负数则永不丢弃。默认为0;   
maxPoolSize:连接池中保留的最大连接数。默认为15;   
maxStatements:JDBC的标准参数,用以控制数据源内加载的PreparedStatement数量。但由于预缓存的Statement属于单个Connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素,如果maxStatements与 maxStatementsPerConnection均为0,则缓存被关闭。默认为0;   
maxStatementsPerConnection:连接池内单个连接所拥有的最大缓存Statement数。默认为0;   
numHelperThreads:C3P0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能,通过多线程实现多个操作同时被执行。默认为3;   
preferredTestQuery:定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个参数能显著提高测试速度。测试的表必须在初始数据源的时候就存在。默认为null;   
propertyCycle: 用户修改系统配置参数执行前最多等待的秒数。默认为300;   
testConnectionOnCheckout:因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable   
等方法来提升连接测试的性能。默认为false;   
testConnectionOnCheckin:如果设为true那么在取得连接的同时将校验连接的有效性。默认为false。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值