2010年9月10日

Spring事务配置的五种方式

前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识。通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的。

总结如下:

Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。

DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为HibernateTransactionManager。

具体如下图:

Spring事务配置 (2)

根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:

第一种方式:每个Bean都有一个代理

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context
="http://www.springframework.org/schema/context"
xmlns:aop
="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
>

<bean id="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
bean>


<bean id="transactionManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
bean>


<bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
bean>

<bean id="userDao"
class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

<property name="transactionManager" ref="transactionManager" />
<property name="target" ref="userDaoTarget" />
<property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />

<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIREDprop>
props>
property>
bean>
beans>

第二种方式:所有Bean共享一个代理基类

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context
="http://www.springframework.org/schema/context"
xmlns:aop
="http://www.springframework.org/schema/aop"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
>

<bean id="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
bean>


<bean id="transactionManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
bean>

<bean id="transactionBase"
class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init
="true" abstract="true">

<property name="transactionManager" ref="transactionManager" />

<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIREDprop>
props>
property>
bean>


<bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
bean>

<bean id="userDao" parent="transactionBase" >
<property name="target" ref="userDaoTarget" />
bean>
beans>

第三种方式:使用拦截器

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context
="http://www.springframework.org/schema/context"
xmlns:aop
="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
>

<bean id="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
bean>


<bean id="transactionManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
bean>

<bean id="transactionInterceptor"
class
="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />

<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIREDprop>
props>
property>
bean>

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Daovalue>
list>
property>
<property name="interceptorNames">
<list>
<value>transactionInterceptorvalue>
list>
property>
bean>


<bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
bean>
beans>

第四种方式:使用tx标签配置的拦截器

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context
="http://www.springframework.org/schema/context"
xmlns:aop
="http://www.springframework.org/schema/aop"
xmlns:tx
="http://www.springframework.org/schema/tx"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>

<context:annotation-config />
<context:component-scan base-package="com.bluesky" />

<bean id="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
bean>


<bean id="transactionManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
tx:attributes>
tx:advice>

<aop:config>
<aop:pointcut id="interceptorPointCuts"
expression
="execution(* com.bluesky.spring.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref
="interceptorPointCuts" />
aop:config>
beans>

第五种方式:全注解

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context
="http://www.springframework.org/schema/context"
xmlns:aop
="http://www.springframework.org/schema/aop"
xmlns:tx
="http://www.springframework.org/schema/tx"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>

<context:annotation-config />
<context:component-scan base-package="com.bluesky" />

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
bean>


<bean id="transactionManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
bean>

beans>

此时在DAO上需加上@Transactional注解,如下:

package com.bluesky.spring.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import com.bluesky.spring.domain.User;

@Transactional
@Component(
"userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

public List<User> listUsers() {
return this.getSession().createQuery("from User").list();
}


}

2010年9月6日

COPY-軟件版本Beta,RC,Demo,Build,GA等是什麼意思呢?

COPY-軟件版本Beta,RC,Demo,Build,GA等是什麼意思呢?
2009/02/23 10:40
Alpha:是內部測試版,一般不向外部發佈,會有很多Bug.一般只有測試人員使用。
Beta:也是測試版,這個階段的版本會一直加入新的功能。在Alpha版之後推出。
RC:(Release Candidate)顧名思義麼 ! 用在軟件上就是候選版本。系統平台上就是發行候選版本。RC版不會再加入新的功能了,主要著重於除錯。
RTM:(Release to Manufacture)是給工廠大量壓片的版本,內容跟正式版是一樣的,不過RTM版也有出限制、評估版的。但是和正式版本的主要程序代碼都是一樣的。
OEM:是給計算機廠商隨著計算機販賣的,也就是隨機版。只能隨機器出貨,不能零售。只能全新安裝,不能從舊有操作系統升級。包裝不像零售版精美,通常只有一面CD和說明書(授權書)。
RVL:號稱是正式版,其實RVL根本不是版本的名稱。它是中文版/英文版文檔破解出來的。
EVAL:而流通在網絡上的EVAL版,與「評估版」類似,功能上和零售版沒有區別。
RTL:Retail(零售版)是真正的正式版,正式上架零售版。在安裝盤的i386文件夾裡有一個eula.txt,最後有一行 EULAID,就是你的版本。比如簡體中文正式版是EULAID:WX.4_PRO_RTL_CN,繁體中文正式版是WX.4_PRO_RTL_TW。其中:如果是WX.開頭是正式版,WB.開頭是測試版。_PRE,代表家庭版;_PRO,代表專業版。

