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 周锐 阅读(456) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航: