<beans default-autowire="byName"> <!--设置了红色部分后,service 和 dao等的属性就可以不用手工写了,会根据属性名字自动的找对应的 bean-->
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:com/tc/config/init.properties</value>
</property>
</bean>
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" dependency-check="none">
<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>
<property name="acquireIncrement">
<value>${c3p0.acquireIncrement}</value>
</property>
<property name="initialPoolSize">
<value>${c3p0.initialPoolSize}</value>
</property>
<property name="minPoolSize">
<value>${c3p0.minPoolSize}</value>
</property>
<property name="maxPoolSize">
<value>${c3p0.maxPoolSize}</value>
</property>
<property name="maxIdleTime">
<value>${c3p0.maxIdleTime}</value>
</property>
<property name="idleConnectionTestPeriod">
<value>${c3p0.idleConnectionTestPeriod}</value>
</property>
<property name="maxStatements">
<value>${c3p0.maxStatements}</value>
</property>
<property name="numHelperThreads">
<value>${c3p0.numHelperThreads}</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/tc/model</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.jdbc.fetch_size">
${hibernate.jdbc.fetch_size}
</prop>
<prop key="hibernate.jdbc.batch_size">
${hibernate.jdbc.batch_size}
</prop>
<!-- <prop key="hibernate.query.factory_class">
org.hibernate.hql.classic.ClassicQueryTranslatorFactory < ! - - #2.x - - >
<! - - org.hibernate.hql.ast.ASTQueryTranslatorFactory #3.x - - >
</prop> -->
</props>
</property>
</bean>
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="txProxyTemplate" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="myTransactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
<!-- <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> -->
</props>
</property>
</bean>
<!-- 这是一个自动代理,用来在被代理的bean 中指定的方法上加上事物 -->
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<idref local="txProxyTemplate" />
</list>
</property>
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
</bean>
<!-- service -->
<bean id="userService" class="com.tc.service.impl.UserServiceImp" />
<!-- dao -->
<bean id="userDao" class="com.tc.dao.impl.UsersDaoImp"/>
需要注意:dao中链接数据库不应该通过往dao注入transactionManager并使用之的方式,此方式spring事务管理无法管理到。正确的方式应该是让dao 集成于HibernateDaoSupport(mybatis则应该是 SqlSessionDaoSupport),然后调用HibernateDaoSupport 的getXxxxTemplet 方法(mybatis 则是SqlSessionDaoSupport 的 getSqlSessionTemplate() 方法),获取模板类,并调用模板类得方法操作数据库。原理则在于,这些support类在同一个线程内获取的数据库链接资源将是同一个,就好像将数据链接资源保存在LocalThread里一样。如此service代理类才能在service的方法的开头开启事务,结实时提交事务,并关闭链接。而在这开启和提交事务期间,service调用了dao类操作数据库,它们使用的链接资源是同一个。
以下是上面指定的com/tc/config/init.properties 的内容
datasource.type=mysql
datasource.driverClassName=com.mysql.jdbc.Driver
datasource.url=jdbc:mysql://10.10.11.124:3306/tcmimms?useUnicode=true&characterEncoding=utf-8
datasource.username=root
datasource.password=root
datasource.maxActive=10
datasource.maxIdle=2
datasource.maxWait=120000
datasource.whenExhaustedAction=1
datasource.validationQuery=select 1 from dual
datasource.testOnBorrow=true
datasource.testOnReturn=false
c3p0.acquireIncrement=3
c3p0.initialPoolSize=3
c3p0.idleConnectionTestPeriod=900
c3p0.minPoolSize=2
c3p0.maxPoolSize=30
c3p0.maxStatements=30
c3p0.numHelperThreads=10
c3p0.maxIdleTime=600
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
hibernate.jdbc.batch_size=25
hibernate.jdbc.fetch_size=50
hibernate.show_sql=true
hibernate.connection.release_mode=after_transaction
评论