上次折騰了半天,終于把延時加載配置好了。可是不配置事務總是覺得怪怪的。so..決定把事務也配置好。雖然是個小項目吧^_^.
<!-- 事務配置 -->
<!-- 事務管理器 用于hibernate的事務管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 事務攔截器 用于對攔截的方法開啟事務,其中指定了一些只讀事務-->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*list">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="display*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*display">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*view">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="main*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- 自動代理,配置使所有service層bean使用事務攔截器 -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor" ref="transactionInterceptor"/>
</bean>
<!-- 事務配置結束 -->
簡單說明一下,其中是用了spring提供的
BeanNameAutoProxyCreator這個自動代理服務,自動對名為XXXService的的bean使用使用攔截器開啟事務,而在transactionInterceptor則定義了事務的屬性,限定了一些只讀的事務以提搞效率。