Spring Hibernate integration of XML configuration files, configuration files and web.xml

When using the XML file Spring integrate Hibernate configuration 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 "> 

    ! <- registered Spring container 
         AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor, RequiredAnnotationBeanPostProcessor four BeanPostProcessor 
          identifying annotation -> 
    <context: annotation-config /> 
    
    ! <- automatically scan base-pack subpacket following Java file, If the class has to scan the like @Component @Controller @Service these annotations, put these classes registered as the bean -> 
    <context: Component Base-Package-scan = "cqvie.yjq" /> 
    
! <- *** ************************************************** ******************************************* ->
    <! - defined .properties file -> 
    <bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
          
          <Property name = "locations" value = "the CLASSPATH: jdbc.properties" /> 
          
          <-! - If there are multiple .properties file, you can be set via locations attribute: 
            <property name = "locations"> 
                <List> 
                    <value> CLASSPATH: mailsender1.properties </ value> 
                    <value> CLASSPATH: mailsender2.properties </ value> 
                </ List> 
            </ Property> 
           -> 
    </ the bean> 

<!- **************************** **************************** - > 
    <! - configure data source ->
    <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>
           <! - The following java files scan package ->
           ->
           <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>

Transfer: https://www.cnblogs.com/yjq520/p/6701185.html

Guess you like

Origin blog.csdn.net/writebook2016/article/details/83185175