分享

Spring3整合Hibernate4配置

 IT_易风 2016-03-11
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www./schema/beans"
    xmlns:xsi="http://www./2001/XMLSchema-instance"
    xmlns:context="http://www./schema/context"
    xmlns:tx="http://www./schema/tx"
    xmlns:aop="http://www./schema/aop"
    xmlns:mvc="http://www./schema/mvc"
    xmlns:jee="http://www./schema/jee"
    xsi:schemaLocation="http://www./schema/mvc
    http://www./schema/mvc/spring-mvc-3.1.xsd
        http://www./schema/tx
        http://www./schema/tx/spring-tx-3.0.xsd
        http://www./schema/aop
        http://www./schema/aop/spring-aop-3.0.xsd
        http://www./schema/beans
        http://www./schema/beans/spring-beans-3.0.xsd
        http://www./schema/context
        http://www./schema/context/spring-context-3.0.xsd
        http://www./schema/jee
        http://www./schema/jee/spring-jee-3.0.xsd">
      
    <!-- 打开基于注解的配置 -->
    <context:annotation-config/>
      
    <aop:aspectj-autoproxy />
      
    <!-- 开启组件自动扫描 -->
    <context:component-scan base-package="org.rsun.ams"/>  
      
    <!-- 读取外部属性文件 -->
    <context:property-placeholder location="classpath:DataSource.properties"/>
      
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <!-- 初始化连接数量; -->
        <property name="initialSize" value="0" />
        <!-- 最大并发连接数 -->
        <property name="maxActive" value="20" />
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="20" />
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="0" />
        <!-- 最大等待时长 -->
        <property name="maxWait" value="60000" />
        <!-- 超过时间限制是否回收 -->
        <property name="removeAbandoned" value="true" />
        <!-- 超过时间限制多长; -->
        <property name="removeAbandonedTimeout" value="180"/>     
          
          
        <!-- 数据源连接参数配置; -->
        <property name="username" value="${username}"/>
        <property name="url" value="${url}"/>
        <property name="password" value="${password}"/>
        <property name="driverClassName" value="${driverClassName}"/>
          
    </bean
      
    <!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>    
        <property name="packagesToScan" value="org.rsun.ams.core.model"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            </props>
        </property>
    </bean>
      
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean
      
    <!-- 定义切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* org.rsun.ams.service.*.* (..))" id="txPointCut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
      
    <!-- 声明式事务 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">            
        <tx:attributes>
                <tx:method name="add*" propagation="REQUIRED"/>
                <tx:method name="remove*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="init*" propagation="REQUIRED"/>              
                <tx:method name="*" read-only="true" propagation="REQUIRED"/>
        </tx:attributes>        
    </tx:advice>
      
    <!-- 开启事务注解
    <tx:annotation-driven transaction-manager="transactionManager"/>
     -->
       
      
</beans>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约