接下来讲解下spring的配置文件:
<beans>
<!--定义读取属性配置文件,如数据库驱动等-->
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:system.properties</value>
</property>
</bean>
<!--定义数据源,id为dataSource。通常用的class为 org.apache.commons.dbcp.BasicDataSource,改为com.mchange.v2.c3p0.ComboPooledDataSource.在网上评论,前者的数据库管理类存在多个bug,
性能不是很好,建议用后者。-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!--配置数据库相关属性,从属性文件中读取-->
<property name="driverClass">
<value>${datasource.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${datasource.url}</value>
</property>
<property name="user">
<value>${datasource.userName}</value>
</property>
<property name="password">
<value>${datasource.password}</value>
</property>
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<property name="initialPoolSize" value="1"></property>
<property name="maxPoolSize" value="300"></property>
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="20"></property>
<property name="minPoolSize" value="1"></property>
<!-- 定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
<property name="acquireRetryAttempts" value="3"></property>
</bean>
<!-- 定义Hibernate的SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--依赖注入,真是上下文提供的bean-->
<property name="dataSource"><ref local="dataSource"/></property>
<!-- mappingResouces 属性用来列出全部映射文件 -->
<property name="mappingDirectoryLocations">
<!-- 以下用来列出所有的PO映射文件 -->
<list>
<value>classpath:/hbm</value>
</list>
</property>
<!-- 定义Hibernate的SessionFactory的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${datasource.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!--定义hibernate的事务管理类,此类为单列模式。一般情况下,默认的为单列模式。
还有prototype模式,允许spring创建多个实例-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!--定义代理类-->
<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop><!--定义操作的原子性-->
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="sync*">PROPAGATION_REQUIRED</prop>
<prop key="enable*">PROPAGATION_REQUIRED</prop>
<prop key="disable*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!--定义dao层-->
<!--根据名字自动注入-->
<bean id="bstDAOTarget" class="cn.com.zjtelecom.bst.dao.BSTDAO" autowire="byName"/>
<bean id="bstDAO" parent="txProxyTemplate">
<property name="target"><ref local="bstDAOTarget"/></property>
</bean>
</beans>
当有多个人开发时,将每个人用到的相应的dao层配置在其对应的spring的配置文件中,命名可以参考如下:spring-xxx.xml。这样做的好处是:每个人的开发不会相互干扰,只要一张基本的spring配置文件和自己相应的spring配置
文件,这个工程就可以跑起来。