beans配置

<?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:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
  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
        http://www.directwebremoting.org/schema/spring-dwr
      http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
        
       <!-- 打开Spring的Annotation的支持 -->
       <context:annotation-config />
       <!-- 设定Spring去哪些包中查找Annotation -->
       <context:component-scan base-package="cn.zf.houserent" />
       
       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!-- 配置连接池的初始值 -->
        <property name="initialSize" value="1" />
        <!-- 连接池的最大值 -->
        <!-- <property name="maxActive" value="500"/> -->
        <!-- 最大空闲时,当经过一个高峰之后,连接池可以将一些用不到的连接释放,一直减少到maxIdle为止 -->
        <!-- <property name="maxIdle" value="2"/> -->
        <!-- 当最小空闲时,当连接少于minIdle时会自动去申请一些连接 -->
        <property name="minIdle" value="1" />
        <property name="maxActive" value="100" />
        <property name="maxIdle" value="20" />
        <property name="maxWait" value="1000" />
    </bean>

    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <!-- 创建Spring的SessionFactory工厂 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
        <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource" />
         <!-- 设置Spring去哪个包中查找相应的实体类 -->
        <property name="packagesToScan">
            <value>cn.zf.houserent.bean</value>
        </property>
        <property name="hibernateProperties">
          <props>
              <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
              <prop key="hibernate.show_sql">true</prop>
              <prop key="hibernate.hbm2ddl.auto">update</prop>
          </props>
         </property>
    </bean>
    
    <!-- 配置Spring的事务处理 -->
        <!-- 创建事务管理器 -->
      <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
      </bean>
      <!-- 配置AOP,Spring是通过AOP来进行事务管理的 -->
      <aop:config>
          <!-- 设置PointCut表示那些方法需要加入到事务管理 -->
        <aop:pointcut id="allMethods" expression="execution(* cn.zf.houserent.biz.impl.*.*(..))"/>
        <!-- 通过advisor来确定具体要加入事务控制的方法 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods"/>
      </aop:config>
    
      <!-- 配置哪些方法需要加入事务控制 -->
      <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
          <!-- 让所有方法都加入事务管理,为了提高效率,可以把一些查询之类的方法设置为只读的事务 -->
          <tx:method name="*" propagation="REQUIRED"/>
          <!-- 以下方法都是可能涉及修改的方法,就无法设置为只读 -->
         <!--  <tx:method name="add*" propagation="REQUIRED"/>
          <tx:method name="del*" propagation="REQUIRED"/>
          <tx:method name="update*" propagation="REQUIRED"/>
          <tx:method name="save*" propagation="REQUIRED"/>
          <tx:method name="modify*" propagation="REQUIRED"/>
          <tx:method name="regiseter" propagation="REQUIRED"/>-->
        </tx:attributes>
      </tx:advice>
</beans>

 

posted @ 2019-06-09 23:43  五艺  阅读(278)  评论(0)    收藏  举报