zoukankan      html  css  js  c++  java
  • 配置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" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    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-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


    <!--c3p0连接池参数设置说明 -->
    <!--maxPoolSize表示连接池中保留的最大连接数,default为15 -->
    <!--minPoolSize表示连接池中保留的最小连接数 -->
    <!--initialPoolSize表示初始化时获取的连接数,取值应该在maxPoolSize和minPoolSize之间,default为3 -->
    <!--maxIdleTime表示最大空闲时间,60秒内未使用则连接被丢弃,若为0则永不丢弃,default为0 -->
    <!--acquireIncrement当连接池用连接被耗尽的时候c3p0一次同时获取的连接数,default 3 -->
    <!--maxStatements,JDBC的标准参数,用于控制数据源内加载的PreparedStatements数量,但由于预缓存的statements属于单个connection而不是整个连接池。
    所以设置这个参数需要考虑到多方面的因素。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭,default 0 -->
    <!--idleConnectionTestPeriod表示每多少秒检查连接池中的空闲连接,default 0 -->
    <!--acquireRetryAttempts定义在数据库获取新连接失败后重新尝试的次数,default -->
    <!--breakAfterAcquireFailure获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效保留,并在下次调用getConnection()的时候继
    续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
    <!--testConnectionOnCheckout因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的时候都将校验其有效性。建议使用
    idleConnectionTestPeriod或automaticTestTable等方法来提升连接测试的性能。Default: false -->
    <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sshanno" />
    <property name="user" value="root" />
    <property name="password" value="root" />
    <property name="maxPoolSize" value="20" />
    <property name="minPoolSize" value="5" />
    <property name="maxStatements" value="50" />
    <property name="maxIdleTime" value="1800" />
    </bean> -->

    <!-- location值为参数配置文件的位置 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--dbcp连接池参数设置说明 -->
    <!--initialSize表示池启动时创建的连接数量 -->
    <!--maxActive表示同一时间可以从池分配的最多连接数量,设置为0时表示无限制 -->
    <!--maxIdle表示池里不会释放的最多的空闲的连接数量,设置为0时表示无限制 -->
    <!--minIdle表示在不新建连接条件下,池中保持空闲的最少连接数 -->
    <!--removeAbandoned设置自动回收超时连接,value为true或false -->
    <!--removeAbandonedTimeout设置自动回收超时时间(以秒数为单位) -->
    <!--logAbandoned设置在自动回收超时连接的时候打印连接的超时错误,value为true或false -->
    <!--maxWait等待超时以毫秒为单位,在抛出异常前,池等待连接被回收的最长时间(当没有可用连接时),设置为-1表示无限等待 -->
    <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close"
    p:driverClassName="${driverClassName}"
    p:url="${url}"
    p:username="${username}"
    p:password="${password}"
    p:maxActive="${maxActive}"
    p:maxIdle="${maxIdle}"
    p:minIdle="${minIdle}"/>

    <!--在hibernate3中获取sessionFactory ,hibernate4则class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" -->
    <!--设置属性说明 -->
    <!--hibernate.dialect设置方言 -->
    <!--hibernate.format_sql输出格式化的sql -->
    <!--hibernate.show_sql控制台输出sql -->
    <!--hibernate.hbm2ddl.auto 自动创建|更新|验证数据库表结构。有以下几个参数:
    create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
    create-drop:每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。
    update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了
    但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。
    validate :每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。 -->
    <!--current_session_context_class使用缓存机制。(hibernate3:Thread或jta);(hibernate4:org.springframework.orm.hibernate4.SpringSessionContext) -->
    <!--hibernate.cache.provider_class 使用Ehcache缓存;hibernate3是org.hibernate.cache.HashtableCacheProvider;hibernate4是net.sf.ehcache.hibernate.EhCacheProvider -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource"/>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
    <!-- <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2005Dialect</prop> -->
    <!-- <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> -->
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="current_session_context_class">Thread</prop>
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
    </props>
    </property>
    <!--自动扫描(实体类)注解包:实体类位置为:com.spring.mvc.entities.User,不能写成:com.spring.mvc.entities,而要写成:com.spring.mvc,packagesToScan要比实际上前一层-->
    <property name="packagesToScan" value="ssh.demo..*"/>



    <!-- 对于annotatedClasses只能一个类一个类的写,不能用*通配符 。可以用packagesToScan扫描包的方式代替annotatedClasses方式-->
    <!-- <property>
    <list>
    <value>ssh.demo.*</value>
    </list>
    </property> -->
    <!--上面的方法是错误的,下面的方法才是正确的。但是当类很多时,可以用packagesToScan扫描包的方式代替 -->
    <!-- <property name="annotatedClasses">
    <list>
    <value>ssh.demo.domain.User</value>
    <value>ssh.demo.domain.UserLog</value>
    <value>ssh.demo.domain.Product</value>
    </list>
    </property> -->
    </bean>

    <!--hibernate3.1以及以上版本取消了hibernateTemplate,hibernate4需要通过getCurrentSession()获取session -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"
    p:sessionFactory-ref="sessionFactory" />

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

    <!--spring对hibernate的事务管理 -->
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"
    p:sessionFactory-ref="sessionFactory">
    </bean>

    <aop:config>
    <aop:pointcut expression="execution(* ssh.demo.service..*(..))" id="serviceMethod"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <!--默认:
    isolation:DEFAULT
    propagation:REQUIRED
    timeout:-1 事物超时时间由底层事物系统决定
    read-only:false
    rollback-for:所有允许异常回滚
    no-rollback-for:所有检查异常不回滚 -->

    <tx:method name="get*" read-only="true"/>
    <tx:method name="do*"/>
    </tx:attributes>
    </tx:advice>

    </beans>

  • 相关阅读:
    Windows7平台下gitblit服务器安装
    使用JDK的zip编写打包工具类
    MongoDB和Java(7):MongoDB用户管理
    MongoDB和Java(6):Spring Data整合MongoDB副本集、分片集群
    MongoDB和Java(5):Spring Data整合MongoDB(注解配置)
    MongoDB和Java(4):Spring Data整合MongoDB(XML配置)
    MongoDB和Java(3):Java操作MongoB
    MongoDB和Java(2):普通用户启动mongod进程
    Spring MVC 执行原理
    选择性配置-ConditionalOnProperty
  • 原文地址:https://www.cnblogs.com/andyFok/p/9529800.html
Copyright © 2011-2022 走看看