Spring整合Hibernate的XML文件配置,以及web.xml文件配置

利用Spring整合Hibernate時的XML文件配置 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.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.xsd">

    <!-- 向 Spring 容器註冊
         AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor 這 4 個BeanPostProcessor
          識別註解 -->
    <context:annotation-config />
    
    <!-- 自動掃描base-pack子包下面Java文件,如果掃描到有@Component @Controller @Service等這些註解的類,則把這些類註冊爲bean -->
    <context:component-scan base-package="cqvie.yjq" />
    
<!-- ************************************************************************************************ -->
    <!-- 定義.properties文件 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          
          <property name="locations" value="classpath:jdbc.properties"/>
          
          <!--  如果有多個.properties文件,則可以透過  locations屬性來設定:
            <property name="locations">
                <list>
                    <value>classpath:mailsender1.properties</value>
                    <value>classpath:mailsender2.properties</value>
                </list>
            </property>
           -->
    </bean>

<!-- ************************************************************************************************ -->
    <!-- 配置數據源  -->
    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp2.BasicDataSource">
          <property name="driverClassName" value="${jdbc.driverClassName}"/>
          <property name="url" value="${jdbc.url}"/>
          <property name="username" value="${jdbc.username}"/>
          <property name="password" value="${jdbc.password}"/>
    </bean>

<!-- ************************************************************************************************ -->
    <!-- 自動創建 SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
          <property name="dataSource" ref="dataSource"/>
          <!-- 等價於下面的方法
              <property name="annotatedClasses">
                  <list>
                    <value>cqvie.yjq.model.User</value>
                    <value>cqvie.yjq.model.Log</value>
                  </list>
              </property>
           -->
           <!-- 掃描包下面的java文件 -->
           <property name="packagesToScan">
              <list>
                <value>cqvie.yjq.model</value>
              </list>
          </property>
            
          <!-- 配置 hibernate.cfg.xml 中的信息 -->
          <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>
              </props>
          </property>
    </bean>
    
<!-- ************************************************************************************************ -->
    <!-- 事務管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
 
     <!-- 註解方式
        <tx:annotation-driven transaction-manager="txManager"/>
     -->
     <!-- XML方式   -->
     <aop:config>
         <aop:pointcut expression="execution(public * cqvie.yjq.service..*.*(..))" id="bussinessService"/>
         <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice"/>
     </aop:config>
     
     <tx:advice id="txAdvice" transaction-manager="txManager">
         <tx:attributes>
             <tx:method name="is*" read-only="true"/>
             <tx:method name="add*" propagation="REQUIRED"/>
         </tx:attributes>
     </tx:advice>
     
<!-- ************************************************************************************************ -->
     <!-- Spring 調用 Hibernate 的持久化操作 -->
     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
         <property name="sessionFactory" ref="sessionFactory"></property>
     </bean>
     
</beans>

轉自:https://www.cnblogs.com/yjq520/p/6701185.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章