Spring项目开发笔记系列(三)

 Spring项目开发笔记系列(三)

      --核心构件spring配置文件

<?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:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                      http://www.springframework.org/schema/tx
                      http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  <!-- 定义数据库连接的信息,如driverClass,jdbcUrl,user,password等 -->
  <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/config/jdbc.properties"/>
  </bean>
  <!-- 定义数据源,该bean的ID为dataSource -->
  <bean id="dataSource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
    <!-- 指定数据库驱动 -->
    <property name="driverClass"><value>${driverClass}</value></property>
    <!-- 指定数据库连接的URL -->
    <property name="jdbcUrl"><value>${jdbcUrl}</value></property>
    <!-- 指定数据库用户名 -->
    <property name="user"><value>${user}</value></property>
    <!-- 指定数据库用户密码 -->
    <property name="password"><value>${password}</value></property>
    <property name="minPoolSize"><value>1</value></property>
    <property name="maxPoolSize"><value>20</value></property>
    <property name="maxIdleTime"><value>1800</value></property>
    <property name="acquireIncrement"><value>2</value></property>
    <property name="maxStatements"><value>0</value></property>
    <property name="initialPoolSize"><value>2</value></property>
    <property name="idleConnectionTestPeriod"><value>1800</value></property>
    <property name="acquireRetryAttempts"><value>30</value></property>
    <property name="breakAfterAcquireFailure"><value>true</value></property>
    <property name="testConnectionOnCheckout"><value>false</value></property>
  </bean>
  <!-- 定义Hibernate的SessionFactory加载.hbm.xml文件时用这种配置-->
  <!--<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">-->
  <!-- 定义AnnotationHibernate的SessionFactory -->
  <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <!-- 依赖注入数据源,注入dataSource -->
    <property name="dataSource" ref="dataSource" />
    <!-- mappingResources属性用来列出全部映射文件 -->
    <property name="annotatedClasses">
      <list>
      <!-- 列出所有PO实体文件 -->
        <value>com.daniel.lr.crbt.share.db.User</value>
      </list>
    </property>
    <!-- 定义Hibernate的SessionFactory的属性 -->
    <property name="hibernateProperties">
      <props>
        <!-- 指定Hibernate的连接方言 -->
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <!-- 不同数据库连接,启动时选择create,update,create-drop -->
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.generate_statistics">true</prop>
        <prop key="hibernate.connection.release_mode">auto</prop>                     
        <prop key="hibernate.autoReconnect">true</prop>
        <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
      </props>
    </property>
    <property name="eventListeners">
      <map>
        <entry key="merge">
          <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
        </entry>
      </map>
    </property>
  </bean>
  <aop:config proxy-target-class="true">
    <aop:advisor pointcut="execution(* *..Hibernate.*(..))" advice-ref="txAdvice"/>
  </aop:config>
  <tx:advice id="txAdvice">
    <tx:attributes>
      <tx:method name="*" read-only="false"/>
    </tx:attributes>
  </tx:advice>
  <!-- 配置Hibernate的事务管理 -->
  <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>
  <!-- 配置DaoBean -->
  <bean id="userDao" class="com.daniel.lr.crbt.share.dao.UserDao">
    <!-- 为DAO注入所需的DataSource实例 -->
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>
  <!-- 配置userDao bean的事务代理 -->
  <bean id="userDaoProxy"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="transactionManager" />
    <property name="target" ref="userDao"/>
    <property name="transactionAttributeSource">
      <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
    </property>
  </bean>
  <!-- Spring代理Struts的Action Action不使用Singleton-->
  <bean name="/login"
        class="com.daniel.lr.crbt.service.web.action.LoginAction"
        scope="prototype">
    <property name="userDao" ref="userDao"/>
  </bean>
</beans>

这就是web.xml中加载的spring-config.xml文件,也就是spring的配置文件~~~

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值