hibernate与spring整合

版权声明:爱我请告诉我 https://blog.csdn.net/hardly555/article/details/78166201
<?xml version="1.0" encoding="UTF-8"?>
<!-- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
 -->
  <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.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.xsd">
        <!-- 从底层配起,service需要dao所以配dao,action需要service所以配service
                 但是配置service过程要把其需要的元素依赖注入。
                 即每一个bean都能单独注入到需要到它的类中,每一个bean都是健全的 -->
<!--         <bean name="mydao" class="website.hardly.DAO.Impl.UserDAOSQLImpl"></bean>
        <bean name="service" class="website.hardly.service.Impl.UserServiceImpl">
            <property name="dao" ref="mydao"></property>
        </bean>
 -->
    <context:annotation-config/>
    <context:component-scan base-package="website.hardly.DAO.Impl,website.hardly.service.Impl,website.hardly.aop"></context:component-scan>
    <aop:aspectj-autoproxy/>
    <!-- 读取外部属性文件,获取数据源参数,src目录下的dataSource.properties -->
    <context:property-placeholder location="classpath:dataSource.properties"/>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 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="${db.username}"/>
        <property name="url" value="${db.url}"/>
        <property name="password" value="${db.password}"/>
        <property name="driverClassName" value="${db.driverClassName}"/>

    </bean> 

<!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>    
        <property name="packagesToScan" value="website.hardly.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>
    <!-- 初始化hibernateTemplate,记得在DAO类中注入hibernateTemplate,以用它进行增删查改不再用session -->  
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 配置事务管理器,保证每个数据库操作一致性原子性 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean> 

    <!-- 声明式事务,用到事务管理器 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">            
        <tx:attributes>
          <tx:method name="add*" propagation="REQUIRED"/>
          <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
        </tx:attributes>        
    </tx:advice>

    <!-- 定义切面 ,用到声明的事务-->
    <aop:config>
        <aop:pointcut expression="execution(* website.hardly.service.Impl.*.* (..))" id="txPointCut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>

 </beans>

猜你喜欢

转载自blog.csdn.net/hardly555/article/details/78166201
今日推荐