1. 程式人生 > >hibernate與spring整合

hibernate與spring整合

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