spring一些知識

Spring容器配置

1.定義數據源(datasource)
2.通過依賴注入datasource創建sessionFactory
3.通過依賴注入sessionFactory創建hibernateTemplate
4.通過依賴注入hibernateTemplate創建dao
5.通過依賴注入dao創建service
6.通過依賴注入service創建action

配置事務控制
 <!--定義Hibernate的事務管理器HibernateTransactionManager-->
    <beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
       <!-- 依賴注入上面定義的sessionFactory-->
       <property name="sessionFactory"ref="sessionFactory"/>
   </bean>

    <!--定義一個事務通知txAdvice,配置事務的傳播特性 -->
<tx:advice id="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<!-- 所有以browse、list、load、get及is開頭的業務邏輯方法均不需要事務控制且只讀 -->
<tx:method name="browse*"propagation="NOT_SUPPORTED" read-only="true"/>
<tx:method name="list*"propagation="NOT_SUPPORTED" read-only="true"/>
<tx:method name="load*"propagation="NOT_SUPPORTED" read-only="true"/>
<tx:method name="get*"propagation="NOT_SUPPORTED" read-only="true"/>
<tx:method name="is*"propagation="NOT_SUPPORTED" read-only="true"/>
<!-- 設置所有方法均進行事務控制,如果當前沒有事務,則新建一個事務-->
<tx:method name="*"propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 基於AOP技術的事務管理實現 -->
<aop:config>
<!--定義一個事務切入點,攔截com.eportal.service.impl包中所有類的所有方法-->
<aop:pointcut id="transactionPointcut"expression="execution(* com.eportal.service.impl.*.*(..))"/>
<!-- 引用txAdvice事務通知 -->
<aop:advisor advice-ref="txAdvice"pointcut-ref="transactionPointcut"/>
</aop:config>

Spring當中常用事務類型
REQUERED:支持當前事務,如果當前沒有事務,則新建事務,常用
SUPPORTS:支持當前事務,如果當前沒有事務,就以非事務方式執行
MANDATORY:支持當前事務,如果當前沒有事務,就拋出異常
REQUIRES-NEW:新建一個事務,如果當前存在事務,則將當前事務掛起
NOT-SUPPORTED:以非事務方式執行,如果當前存在事務,則掛起當前事務
NEVER和NOT-SUPPORTED茶不錯,以非事務方式執行,區別在於如果當前存在事務,則拋出異常
NESTED:如果當前存在事務,則嵌套在事務內執行;如果當前沒有事務,則執行REQUIRED相同操作。


Table 4.3. Bean scopes

Scope Description
singleton (Default) Scopes a single bean definition to asingle object instance per Spring IoC container.

prototype Scopes a single bean definition to any number ofobject instances.

request Scopes a single bean definition to the lifecycle of asingle HTTP request; that is, each HTTP request has its owninstance of a bean created off the back of a single beandefinition. Only valid in the context of a web-aware SpringApplicationContext.

session Scopes a single bean definition to the lifecycle of anHTTP Session. Only valid in the context of a web-aware SpringApplicationContext.

global session Scopes a single bean definition to thelifecycle of a global HTTP Session. Typically only valid when usedin a portlet context. Only valid in the context of a web-awareSpring ApplicationContext.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章