版本號:
V(Version):即版本,通常用數字表示版本號。(如:EVEREST Ultimate v4.20.1188 Beta )
Build:用數字或日期標示版本號的一種方式。(如:VeryCD eMule v0.48a Build 071112)
SP:Service Pack,升級包。(如:Windows XP SP 2/Vista SP 1)

授權和功能劃分:
Trial:試用版,通常都有時間限制,有些試用版軟件還在功能上做了一定的限制。可註冊或購買成為正式版
Unregistered:未註冊版,通常沒有時間限制,在功能上相對於正式版做了一定的限制。可註冊或購買成為正式版。
Demo:演示版,僅僅集成了正式版中的幾個功能,不能升級成正式版。
Lite:精簡版。
Full version:完整版,屬於正式版。

語言劃分:
SC:Simplified Chinese簡體中文版。
CN : 簡體中文版
GBK:簡體中文漢字內碼擴展規範版。
TC:Traditional Chinese繁體中文版。
CHT : 繁體中文版
BIG5:繁體中文大五碼版。
EN : 英文版
Multilanguage : 多語言版
UTF8:Unicode Transformation Format 8 bit,對現有的中文系統不是好的解決方案。

開發階段劃分:
α(Alpha)版:內測版,內部交流或者專業測試人員測試用。Bug較多,普通用戶最好不要安裝。
β(Beta)版:公測版,專業愛好者大規模測試用,存在一些缺陷,該版本也不適合一般用戶安裝。
γ(Gamma)版:相當成熟的測試版,與即將發行的正式版相差無幾。
RC版:Release Candidate。
RC 版。是 Release Candidate 的縮寫,意思是發佈倒計時,候選版本,處於Gamma階段,該版本已經完成全部功能並清除大部分的 BUG。到了這個階段只會除BUG,不會對軟件做任何大的更改。從Alpha到Beta再到Gamma是改進的先後關係,但RC1、RC2往往是取捨關係。
Final:正式版。

其他版本
Enhance :增強版或者加強版 屬於正式版1
Free :自由版
Release :發行版 有時間限制
Upgrade :升級版
Retail  :零售版
Cardware :屬共享軟件的一種,只要給作者回覆一封電郵或明信片即可。(有的作者並由此提供註冊碼等),目前這種形式已不多見。/ S
Plus :屬增強版,不過這種大部分是在程序界面及多媒體功能上增強。
Preview :預覽版
Corporation & Enterprise :企業版
Standard :標準版
Mini :迷你版也叫精簡版只有最基本的功能
Premium : 貴價版
Professional : 專業版
Express : 特別版
Deluxe : 豪華版
Regged : 已註冊版
Rip :是指從原版文件(一般是指光盤或光盤鏡像文件)直接將有用的內容(核心內容)分離出來,剔除無用的文檔,例如PDF說明文件啊,視頻演示啊之類的東西,也可以算做是精簡版吧…但主要內容功能是一點也不能缺少的!另:DVDrip是指將視頻和音頻直接從DVD光盤裡以文件方式分離出來。
RTM 版 :這基本就是最終的版本,英文是 Release To Manufactur,意思是發佈到生產商。


RC就是Release Candidate(候選版本)的簡稱
GA:General Availability,正式發佈的版本,在國外都是用GA來說明release版本的