applicationContext-db.xml
<bean id="commonOnlDAO" class="dap.dao.BaseDAO">
<property name="sessionFactory">
<ref bean="onlSessionFactory" />
</property>
</bean>
<bean id="commonHisDAO" class="dap.dao.BaseDAO">
<property name="sessionFactory">
<ref bean="hisSessionFactory" />
</property>
</bean>
两个DAO的定义,通常声明DAO的时候,都是用接口来声明来使spring注入
<bean id="txn1001BO" name="txn1001BO" class="dap.service.bo.validate.impl.AuthReqBOImpl" scope="prototype">
<property name="commonOnlDAO" ref="commonOnlDAO"/>
<property name="commonHisDAO" ref="commonHisDAO"/>定义bo层的操作时,需要注入的DAO定义
</bean>
<!-- 历史库数据源-->
<bean id="hisDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${dapHis.connection.driver_class}"/>
<property name="jdbcUrl" value="${dapHis.connection.url}"/>
<property name="idleConnectionTestPeriod" value="${dapHis.pool.c3p0.idle_connection_test_period}" />
<property name="properties">
<props>
<prop key="user">${dapHis.connection.username}</prop>
<prop key="password">${dapHis.connection.password}</prop>
<prop key="acquireIncrement">${dapHis.pool.c3p0.acquire_increment}</prop>
<prop key="maxPoolSize">${dapHis.pool.c3p0.max_size}</prop>
<prop key="minPoolSize">${dapHis.pool.c3p0.min_size}</prop>
<prop key="checkoutTimeout">3000</prop>
</props>
</property>
</bean>
<!-- 在线库数据源 -->
<bean id="onlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${dapOnl.connection.driver_class}"/>
<property name="jdbcUrl" value="${dapOnl.connection.url}"/>
<property name="idleConnectionTestPeriod" value="${dapOnl.pool.c3p0.idle_connection_test_period}" />
<property name="properties">
<props>
<prop key="user">${dapOnl.connection.username}</prop>
<prop key="password">${dapOnl.connection.password}</prop>
<prop key="acquireIncrement">${dapOnl.pool.c3p0.acquire_increment}</prop>
<prop key="maxPoolSize">${dapOnl.pool.c3p0.max_size}</prop>
<prop key="minPoolSize">${dapOnl.pool.c3p0.min_size}</prop>
<prop key="checkoutTimeout">3000</prop>
</props>
</property>
</bean>
<bean id="hisTransactionManager"历史库的事务处理声明 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="hisSessionFactory"/>
</property>
</bean>
<bean id="onlTransactionManager"在线库的事务处理声明 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="onlSessionFactory"/>
</property>
</bean>
<bean id="system.onlTransactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">在线库的事务处理声明
<ref bean="onlTransactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_SUPPORTS,-java.lang.Exception</prop>
<prop key="execut*">PROPAGATION_REQUIRED,-java.lang.Exception</prop>
处理所有execut开头的方法,如果报Exception错误就回滚。(+意思就是不回滚)
</props>
</property>
</bean>
<bean id="system.hisTransactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">历史库的事务拦截器
<ref bean="hisTransactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_SUPPORTS,-java.lang.Exception</prop>
<prop key="execut*">PROPAGATION_REQUIRED,-java.lang.Exception</prop>
<!--<prop key="load*">PROPAGATION_SUPPORTS,readOnly</prop>-->
</props>
</property>
</bean>
<bean id="onlSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="onlDataSource"/>
</property>
<property name="mappingResources">
<list>
<value>hbm/TblDapTxnLog.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>
<!-- 在log和console中打印出更漂亮的SQL -->
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
<prop key="hibernate.default_batch_fetch_size">16</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">30</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
<prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
</props>
</property>
</bean>
<bean id="hisSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="hisDataSource"/>
</property>
<property name="mappingResources">
<list>
<value>hbm/TblDapBlackCardInfo.hbm.xml</value>生成的类所对应的表一定要有主键
<value>hbm/TblDapCardInfo.hbm.xml</value>
<value>hbm/TblDapInstInfo.hbm.xml</value>
<value>hbm/TblDapSysParam.hbm.xml</value>
<value>hbm/TblDapUserCardMap.hbm.xml</value>
<value>hbm/TblDapUsrExtInfo.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>
<!-- 在log和console中打印出更漂亮的SQL -->
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
<prop key="hibernate.default_batch_fetch_size">16</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">30</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
<prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!--<property name="proxyTargetClass" value="true" />-->
当value="false"时,不能用类来声明需要注入的数据,一定要用接口来声明
<property name="beanNames">
<list>
<value>*BO</value>处理以BO结尾的bean,对应bo层的方法
</list>
</property>
<property name="interceptorNames">
<list>
<value>system.onlTransactionInterceptor</value>拦截器的名字一定要唯一
<value>system.hisTransactionInterceptor</value>
</list>
</property>
</bean>
以上配置的是配置bo层的东西,可以让spring来管理释放session。当然有时候有必要配置一个dao,在调用这个dao的时候不通过bo来注入,这个时候如果没有单独配置的话,就会有session不能释放的情况,导致应用崩溃。
<!-- onl-->
<bean id="onlhibernateInterceptor"
class="org.springframework.orm.hibernate3.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="onlSessionFactory" />
</property>
</bean>
<!-- onlTarget-->
<bean id="commonOnlDAOTarget" class="dap.dao.BaseDAO">
<property name="sessionFactory">
<ref bean="onlSessionFactory" />
</property>
</bean>
<!--onlDao-->
<bean id="commonOnlDAO" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>dap.dao.IBaseDAO</value>
</property>
<property name="interceptorNames">
<list>
<value> onlhibernateInterceptor </value>
<value> commonOnlDAOTarget </value>
</list>
</property>
</bean>
这样单独的配置一般用在一些公共查询中,在一些框架的里面也需要这种单独配置注入的DAO,所以掌握好以上两种注入基本不会碰到大问了~~