posts - 431,  comments - 344,  trackbacks - 0

          1.讀取屬性文件
          <!-- for properties files -->
           <bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
             <list>
              <value>classpath:*.properties</value>
             </list>
            </property>
           </bean>

          2.配置數據源
          <!-- for dataSource -->
          <bean id="dataSource"
            class="com.mchange.v2.c3p0.ComboPooledDataSource
            destroy-method="close">
            <property name="driverClass" value="${jdbc.driver}" />
            <property name="jdbcUrl" value="${jdbc.url}" />

            <property name="properties">
             <props>
              <prop key="c3p0.minPoolSize">${hibernate.c3p0.minPoolSize}</prop>
              <prop key="hc3p0.maxPoolSize">${hibernate.c3p0.maxPoolSize}</prop>
              <prop key="hc3p0.timeout">${hibernate.c3p0.timeout}</prop>
              <prop key="c3p0.max_statement">${hibernate.c3p0.max_statement}</prop>
              <prop key="user">${jdbc.username}</prop>
              <prop key="password">${jdbc.password}</prop>
              <prop key="c3p0.testConnectionOnCheckout">true</prop>
             </props>
            </property>
           </bean>
          #Jdbc Configuration

          jdbc.driver=com.mysql.jdbc.Driver
          jdbc.url=jdbc:mysql://localhost/wsh?useUnicode=true&characterEncoding=utf-8
          jdbc.username=wsh
          jdbc.password=wsh

          hibernate.c3p0.minPoolSize=2
          hibernate.c3p0.maxPoolSize=10
          hibernate.c3p0.timeout=100
          hibernate.c3p0.max_statement=900

          注:當然還有很多例如"org.apache.commons.dbcp.BasicDataSource"
          <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}" />
           </bean>
          jdbc.driverClassName=org.gjt.mm.mysql.Driver
          jdbc.url=jdbc:mysql://172.19.30.178:3306/wsh?useUnicode=true&characterEncoding=utf-8
          jdbc.username=wsh
          jdbc.password=wsh

          hibernate.dialect=org.hibernate.dialect.MySQLDialect
          hibernate.show_sql=true
          hibernate.hbm2ddl.auto=update

          3.sessionFactory配置
          <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />

            <property name="mappingDirectoryLocations">
             <list>
              <value>classpath:/com/rain/wsh/model/</value>
             </list>
            </property>

            <property name="hibernateProperties">
             <props>
              <prop key="hibernate.dialect">
               org.hibernate.dialect.MySQLDialect
              </prop>
              <prop key="hibernate.show_sql">false</prop>
              <!-- <prop key="hibernate.cache.use_query_cache">false</prop> -->
              <!-- prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop-->
              <prop key="hibernate.connection.provider_class">
               org.hibernate.connection.C3P0ConnectionProvider
              </prop>
              <prop key="hibernate.generate_statistics">false</prop>
             </props>
            </property>
           </bean>

          <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
            singleton="true">
            <property name="dataSource">
             <ref local="dataSource" />
            </property>
            <property name="mappingResources">
             <list>
              <value>
               com/rain/study/model/User.hbm.xml
              </value>
             </list>
            </property>
            <property name="hibernateProperties">
             <props>
              <prop key="hibernate.dialect">
               ${hibernate.dialect}
              </prop>
              <prop key="hibernate.show_sql">
               ${hibernate.show_sql}
              </prop>
              <!--   <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>-->
             </props>
            </property>
           </bean>

          4.transactionManager配置
          <!-- Hibernate transaction processing -->
           <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
             <ref local="sessionFactory" />
            </property>
           </bean>

          5.baseTxService配置(Service的代理工廠)
          <bean id="baseTxService" abstract="true"
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager" ref="transactionManager" />
            <property name="transactionAttributes">
             <props>
              <prop key="create*">PROPAGATION_REQUIRED</prop>
              <prop key="update*">PROPAGATION_REQUIRED</prop>
              <prop key="delete*">PROPAGATION_REQUIRED</prop>
              <prop key="get*">PROPAGATION_REQUIRED, readOnly</prop>
              <prop key="*">PROPAGATION_REQUIRED</prop>
             </props>
            </property>
            <property name="postInterceptors" ref="methodSecurityInterceptor" />
           </bean>
          methodSecurityInterceptor則是acegi中的攔截器(注:見下一篇acegi配置)

          6.Service配置
          <bean id="userService" parent="baseTxService">
              <property name="target">
                <bean class="com.rain.wsh.service.impl.UserServiceImpl"/>
              </property>
          </bean>

          注意:為了更加的是配置文件清晰,可以把spring的配置文件分成多個配置文件,例如(applicationContext-hibernate.xml,applicationContext-service.xml等),然后在web.xml中配置的時候寫成:
           <context-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>/WEB-INF/applicationContext*.xml</param-value>
           </context-param>



          posted on 2007-03-29 10:10 周銳 閱讀(463) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 闻喜县| 和硕县| 凤城市| 茂名市| 南安市| 马公市| 惠来县| 平泉县| 三台县| 武安市| 湖州市| 清丰县| 文山县| 资兴市| 襄樊市| 武城县| 临夏县| 江达县| 杭锦后旗| 漠河县| 柯坪县| 孟村| 郸城县| 铅山县| 汽车| 忻城县| 古田县| 绥德县| 崇义县| 集安市| 咸宁市| 延川县| 枣阳市| 安塞县| 峨边| 新民市| 汨罗市| 衡南县| 洛阳市| 合川市| 商南县|