spring-MVC__spring__hibernate整合值之spring的配置文件 (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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
        
       <!-- 配置數據源jdbc-dbcp -->
      <!--  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/hibernate"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="username" value="root"></property>
        <property name="password" value="rt"></property>
       </bean> -->
       
       <!-- 讀取jdbc的配置文件 -->
       <context:property-placeholder location="classpath:jdbc.properties"/>
       <!-- 配置數據源jdbc-c3p0 -->
       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
       </bean>
       
       
       <!-- spring整合hibernate -->
       <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- 讀取hibernate配置文件 -->
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <!-- 傳入數據源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- hibernate屬性(hibernate屬性可以放在spring裏一般放在hibernate裏) -->
       <!-- <property name="hibernateProperties">
        <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.format_sql">true</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        <prop key="hibernate.current_session_context_class">thread</prop>
        </props>
        </property> -->
        <!-- 映射文件 -->
        <property name="mappingLocations" value="classpath:com/qf/entity/*.hbm.xml"></property>
       </bean>
     
       
       
       <!--  spring事物全自動化的 -->
       <!-- 事務管理器class換成hibernate的 -->
       <bean id="traManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
       </bean>
       <!-- 事物以註解的方式實現 @Transational
       <tx:annotation-driven transaction-manager="traManager"/> -->
       <!-- 配置事務策略 -->
       <tx:advice id="txAdvice" transaction-manager="traManager">
        <tx:attributes>
        <!-- name:方法名稱,支持通配符的
        isolation:隔離級別
        propagation:事物的轉播屬性
        read-only:是否只讀
        -->
        <tx:method name="tra*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />
        <!-- 查詢方法 -->
        <tx:method name="get*" read-only="true" />
        <tx:method name="find*" read-only="true" />
        <tx:method name="query*" read-only="true" />
        <!-- 更新操作 -->
        <tx:method name="insert*" read-only="false" />
        <tx:method name="remo*" read-only="false" />
        <tx:method name="update*" read-only="false" />
        <tx:method name="del*" read-only="false" />
        <tx:method name="add*" read-only="false" />
        </tx:attributes>
       </tx:advice>
       <!-- AOP的配置 -->
       <aop:config>
        <!-- <aop:pointcut expression="execution(* com.qf.service.*.*(..))" id="p1"/>  pointcut-ref="p1" -->
        <!-- 事物一般加在service(業務層)層 -->
        <aop:advisor  advice-ref="txAdvice" pointcut="execution(* com.qf.service.*.*(..))" />
       </aop:config>
       
       
       
       <!-- service層和dao層的配置 -->
      <!-- 注入hibernateTemplate,hibernateTemplate裏注入sessionFactory -->
       <bean id="hibernateTemplate"  class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
       </bean>
       <bean id="accountDao" class="com.qf.dao.impl.AccountDaoImpl">
        <!-- hibernateDaoTemplate裏注入hibernateTemplate -->
        <property name="hibernateTemplate" ref="hibernateTemplate"></property>
       </bean>
       <bean id="accountService" class="com.qf.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
       </bean>
       
       
</beans>
發佈了48 篇原創文章 · 獲贊 6 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章