﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-纸飞机-文章分类-Spring</title><link>http://www.blogjava.net/jxhkwhy/category/43640.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 18 Jan 2010 11:56:50 GMT</lastBuildDate><pubDate>Mon, 18 Jan 2010 11:56:50 GMT</pubDate><ttl>60</ttl><item><title>Spring 框架简介(转)</title><link>http://www.blogjava.net/jxhkwhy/articles/309974.html</link><dc:creator>纸飞机</dc:creator><author>纸飞机</author><pubDate>Mon, 18 Jan 2010 09:06:00 GMT</pubDate><guid>http://www.blogjava.net/jxhkwhy/articles/309974.html</guid><wfw:comment>http://www.blogjava.net/jxhkwhy/comments/309974.html</wfw:comment><comments>http://www.blogjava.net/jxhkwhy/articles/309974.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jxhkwhy/comments/commentRss/309974.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jxhkwhy/services/trackbacks/309974.html</trackback:ping><description><![CDATA[<table border="0" cellspacing="0" cellpadding="0" width="100%">
    <tbody>
        <tr valign="top">
            <td width="100%">
            <p id="subtitle"><em>Spring AOP 和 IOC 容器入门</em></p>
            <img class="display-img" alt="" src="http://www.ibm.com/i/c.gif" width="1" height="6" /></td>
            <td class="no-print" width="192"><img alt="developerWorks" src="http://www.ibm.com/developerworks/i/dw.gif" width="192" height="18" /></td>
        </tr>
    </tbody>
</table>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
    <tbody>
        <tr valign="top">
            <td width="10"><img alt="" src="http://www.ibm.com/i/c.gif" width="10" height="1" /></td>
            <td width="100%">
            <table class="no-print" border="0" cellspacing="0" cellpadding="0" width="160" align="right">
                <tbody>
                    <tr>
                        <td width="10"><img alt="" src="http://www.ibm.com/i/c.gif" width="10" height="1" /></td>
                        <td>
                        <table border="0" cellspacing="0" cellpadding="0" width="150">
                            <tbody>
                                <tr>
                                    <td class="v14-header-1-small">文档选项</td>
                                </tr>
                            </tbody>
                        </table>
                        <table class="v14-gray-table-border" border="0" cellspacing="0" cellpadding="0">
                            <tbody>
                                <tr>
                                    <td class="no-padding" width="150">
                                    <table border="0" cellspacing="0" cellpadding="0" width="143">
                                        <script language="JavaScript" type="text/javascript">
<!-- document.write('<tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt="" /></td><td width="16"><img alt="将打印机的版面设置成横向打印模式" height="16" src="//www.ibm.com/i/v14/icons/printer.gif" width="16" vspace="3"  /></td><td width="122"><p><strong><a class="smallplainlink" href="javascript:print()">打印本页</a></strong></p></td></tr>');
//-->
</script><noscript></noscript>
                                        <form name="email" action="https://www.ibm.com/developerworks/secure/email-it.jsp">
                                            <input value="在这由三部分组成的介绍 Spring 框架的系列文章的第一期中，将开始学习如何用 Spring 技术构建轻量级的、强壮的 J2EE 应用程序。" type="hidden" name="body" /><input value="Spring 系列: Spring 框架简介" type="hidden" name="subject" /><input value="cn" type="hidden" name="lang" /> <script language="JavaScript" type="text/javascript">
<!-- document.write('<tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt="" /></td><td width="16"><img src="//www.ibm.com/i/v14/icons/em.gif" height="16" width="16" vspace="3" alt="将此页作为电子邮件发送"  /></td><td width="122"><p><a class="smallplainlink" href="javascript:document.email.submit();"><strong>将此页作为电子邮件发送</strong></a></p></td></tr>');
//-->
</script><noscript></noscript>
                                        </form>
                                        <tbody>
                                            <tr valign="top">
                                                <td width="8"><img alt="" src="http://www.ibm.com/i/c.gif" width="8" height="1" /></td>
                                                <td width="16"><img border="0" alt="" vspace="3" src="http://www.ibm.com/i/v14/icons/dn.gif" width="16" height="16" /></td>
                                                <td width="122">
                                                <p><a class="smallplainlink" href="http://www.ibm.com/developerworks/cn/java/wa-spring1/#download"><strong>样例代码</strong></a></p>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <!--start RESERVED FOR FUTURE USE INCLUDE FILES--><!-- this content will be automatically generated across all content areas --><!--end RESERVED FOR FUTURE USE INCLUDE FILES--><br />
                        </td>
                    </tr>
                </tbody>
            </table>
            <p>级别： 初级</p>
            <p><a href="http://www.ibm.com/developerworks/cn/java/wa-spring1/#author">Naveen Balani</a> (<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#110;&#97;&#118;&#101;&#101;&#110;&#98;&#97;&#108;&#97;&#110;&#105;&#64;&#114;&#101;&#100;&#105;&#102;&#102;&#109;&#97;&#105;&#108;&#46;&#99;&#111;&#109;&#63;&#115;&#117;&#98;&#106;&#101;&#99;&#116;&#61;&#83;&#112;&#114;&#105;&#110;&#103;&#32;&#26694;&#26550;&#31616;&#20171;&#38;&#97;&#109;&#112;&#59;&#99;&#99;&#61;&#104;&#116;&#99;&#64;&#117;&#115;&#46;&#105;&#98;&#109;&#46;&#99;&#111;&#109;">naveenbalani@rediffmail.com</a>), 技术架构师, Webify Solutions<br />
            </p>
            <p>2005 年 8 月 18 日</p>
            <blockquote>在这由三部分组成的介绍 Spring 框架的系列文章的第一期中，将开始学习如何用 Spring 技术构建轻量级的、强壮的 J2EE 应用程序。developerWorks 的定期投稿人 Naveen Balani 通过介绍 Spring 框架开始了他由三部分组成的 <em>Spring 系列</em>，其中还将介绍 Spring 面向方面的编程（AOP）和控制反转（IOC）容器。</blockquote><!--start RESERVED FOR FUTURE USE INCLUDE FILES--><!-- include java script once we verify teams wants to use this and it will work on dbcs and cyrillic characters --><!--end RESERVED FOR FUTURE USE INCLUDE FILES-->
            <p>Spring 是一个开源框架，是为了解决企业应用程序开发复杂性而创建的。框架的主要优势之一就是其分层架构，分层架构允许您选择使用哪一个组件，同时为 J2EE 应用程序开发提供集成的框架。</p>
            <p>在这篇由三部分组成的 <em>Spring 系列</em> 的第 1 部分中，我将介绍 Spring 框架。我先从框架底层模型的角度描述该框架的功能，然后将讨论两个最有趣的模块：Spring 面向方面编程（AOP）和控制反转 （IOC） 容器。接着将使用几个示例演示 IOC 容器在典型应用程序用例场景中的应用情况。这些示例还将成为本系列后面部分进行的展开式讨论的基础，在本文的后面部分，将介绍 Spring 框架通过 Spring AOP 实现 AOP 构造的方式。</p>
            <p>请参阅 <a href="http://www.ibm.com/developerworks/cn/java/wa-spring1/#download">下载</a>，下载 Spring 框架和 Apache Ant，运行本系列的示例应用程序需要它们。</p>
            <p><a name="1"><span class="atitle">Spring 框架</span></a></p>
            <p>Spring 框架是一个分层架构，由 7 个定义良好的模块组成。Spring 模块构建在核心容器之上，核心容器定义了创建、配置和管理 bean 的方式，如图 1 所示。</p>
            <br />
            <a name="fig1"><strong>图 1. Spring 框架的 7 个模块</strong></a><br />
            <img alt="Spring 框架图示" src="http://www.ibm.com/developerworks/cn/java/wa-spring1/spring_framework.gif" width="555" height="288" /> <br />
            <p>组成 Spring 框架的每个模块（或组件）都可以单独存在，或者与其他一个或多个模块联合实现。每个模块的功能如下：
            <ul>
                <li><strong>核心容器</strong>：核心容器提供 Spring 框架的基本功能。核心容器的主要组件是 <code>BeanFactory</code>，它是工厂模式的实现。<code>BeanFactory</code> 使用<em>控制反转</em> （IOC） 模式将应用程序的配置和依赖性规范与实际的应用程序代码分开。</li>
                <li><strong>Spring 上下文</strong>：Spring 上下文是一个配置文件，向 Spring 框架提供上下文信息。Spring 上下文包括企业服务，例如 JNDI、EJB、电子邮件、国际化、校验和调度功能。</li>
                <li><strong>Spring AOP</strong>：通过配置管理特性，Spring AOP 模块直接将面向方面的编程功能集成到了 Spring 框架中。所以，可以很容易地使 Spring 框架管理的任何对象支持 AOP。Spring AOP 模块为基于 Spring 的应用程序中的对象提供了事务管理服务。通过使用 Spring AOP，不用依赖 EJB 组件，就可以将声明性事务管理集成到应用程序中。</li>
                <li><strong>Spring DAO</strong>：JDBC DAO 抽象层提供了有意义的异常层次结构，可用该结构来管理异常处理和不同数据库供应商抛出的错误消息。异常层次结构简化了错误处理，并且极大地降低了需要编写的异常代码数量（例如打开和关闭连接）。Spring DAO 的面向 JDBC 的异常遵从通用的 DAO 异常层次结构。</li>
                <li><strong>Spring ORM</strong>：Spring 框架插入了若干个 ORM 框架，从而提供了 ORM 的对象关系工具，其中包括 JDO、Hibernate 和 iBatis SQL Map。所有这些都遵从 Spring 的通用事务和 DAO 异常层次结构。</li>
                <li><strong>Spring Web 模块</strong>：Web 上下文模块建立在应用程序上下文模块之上，为基于 Web 的应用程序提供了上下文。所以，Spring 框架支持与 Jakarta Struts 的集成。Web 模块还简化了处理多部分请求以及将请求参数绑定到域对象的工作。</li>
                <li><strong>Spring MVC 框架</strong>：MVC 框架是一个全功能的构建 Web 应用程序的 MVC 实现。通过策略接口，MVC 框架变成为高度可配置的，MVC 容纳了大量视图技术，其中包括 JSP、Velocity、Tiles、iText 和 POI。</li>
            </ul>
            <p>&nbsp;</p>
            <p>Spring 框架的功能可以用在任何 J2EE 服务器中，大多数功能也适用于不受管理的环境。Spring 的核心要点是：支持不绑定到特定 J2EE 服务的可重用业务和数据访问对象。毫无疑问，这样的对象可以在不同 J2EE 环境 （Web 或 EJB）、独立应用程序、测试环境之间重用。 </p>
            <br />
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                <tbody>
                    <tr>
                        <td><img alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" height="1" /><br />
                        <img border="0" alt="" src="http://www.ibm.com/i/c.gif" width="8" height="6" /></td>
                    </tr>
                </tbody>
            </table>
            <table class="no-print" cellspacing="0" cellpadding="0" align="right">
                <tbody>
                    <tr align="right">
                        <td><img alt="" src="http://www.ibm.com/i/c.gif" width="100%" height="4" /><br />
                        <table border="0" cellspacing="0" cellpadding="0">
                            <tbody>
                                <tr>
                                    <td valign="middle"><img border="0" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" height="16" /><br />
                                    </td>
                                    <td valign="top" align="right"><a class="fbox" href="http://www.ibm.com/developerworks/cn/java/wa-spring1/#main"><strong>回页首</strong></a></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br />
            <br />
            <p><a name="2"><span class="atitle">IOC 和 AOP</span></a></p>
            <p>控制反转模式（也称作依赖性介入）的基本概念是：不创建对象，但是描述创建它们的方式。在代码中不直接与对象和服务连接，但在配置文件中描述哪一个组件需要哪一项服务。容器 （在 Spring 框架中是 IOC 容器） 负责将这些联系在一起。</p>
            <p>在典型的 IOC 场景中，容器创建了所有对象，并设置必要的属性将它们连接在一起，决定什么时间调用方法。下表列出了 IOC 的一个实现模式。</p>
            <table border="1" cellspacing="4" cellpadding="2" width="80%">
                <tbody>
                    <tr valign="top">
                        <td>类型 1</td>
                        <td>服务需要实现专门的接口，通过接口，由对象提供这些服务，可以从对象查询依赖性（例如，需要的附加服务）</td>
                    </tr>
                    <tr valign="top">
                        <td>类型 2</td>
                        <td>通过 JavaBean 的属性（例如 setter 方法）分配依赖性</td>
                    </tr>
                    <tr valign="top">
                        <td>类型 3</td>
                        <td>依赖性以构造函数的形式提供，不以 JavaBean 属性的形式公开</td>
                    </tr>
                </tbody>
            </table>
            <p>Spring 框架的 IOC 容器采用类型 2 和类型3 实现。</p>
            <p><a name="N100FB"><span class="smalltitle">面向方面的编程</span></a></p>
            <p><em>面向方面的编程</em>，即 AOP，是一种编程技术，它允许程序员对横切关注点或横切典型的职责分界线的行为（例如日志和事务管理）进行模块化。AOP 的核心构造是<em>方面</em>，它将那些影响多个类的行为封装到可重用的模块中。</p>
            <p>AOP 和 IOC 是补充性的技术，它们都运用模块化方式解决企业应用程序开发中的复杂问题。在典型的面向对象开发方式中，可能要将日志记录语句放在所有方法和 Java 类中才能实现日志功能。在 AOP 方式中，可以反过来将日志服务<em>模块化</em>，并以声明的方式将它们应用到需要日志的组件上。当然，优势就是 Java 类不需要知道日志服务的存在，也不需要考虑相关的代码。所以，用 Spring AOP 编写的应用程序代码是松散耦合的。</p>
            <p>AOP 的功能完全集成到了 Spring 事务管理、日志和其他各种特性的上下文中。</p>
            <br />
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                <tbody>
                    <tr>
                        <td><img alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" height="1" /><br />
                        <img border="0" alt="" src="http://www.ibm.com/i/c.gif" width="8" height="6" /></td>
                    </tr>
                </tbody>
            </table>
            <table class="no-print" cellspacing="0" cellpadding="0" align="right">
                <tbody>
                    <tr align="right">
                        <td><img alt="" src="http://www.ibm.com/i/c.gif" width="100%" height="4" /><br />
                        <table border="0" cellspacing="0" cellpadding="0">
                            <tbody>
                                <tr>
                                    <td valign="middle"><img border="0" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" height="16" /><br />
                                    </td>
                                    <td valign="top" align="right"><a class="fbox" href="http://www.ibm.com/developerworks/cn/java/wa-spring1/#main"><strong>回页首</strong></a></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br />
            <br />
            <p><a name="3"><span class="atitle">IOC 容器</span></a></p>
            <p>Spring 设计的核心是 <code>org.springframework.beans</code> 包，它的设计目标是与 JavaBean 组件一起使用。这个包通常不是由用户直接使用，而是由服务器将其用作其他多数功能的底层中介。下一个最高级抽象是 <code>BeanFactory</code> 接口，它是工厂设计模式的实现，允许通过名称创建和检索对象。<code>BeanFactory</code> 也可以管理对象之间的关系。</p>
            <p><code>BeanFactory</code> 支持两个对象模型。</p>
            <ul>
                <li><strong>单态</strong> 模型提供了具有特定名称的对象的共享实例，可以在查询时对其进行检索。Singleton 是默认的也是最常用的对象模型。对于无状态服务对象很理想。</li>
                <li><strong>原型</strong> 模型确保每次检索都会创建单独的对象。在每个用户都需要自己的对象时，原型模型最适合。</li>
            </ul>
            <p>bean 工厂的概念是 Spring 作为 IOC 容器的基础。IOC 将处理事情的责任从应用程序代码转移到框架。正如我将在下一个示例中演示的那样，Spring 框架使用 JavaBean 属性和配置数据来指出必须设置的依赖关系。</p>
            <p><a name="N10143"><span class="smalltitle">BeanFactory 接口</span></a></p>
            <p>因为 <code>org.springframework.beans.factory.BeanFactory</code> 是一个简单接口，所以可以针对各种底层存储方法实现。最常用的 <code>BeanFactory</code> 定义是 <code>XmlBeanFactory</code>，它根据 XML 文件中的定义装入 bean，如清单 1 所示。</p>
            <br />
            <a name="N10159"><strong>清单 1. XmlBeanFactory</strong></a><br />
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                <tbody>
                    <tr>
                        <td class="code-outline">
                        <pre class="displaycode">BeanFactory factory = new XMLBeanFactory(new FileInputSteam("mybean.xml"));
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br />
            <p>在 XML 文件中定义的 Bean 是被消极加载的，这意味在需要 bean 之前，bean 本身不会被初始化。要从 <code>BeanFactory</code> 检索 bean，只需调用 <code>getBean()</code> 方法，传入将要检索的 bean 的名称即可，如清单 2 所示。</p>
            <br />
            <a name="N1016F"><strong>清单 2. getBean()</strong></a><br />
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                <tbody>
                    <tr>
                        <td class="code-outline">
                        <pre class="displaycode">MyBean mybean = (MyBean) factory.getBean("mybean");
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br />
            <p>每个 bean 的定义都可以是 POJO （用类名和 JavaBean 初始化属性定义） 或 <code>FactoryBean</code>。<code>FactoryBean</code> 接口为使用 Spring 框架构建的应用程序添加了一个间接的级别。</p>
            <br />
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                <tbody>
                    <tr>
                        <td><img alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" height="1" /><br />
                        <img border="0" alt="" src="http://www.ibm.com/i/c.gif" width="8" height="6" /></td>
                    </tr>
                </tbody>
            </table>
            <table class="no-print" cellspacing="0" cellpadding="0" align="right">
                <tbody>
                    <tr align="right">
                        <td><img alt="" src="http://www.ibm.com/i/c.gif" width="100%" height="4" /><br />
                        <table border="0" cellspacing="0" cellpadding="0">
                            <tbody>
                                <tr>
                                    <td valign="middle"><img border="0" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" height="16" /><br />
                                    </td>
                                    <td valign="top" align="right"><a class="fbox" href="http://www.ibm.com/developerworks/cn/java/wa-spring1/#main"><strong>回页首</strong></a></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br />
            <br />
            <p><a name="4"><span class="atitle">IOC 示例</span></a></p>
            <p>理解控制反转最简单的方式就是看它的实际应用。在对由三部分组成的 <em>Spring 系列</em> 的第 1 部分进行总结时，我使用了一个示例，演示了如何通过 Spring IOC 容器注入应用程序的依赖关系（而不是将它们构建进来）。</p>
            <p>我用开启在线信用帐户的用例作为起点。对于该实现，开启信用帐户要求用户与以下服务进行交互：
            <ul>
                <li>信用级别评定服务，查询用户的信用历史信息。</li>
                <li>远程信息链接服务，插入客户信息，将客户信息与信用卡和银行信息连接起来，以进行自动借记（如果需要的话）。</li>
                <li>电子邮件服务，向用户发送有关信用卡状态的电子邮件。</li>
            </ul>
            <p>&nbsp;</p>
            <br />
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                <tbody>
                    <tr>
                        <td><img alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" height="1" /><br />
                        <img border="0" alt="" src="http://www.ibm.com/i/c.gif" width="8" height="6" /></td>
                    </tr>
                </tbody>
            </table>
            <table class="no-print" cellspacing="0" cellpadding="0" align="right">
                <tbody>
                    <tr align="right">
                        <td><img alt="" src="http://www.ibm.com/i/c.gif" width="100%" height="4" /><br />
                        <table border="0" cellspacing="0" cellpadding="0">
                            <tbody>
                                <tr>
                                    <td valign="middle"><img border="0" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" height="16" /><br />
                                    </td>
                                    <td valign="top" align="right"><a class="fbox" href="http://www.ibm.com/developerworks/cn/java/wa-spring1/#main"><strong>回页首</strong></a></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br />
            <br />
            <p><a name="5"><span class="atitle">三个接口</span></a></p>
            <p>对于这个示例，我假设服务已经存在，理想的情况是用松散耦合的方式把它们集成在一起。以下清单显示了三个服务的应用程序接口。</p>
            <br />
            <a name="N101AB"><strong>清单 3. CreditRatingInterface</strong></a><br />
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                <tbody>
                    <tr>
                        <td class="code-outline">
                        <pre class="displaycode">public interface CreditRatingInterface {
                        public boolean getUserCreditHistoryInformation(ICustomer iCustomer);
                        }
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br />
            <p>清单 3 所示的信用级别评定接口提供了信用历史信息。它需要一个包含客户信息的 <code>Customer</code> 对象。该接口的实现是由 <code>CreditRating</code> 类提供的。</p>
            <br />
            <a name="N101C1"><strong>清单 4. CreditLinkingInterface</strong></a><br />
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                <tbody>
                    <tr>
                        <td class="code-outline">
                        <pre class="displaycode">public interface CreditLinkingInterface {
                        public String getUrl();
                        public void setUrl(String url);
                        public void linkCreditBankAccount() throws Exception ;
                        }
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br />
            <p>信用链接接口将信用历史信息与银行信息（如果需要的话）连接在一起，并插入用户的信用卡信息。信用链接接口是一个远程服务，它的查询是通过 <code>getUrl()</code> 方法进行的。URL 由 Spring 框架的 bean 配置机制设置，我稍后会讨论它。该接口的实现是由 <code>CreditLinking</code> 类提供的。</p>
            <br />
            <a name="N101D7"><strong>清单 5. EmailInterface</strong></a><br />
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                <tbody>
                    <tr>
                        <td class="code-outline">
                        <pre class="displaycode">public interface EmailInterface {
                        public void sendEmail(ICustomer iCustomer);
                        public String getFromEmail();
                        public void setFromEmail(String fromEmail) ;
                        public String getPassword();
                        public void setPassword(String password) ;
                        public String getSmtpHost() ;
                        public void setSmtpHost(String smtpHost);
                        public String getUserId() ;
                        public void setUserId(String userId);
                        }
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br />
            <p><code>EmailInterface</code> 负责向客户发送关于客户信用卡状态的电子邮件。邮件配置参数（例如 SMPT 主机、用户名、口令）由前面提到的 bean 配置机制设置。<code>Email</code> 类提供了该接口的实现。</p>
            <br />
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                <tbody>
                    <tr>
                        <td><img alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" height="1" /><br />
                        <img border="0" alt="" src="http://www.ibm.com/i/c.gif" width="8" height="6" /></td>
                    </tr>
                </tbody>
            </table>
            <table class="no-print" cellspacing="0" cellpadding="0" align="right">
                <tbody>
                    <tr align="right">
                        <td><img alt="" src="http://www.ibm.com/i/c.gif" width="100%" height="4" /><br />
                        <table border="0" cellspacing="0" cellpadding="0">
                            <tbody>
                                <tr>
                                    <td valign="middle"><img border="0" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" height="16" /><br />
                                    </td>
                                    <td valign="top" align="right"><a class="fbox" href="http://www.ibm.com/developerworks/cn/java/wa-spring1/#main"><strong>回页首</strong></a></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br />
            <br />
            <p><a name="6"><span class="atitle">Spring 使其保持松散</span></a></p>
            <p>这些接口就位之后，接下来要考虑的就是如何用松散耦合方式将它们集成在一起。在 <a href="http://www.ibm.com/developerworks/cn/java/wa-spring1/listing1.html">清单 6</a> 中可以看到信用卡帐户用例的实现。</p>
            <p>注意，所有的 setter 方法都是由 Spring 的配置 bean 实现的。所有的依赖关系 （也就是三个接口）都可以由 Spring 框架用这些 bean 注入。<code>createCreditCardAccount()</code> 方法会用服务去执行其余实现。在 <a href="http://www.ibm.com/developerworks/cn/java/wa-spring1/listing2.html">清单 7 </a>中可以看到 Spring 的配置文件。我用箭头突出了这些定义。 </p>
            <br />
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                <tbody>
                    <tr>
                        <td><img alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" height="1" /><br />
                        <img border="0" alt="" src="http://www.ibm.com/i/c.gif" width="8" height="6" /></td>
                    </tr>
                </tbody>
            </table>
            <table class="no-print" cellspacing="0" cellpadding="0" align="right">
                <tbody>
                    <tr align="right">
                        <td><img alt="" src="http://www.ibm.com/i/c.gif" width="100%" height="4" /><br />
                        <table border="0" cellspacing="0" cellpadding="0">
                            <tbody>
                                <tr>
                                    <td valign="middle"><img border="0" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" height="16" /><br />
                                    </td>
                                    <td valign="top" align="right"><a class="fbox" href="http://www.ibm.com/developerworks/cn/java/wa-spring1/#main"><strong>回页首</strong></a></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br />
            <br />
            <p><a name="7"><span class="atitle">运行应用程序</span></a></p>
            <p>要运行示例应用程序，首先必须 <a href="http://prdownloads.sourceforge.net/" target="_new">下载 Spring 框架</a> 及其所有依赖文件。接下来，将框架释放到（比如说）磁盘 <em>c:\</em>，这会创建 <em>C:\spring-framework-1.2-rc2</em> （适用于当前发行版本） 这样的文件夹。在继续后面的操作之前，还必须下载和释放 <a href="http://ant.apache.org/" target="_new">Apache Ant</a>。</p>
            <p>接下来，将源代码释放到文件夹，例如 <em>c:\</em> 盘，然后创建 <em>SpringProject</em>。将 Spring 库（即 <em>C:\spring-framework-1.2-rc2\dist</em> 下的 <em>spring.jar</em> 和 <em>C:\spring-framework-1.2-rc2\lib\jakarta-commons</em> 下的 <em>commons-logging.jar</em>）复制到 <em>SpringProject\lib</em> 文件夹中。完成这些工作之后，就有了必需的构建依赖关系集。</p>
            <p>打开命令提示符，将当前目录切换到 <em>SpringProject</em>，在命令提示符中输入以下命令：<code>build</code>。 </p>
            <p>这会构建并运行 <code>CreateCreditAccountClient</code> 类，类的运行将创建 <code>Customer</code> 类对象并填充它，还会调用 <code>CreateCreditCardAccount</code> 类创建并链接信用卡帐户。<code>CreateCreditAccountClient</code> 还会通过 <code>ClassPathXmlApplicationContext</code> 装入 Spring 配置文件。装入 bean 之后，就可以通过 <code>getBean()</code> 方法访问它们了，如清单 8 所示。</p>
            <br />
            <a name="code8"><strong>清单 8. 装入 Spring 配置文件</strong></a><br />
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                <tbody>
                    <tr>
                        <td class="code-outline">
                        <pre class="displaycode">ClassPathXmlApplicationContext appContext =
                        new ClassPathXmlApplicationContext(new String[] {
                        "springexample-creditaccount.xml"
                        });
                        CreateCreditCardAccountInterface creditCardAccount =
                        (CreateCreditCardAccountInterface)
                        appContext.getBean("createCreditCard");
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br />
            <br />
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                <tbody>
                    <tr>
                        <td><img alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" height="1" /><br />
                        <img border="0" alt="" src="http://www.ibm.com/i/c.gif" width="8" height="6" /></td>
                    </tr>
                </tbody>
            </table>
            <table class="no-print" cellspacing="0" cellpadding="0" align="right">
                <tbody>
                    <tr align="right">
                        <td><img alt="" src="http://www.ibm.com/i/c.gif" width="100%" height="4" /><br />
                        <table border="0" cellspacing="0" cellpadding="0">
                            <tbody>
                                <tr>
                                    <td valign="middle"><img border="0" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" height="16" /><br />
                                    </td>
                                    <td valign="top" align="right"><a class="fbox" href="http://www.ibm.com/developerworks/cn/java/wa-spring1/#main"><strong>回页首</strong></a></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br />
            <br />
            <p><a name="8"><span class="atitle">结束语</span></a></p>
            <p>在这篇由三部分组成的 <em>Spring 系列</em> 的第一篇文章中，我介绍了 Spring 框架的基础。我从讨论组成 Spring 分层架构的 7 个模块开始，然后深入介绍了其中两个模块：Spring AOP 和 IOC 容器。</p>
            <p>由于学习的最佳方法是实践，所以我用一个工作示例介绍了 IOC 模式 （像 Spring 的 IOC 容器实现的那样）如何用松散耦合的方式将分散的系统集成在一起。在这个示例中可以看到，将依赖关系或服务注入工作中的信用卡帐户应用程序，要比从头开始构建它们容易得多。</p>
            <p>请继续关注这一系列的下一篇文章，我将在这里学习的知识基础上，介绍 Spring AOP 模块如何在企业应用程序中提供持久支持，并让您开始了解 Spring MVC 模块和相关插件。</p>
            </td>
        </tr>
    </tbody>
</table> <img src ="http://www.blogjava.net/jxhkwhy/aggbug/309974.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jxhkwhy/" target="_blank">纸飞机</a> 2010-01-18 17:06 <a href="http://www.blogjava.net/jxhkwhy/articles/309974.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Spring的Hibernate事务管理机制</title><link>http://www.blogjava.net/jxhkwhy/articles/309973.html</link><dc:creator>纸飞机</dc:creator><author>纸飞机</author><pubDate>Mon, 18 Jan 2010 09:04:00 GMT</pubDate><guid>http://www.blogjava.net/jxhkwhy/articles/309973.html</guid><wfw:comment>http://www.blogjava.net/jxhkwhy/comments/309973.html</wfw:comment><comments>http://www.blogjava.net/jxhkwhy/articles/309973.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jxhkwhy/comments/commentRss/309973.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jxhkwhy/services/trackbacks/309973.html</trackback:ping><description><![CDATA[<p>转自：http://developer.51cto.com/art/200909/154124.htm<br />
</p>
<ul class="summary">Spring声明式事务让我们从复杂的Hibernate事务处理中得到解脱。使得我们再也无需要去处理获得连接、关闭连接、Hibernate事务提交和回滚等这些操作。 </ul>
    <ul class="con">
        <p>
        <p>本文主要讲了Spring 声明式事务让我们从复杂的Hibernate事务处理中得到解脱。</p>
        <p>使得我们再也无需要去处理获得连接、关闭连接、Hibernate事务提交和回滚等这些操作。再也无&lt; font color="#ff0000"&gt;需要我们在与事务相关的方法中处理大量的try&#8230;catch&#8230;finally代码&lt; /font&gt;。</p>
        <p>我们在使用Spring 声明式事务时，有一个非常重要的概念就是事务属性。事务属性通常由事务的传播行为，事务的隔离级别，事务的超时值和事务只读标志组成。我们在进行事务划分时，需要进行Hibernate事务定义，也就是配置Hibernate事务的属性。</p>
        <p>Spring Hibernate在&gt;TransactionDefinition 接口中定义这些属性,以供PlatfromTransactionManager使用, PlatfromTransactionManager是spring Hibernate事务管理的核心接口。 <br />
        代码 </p>
        <pre>
        <ol class="dp-xml">
            <li class="alt"><span><span>TransactionDefinition&nbsp;publicinterfaceTransactionDefinition&nbsp;{&nbsp; &nbsp;</span></span>
            <li><span>intgetPropagationBehavior();&nbsp; &nbsp;</span>
            <li class="alt"><span>intgetIsolationLevel();&nbsp; &nbsp;</span>
            <li><span>intgetTimeout();&nbsp; &nbsp;</span>
            <li class="alt"><span>booleanisReadOnly();&nbsp; &nbsp;</span>
            <li><span>}&nbsp;&nbsp;</span></li>
        </ol>
        </pre>
        <p>getTimeout()方法，它返回事务必须在多少秒内完成。 <br />
        isReadOnly(),事务是否只读，事务管理器能够根据这个返回值进行优化，确保事务是只读的。 <br />
        getIsolationLevel()方法返回事务的隔离级别，事务管理器根据它来控制另外一个事务可以看到本事务内的哪些数据。 </p>
        <p><strong>在TransactionDefinition接口中定义了五个不同的事务隔离级别:</strong></p>
        <p>1) ISOLATION_DEFAULT 这是一个 PlatfromTransactionManager默认的隔离级别，使用数据库默认的事务隔离级别.另外四个与JDBC的隔离级别相对应 </p>
        <p>2) ISOLATION_READ_UNCOMMITTED这是事务最低的隔离级别，它充许别外一个事务可以看到这个事务未提交的数据。这种隔离级别会产生脏读，不可重复读和幻像读。</p>
        <p>3) ISOLATION_READ_COMMITTED 保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据。这种事务隔离级别可以避免脏读出现，但是可能会出现不可重复读和幻像读。</p>
        <p>4) ISOLATION_REPEATABLE_READ 这种事务隔离级别可以防止脏读，不可重复读。但是可能出现幻像读。它除了保证一个事务不能读取另一个事务未提交的数据外，还保证了避免下面的情况产生(不可重复读)</p>
        <p><strong>在TransactionDefinition接口中定义了七个事务传播行为:</strong></p>
        <p>1) &lt; span style="color: #000000;"&gt; PROPAGATION_REQUIRED 如果存在一个事务，则支持当前事务。如果没有事务则开启一个新的事务; </p>
        <p>2) PROPAGATION_SUPPORTS 如果存在一个事务，支持当前事务。如果没有事务，则非事务的执行;</p>
        <p>3) PROPAGATION_MANDATORY 如果已经存在一个事务，支持当前事务。如果没有一个活动的事务，则抛出异;</p>
        <p>4)PROPAGATION_REQUIRES_NEW 总是开启一个新的事务。如果一个事务已经存在，则将这个存在的事务挂起;</p>
        <p>5) PROPAGATION_NOT_SUPPORTED总是非事务地执行，并挂起任何存在的事务;</p>
        <p>6) PROPAGATION_NEVER总是非事务地执行，如果存在一个活动事务，则抛出异常;</p>
        <p>7) PROPAGATION_NESTED如果一个活动的事务存在，则运行在一个嵌套的事务中. 如果没有活动事务, 则按 TransactionDefinition.PROPAGATION_REQUIRED 属性执行;</p>
    </ul>
<img src ="http://www.blogjava.net/jxhkwhy/aggbug/309973.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jxhkwhy/" target="_blank">纸飞机</a> 2010-01-18 17:04 <a href="http://www.blogjava.net/jxhkwhy/articles/309973.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>spring事务管理</title><link>http://www.blogjava.net/jxhkwhy/articles/309971.html</link><dc:creator>纸飞机</dc:creator><author>纸飞机</author><pubDate>Mon, 18 Jan 2010 08:57:00 GMT</pubDate><guid>http://www.blogjava.net/jxhkwhy/articles/309971.html</guid><wfw:comment>http://www.blogjava.net/jxhkwhy/comments/309971.html</wfw:comment><comments>http://www.blogjava.net/jxhkwhy/articles/309971.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jxhkwhy/comments/commentRss/309971.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jxhkwhy/services/trackbacks/309971.html</trackback:ping><description><![CDATA[转自：http://blog.csdn.net/sunyadongwanghbjm/archive/2007/08/29/1763819.aspx<br />
<br />
<strong><span style="color: black; font-size: 14pt">spring</span></strong><strong><span style="color: black; font-size: 14pt">事务管理</span></strong>
<div><span style="color: black; font-size: 10pt">&nbsp;&nbsp; </span><span style="color: black; font-size: 10pt">在学习</span><span style="color: black; font-size: 10pt">spring</span><span style="color: black; font-size: 10pt">事务管理时</span><span style="color: black; font-size: 10pt">,</span><span style="color: black; font-size: 10pt">我忍不住要问</span><span style="color: black; font-size: 10pt">,spring</span><span style="color: black; font-size: 10pt">为什么进行事务管理</span><span style="color: black; font-size: 10pt">,spring</span><span style="color: black; font-size: 10pt">怎么进行的事务管理</span><span style="color: black; font-size: 10pt">?</span></div>
<div><span style="color: black; font-size: 10pt">&nbsp;&nbsp;&nbsp; </span><span style="color: black; font-size: 10pt">首先</span><span style="color: black; font-size: 10pt">,</span><span style="color: black; font-size: 10pt">为什么要进行事务</span><span style="color: black; font-size: 10pt">,</span><span style="color: black; font-size: 10pt">请参考</span><span style="color: black; font-size: 10pt"><a href="http://blog.csdn.net/sunyadongwanghbjm/archive/2007/08/20/1751518.aspx">http://blog.csdn.net/sunyadongwanghbjm/archive/2007/08/20/1751518.aspx</a></span></div>
<div style="text-indent: 21pt"><span style="color: black; font-size: 10pt">接下来说说</span><span style="color: black; font-size: 10pt">spring</span><span style="color: black; font-size: 10pt">是怎样进行事务管理的</span><span style="color: black; font-size: 10pt">.</span></div>
<div style="text-indent: 21pt"><strong><span style="color: black; font-size: 12pt">①</span></strong><strong><span style="color: black; font-size: 12pt">&nbsp;Spring</span></strong><strong><span style="color: black; font-size: 12pt">事务策略</span></strong></div>
<div style="text-indent: -20pt; margin-left: 41pt"><span style="color: black; font-size: 10pt">&nbsp;&nbsp;&nbsp; Spring</span><span style="color: black; font-size: 10pt">事务策略</span><span style="color: black; font-size: 10pt">,</span><span style="color: black; font-size: 10pt">也就是</span><span style="color: black; font-size: 10pt">spring</span><span style="color: black; font-size: 10pt">事务管理的实现方式</span><span style="color: black; font-size: 10pt">.</span><span style="color: black; font-size: 10pt">它有一个统一的抽象是由实现下面这个接口完成的</span><span style="color: black; font-size: 10pt">.</span></div>
<div style="text-indent: -20pt; margin-left: 41pt"><span style="color: black; font-size: 10pt">&nbsp;&nbsp;&nbsp; org.springframework.transaction.PlatformTransactionManager</span></div>
<div style="text-indent: -20pt; margin-left: 41pt"><span style="color: black; font-size: 10pt">&nbsp;&nbsp;&nbsp; </span><span style="color: black; font-size: 10pt">此接口的内容如下</span><span style="color: black; font-size: 10pt">:</span></div>
<div style="text-indent: -20pt; margin-left: 41pt"><span style="color: black; font-size: 10pt">&nbsp;&nbsp;
<div style="border-bottom: windowtext 0.5pt solid; border-left: windowtext 0.5pt solid; padding-bottom: 4px; padding-left: 5.4pt; width: 95%; padding-right: 5.4pt; background: #e6e6e6; word-break: break-all; border-top: windowtext 0.5pt solid; border-right: windowtext 0.5pt solid; padding-top: 4px">
<div><img id="_45_312_Open_Image" onclick="this.style.display='none'; document.getElementById('_45_312_Open_Text').style.display='none'; document.getElementById('_45_312_Closed_Image').style.display='inline'; document.getElementById('_45_312_Closed_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" /><img style="display: none" id="_45_312_Closed_Image" onclick="this.style.display='none'; document.getElementById('_45_312_Closed_Text').style.display='none'; document.getElementById('_45_312_Open_Image').style.display='inline'; document.getElementById('_45_312_Open_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" /><span style="color: #000000">Public&nbsp;</span><span style="color: #0000ff">interface</span><span style="color: #000000">&nbsp;PlatformTransactionManager</span><span id="_45_312_Open_Text"><span style="color: #000000">{<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransactionStatue&nbsp;getTransaction(TransactionDefinition&nbsp;definition)&nbsp;</span><span style="color: #0000ff">throws</span><span style="color: #000000">&nbsp;TransactionException;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Void&nbsp;commit(TransactionStatus&nbsp;status)&nbsp;</span><span style="color: #0000ff">throws</span><span style="color: #000000">&nbsp;TransactionException;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Void&nbsp;rollback(TransactionStatus&nbsp;status)&nbsp;</span><span style="color: #0000ff">throws</span><span style="color: #000000">&nbsp;TransactionException;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" /></span></div>
</div>
</span></div>
<div style="text-indent: -20pt; margin-left: 41pt"><span style="color: black; font-size: 10pt">&nbsp;&nbsp;&nbsp;</span></div>
<div style="text-indent: -20pt; margin-left: 41pt"><span style="color: black; font-size: 10pt">&nbsp;</span><span style="color: black; font-size: 10pt">不管是声明式的还是编程式的事务管理都需要此抽象来完成</span><span style="color: black; font-size: 10pt">.</span></div>
<div style="margin-left: 41.05pt"><span style="color: black; font-size: 10pt">解释一下这个接口</span><span style="color: black; font-size: 10pt">,</span><span style="color: black; font-size: 10pt">这样可以更好的理解</span><span style="color: black; font-size: 10pt">spring</span><span style="color: black; font-size: 10pt">的事务控制的原理</span><span style="color: black; font-size: 10pt">.</span></div>
<div style="margin-left: 41.05pt"><span style="color: black; font-size: 10pt">getTransaction()</span><span style="color: black; font-size: 10pt">根据类型为</span><span style="color: black; font-size: 10pt">TransactionDefinition</span><span style="color: black; font-size: 10pt">的参数返回一个</span><span style="color: black; font-size: 10pt">TransactionStatus</span><span style="color: black; font-size: 10pt">对象</span><span style="color: black; font-size: 10pt">.</span><span style="color: black; font-size: 10pt">返回的</span><span style="color: black; font-size: 10pt">TransactionStatus</span><span style="color: black; font-size: 10pt">对象可能代表一个新的或已经存在的事务</span><span style="color: black; font-size: 10pt">(</span><span style="color: black; font-size: 10pt">如果在当前调用堆栈有一个符合条件的事务</span><span style="color: black; font-size: 10pt">).</span><span style="color: black; font-size: 10pt">如同</span><span style="color: black; font-size: 10pt">J2EE</span><span style="color: black; font-size: 10pt">事务上下文</span><span style="color: black; font-size: 10pt">,</span><span style="color: black; font-size: 10pt">一个</span><span style="color: black; font-size: 10pt">TransactionStatus</span><span style="color: black; font-size: 10pt">也是和执行的线程关联的</span><span style="color: black; font-size: 10pt">.</span></div>
<div style="margin-left: 41.05pt"><span style="color: black; font-size: 10pt">同时</span><span style="color: black; font-size: 10pt">,</span><span style="color: black; font-size: 10pt">在框架中还存在</span><span style="color: black; font-size: 10pt">TransactionDefinition</span><span style="color: black; font-size: 10pt">接口</span><span style="color: black; font-size: 10pt">,</span><span style="color: black; font-size: 10pt">即上边的参数类型</span><span style="color: black; font-size: 10pt">.</span><span style="color: black; font-size: 10pt">此接口指定了事务隔离程度、事务传播、事务超时、只读状态。</span></div>
<div style="margin-left: 41.05pt"><span style="color: black; font-size: 10pt">另外，还有</span><span style="color: black; font-size: 10pt">TransactionStatus</span><span style="color: black; font-size: 10pt">接口。这个接口为处理事务提供简单的控制事务执行和查询事务状态的方法。</span></div>
<div style="text-indent: 10pt"><span style="color: black; font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp; </span></div>
<div style="text-indent: 21pt"><strong><span style="color: black; font-size: 12pt">②</span></strong><strong><span style="color: black; font-size: 12pt">&nbsp;&nbsp;&nbsp; </span></strong><strong><span style="color: black; font-size: 12pt">两种事务管理方式：编程式、声明式。</span></strong></div>
<div style="text-indent: 0.2pt; margin-left: 42pt"><strong><span style="color: black; font-size: 10pt">Spring</span></strong><strong><span style="color: black; font-size: 10pt">提供两种方式的编程式事务管理</span></strong><span style="color: black; font-size: 10pt">，分别是：使用</span><span style="color: black; font-size: 10pt">TransactionTemplate</span><span style="color: black; font-size: 10pt">和直接使用</span><span style="color: black; font-size: 10pt">PlatformTransactionManager</span><span style="color: black; font-size: 10pt">。</span></div>
<div style="margin-left: 42pt"><span style="color: black; font-size: 10pt">ⅰ. TransactionTempale采用和其他Spring模板，如JdbcTempalte和HibernateTemplate一样的方法。它使用回调方法，把应用程序从处理取得和释放资源中解脱出来。如同其他模板，TransactionTemplate是线程安全的。</span></div>
<div style="margin-left: 42pt"><span style="color: black; font-size: 10pt">代码片段：</span></div>
<div style="margin-left: 42pt"><span style="color: black; font-size: 10pt">
<div style="border-bottom: windowtext 0.5pt solid; border-left: windowtext 0.5pt solid; padding-bottom: 4px; padding-left: 5.4pt; width: 95%; padding-right: 5.4pt; background: #e6e6e6; word-break: break-all; border-top: windowtext 0.5pt solid; border-right: windowtext 0.5pt solid; padding-top: 4px">
<div><img id="_52_186_Open_Image" onclick="this.style.display='none'; document.getElementById('_52_186_Open_Text').style.display='none'; document.getElementById('_52_186_Closed_Image').style.display='inline'; document.getElementById('_52_186_Closed_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" /><img style="display: none" id="_52_186_Closed_Image" onclick="this.style.display='none'; document.getElementById('_52_186_Closed_Text').style.display='none'; document.getElementById('_52_186_Open_Image').style.display='inline'; document.getElementById('_52_186_Open_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" /><span style="color: #000000">Object&nbsp;result&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;tt.execute(</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;TransactionCallback()</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="_52_186_Closed_Text">...</span><span id="_52_186_Open_Text"><span style="color: #000000">{<br />
<img id="_110_184_Open_Image" onclick="this.style.display='none'; document.getElementById('_110_184_Open_Text').style.display='none'; document.getElementById('_110_184_Closed_Image').style.display='inline'; document.getElementById('_110_184_Closed_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="_110_184_Closed_Image" onclick="this.style.display='none'; document.getElementById('_110_184_Closed_Text').style.display='none'; document.getElementById('_110_184_Open_Image').style.display='inline'; document.getElementById('_110_184_Open_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;Object&nbsp;doTransaction(TransactionStatus&nbsp;status)</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="_110_184_Closed_Text">...</span><span id="_110_184_Open_Text"><span style="color: #000000">{<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;updateOperation();<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;resultOfUpdateOperation();<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000">);<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" /></span></div>
</div>
</span></div>
<div style="text-indent: 10pt"><span style="color: black; font-size: 10pt">使用TransactionCallback()</span><span style="color: black; font-size: 10pt">可以返回一个值。</span></div>
<div style="text-indent: 10pt"><span style="color: black; font-size: 10pt">如果使用</span><span style="color: black; font-size: 10pt">TransactionCallbackWithoutResult</span><span style="color: black; font-size: 10pt">则没有返回值。</span></div>
<div style="text-indent: 10pt"><span style="color: black; font-size: 10pt">ⅱ</span><span style="color: black; font-size: 10pt">. </span><span style="color: black; font-size: 10pt">也可以使用</span><span style="color: black; font-size: 10pt">PlatformTransactionManager</span><span style="color: black; font-size: 10pt">直接管理事务。简单地通过一个</span><span style="color: black; font-size: 10pt">bean</span><span style="color: black; font-size: 10pt">引用给你的</span><span style="color: black; font-size: 10pt">bean</span><span style="color: black; font-size: 10pt">传递一个你使用的</span><span style="color: black; font-size: 10pt">PlatformTransaction</span><span style="color: black; font-size: 10pt">对象。然后，使用</span><span style="color: black; font-size: 10pt">TransactionDefinition</span><span style="color: black; font-size: 10pt">和</span><span style="color: black; font-size: 10pt">TransactionStatus</span><span style="color: black; font-size: 10pt">对象就可以发起、回滚、提交事务。</span></div>
<div style="text-indent: 10pt"><span style="color: black; font-size: 10pt">如下片段：</span></div>
<div>
<div style="border-bottom: windowtext 0.5pt solid; border-left: windowtext 0.5pt solid; padding-bottom: 4px; padding-left: 5.4pt; width: 95%; padding-right: 5.4pt; background: #e6e6e6; word-break: break-all; border-top: windowtext 0.5pt solid; border-right: windowtext 0.5pt solid; padding-top: 4px">
<div><img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" /><span style="color: #000000">DefaultTransactionDefinition&nbsp;&nbsp;&nbsp;def</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;DefaultTransactionDefinition();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">new&nbsp;一个事务</span><span style="color: #008000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" /></span><span style="color: #000000">def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">初始化事务,参数定义事务的传播类型;</span><span style="color: #008000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" /></span><span style="color: #000000">TransactionStatus&nbsp;&nbsp;status&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;transactionManager.getTransaction(def);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">获得事务状态</span><span style="color: #008000"><br />
<img id="_293_352_Open_Image" onclick="this.style.display='none'; document.getElementById('_293_352_Open_Text').style.display='none'; document.getElementById('_293_352_Closed_Image').style.display='inline'; document.getElementById('_293_352_Closed_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" /><img style="display: none" id="_293_352_Closed_Image" onclick="this.style.display='none'; document.getElementById('_293_352_Closed_Text').style.display='none'; document.getElementById('_293_352_Open_Image').style.display='inline'; document.getElementById('_293_352_Open_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" /></span><span style="color: #0000ff">try</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="_293_352_Closed_Text">...</span><span id="_293_352_Open_Text"><span style="color: #000000">{<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&#8230;&#8230;&#8230;&#8230;&#8230;..<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;transactionManager.commit(status);&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">提交事务;</span><span style="color: #008000"><br />
<img id="_363_413_Open_Image" onclick="this.style.display='none'; document.getElementById('_363_413_Open_Text').style.display='none'; document.getElementById('_363_413_Closed_Image').style.display='inline'; document.getElementById('_363_413_Closed_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" /><img style="display: none" id="_363_413_Closed_Image" onclick="this.style.display='none'; document.getElementById('_363_413_Closed_Text').style.display='none'; document.getElementById('_363_413_Open_Image').style.display='inline'; document.getElementById('_363_413_Open_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" /></span><span style="color: #000000">}</span></span><span style="color: #0000ff">catch</span><span style="color: #000000">(&#8230;..)</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="_363_413_Closed_Text">...</span><span id="_363_413_Open_Text"><span style="color: #000000">{<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;transactionManager.rollback(status);&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">回滚事务;</span><span style="color: #008000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" /></span><span style="color: #000000">}</span></span><span style="color: #000000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" /></span></div>
</div>
&nbsp;</div>
<div><strong><span style="color: black">Spring</span></strong><strong><span style="color: black">也提供声明式事务管理。这是通过</span></strong><strong><span style="color: black">AOP</span></strong><strong><span style="color: black">实现的。</span></strong></div>
<div><span style="color: black; font-size: 10pt">大多数</span><span style="color: black; font-size: 10pt">Spring</span><span style="color: black; font-size: 10pt">用户选择声明式事务管理，这是最少影响应用代码的选择，因而这是和非侵入性的轻量级容器的观念是一致的。</span></div>
<div><span style="color: black; font-size: 10pt">①</span><span style="color: black; font-size: 10pt">&nbsp;</span><span style="color: black; font-size: 10pt">通常通过</span><span style="color: black; font-size: 10pt">TransactionProxyFactoryBean</span><span style="color: black; font-size: 10pt">设置</span><span style="color: black; font-size: 10pt">Spring</span><span style="color: black; font-size: 10pt">事务代理。需要一个目标对象包装在事务代理中。这个目标对象一般是一个普通</span><span style="color: black; font-size: 10pt">Javabean</span><span style="color: black; font-size: 10pt">。当我们定义</span><span style="color: black; font-size: 10pt">TransactionProxyFactoryBean</span><span style="color: black; font-size: 10pt">时，必须提供一个相关的</span><span style="color: black; font-size: 10pt">PlatformTransactionManager</span><span style="color: black; font-size: 10pt">的引用和事务属性。事务属性含有事务定义。例如</span><span style="color: black; font-size: 10pt">:</span></div>
<div style="text-indent: 21pt"><span style="color: black; font-size: 10pt">
<div style="border-bottom: windowtext 0.5pt solid; border-left: windowtext 0.5pt solid; padding-bottom: 4px; padding-left: 5.4pt; width: 95%; padding-right: 5.4pt; background: #e6e6e6; word-break: break-all; border-top: windowtext 0.5pt solid; border-right: windowtext 0.5pt solid; padding-top: 4px">
<div><img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" /><span style="color: #0000ff">&lt;</span><span style="color: #800000">bean&nbsp;</span><span style="color: #ff0000">id</span><span style="color: #0000ff">="transactionService"</span><span style="color: #ff0000">class</span><span style="color: #0000ff">="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">property&nbsp;</span><span style="color: #ff0000">name</span><span style="color: #0000ff">="transactionManager"</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">ref&nbsp;&nbsp;&nbsp;</span><span style="color: #ff0000">local</span><span style="color: #0000ff">="transactionManager"</span><span style="color: #0000ff">/&gt;</span><span style="color: #000000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">property</span><span style="color: #0000ff">&gt;</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">property&nbsp;&nbsp;</span><span style="color: #ff0000">name</span><span style="color: #0000ff">="target"</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">ref&nbsp;&nbsp;</span><span style="color: #ff0000">local</span><span style="color: #0000ff">="transactionServiceControl"</span><span style="color: #0000ff">/&gt;</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">property</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">property&nbsp;&nbsp;</span><span style="color: #ff0000">name</span><span style="color: #0000ff">="transactionAttributes"</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">props</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">prop&nbsp;</span><span style="color: #ff0000">key</span><span style="color: #0000ff">=&#8221;insert*&#8221;&gt;PROPAGATION_REQUIRED,-MyCheckedException&lt;/prop&gt;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" /></span><span style="color: #ff0000">&lt;prop&nbsp;key</span><span style="color: #0000ff">=&#8221;update*&#8221;&gt;PROPAGATION_REQUIRED&lt;/prop&gt;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" /></span><span style="color: #ff0000">&lt;prop&nbsp;key</span><span style="color: #0000ff">=&#8221;*&#8221;&gt;PROPAGATION_REQUIRED,readOnly&lt;/prop&gt;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff0000">&lt;/props</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">property</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">bean</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" /></span></div>
</div>
</span></div>
<div style="text-indent: 21pt"><span style="color: black; font-size: 10pt">事务代理会实现目标对象的接口:这里是属性名是target的引用。id是transactionServiceControl。（●使用ＣＧＬＩＢ也可以实现具体类的代理。只要设置proxyTargetClass属性为true即可。如果目标对象没有实现任何接口，这将自动设置该属性为true。通常，我们希望面向接口编程。） ●使用proxyInterfaces属性来限定事务代理来代理指定接口也是可以。 ●也可以通过从org.springframework.aop.framework.ProxyConfig继承或所有AOP代理工厂共享的属性来定制TransactionProxyFactoryBean行为。</span></div>
<div style="text-indent: 21pt"><span style="color: black; font-size: 10pt">然后，说说属性名是</span><span style="background: white; color: green; font-size: 10pt">transactionAttributes</span><span style="color: black; font-size: 10pt">意义</span><span style="color: green; font-size: 10pt">：</span></div>
<div style="text-indent: 21pt"><span style="color: black; font-size: 10pt">这里的transactionAttributes属性是定义在org.springframework.transaction.interceptor.NameMathTransactionAttributeSource中的属性格式设置。这个包括通配符的方法名称映射是很直观的，如&#8221;insert*&#8221;。注意insert*的映射的值包括回滚规则。&#8221;-MyCheckException&#8221;指定如果方法抛出MyCheckException或它的子类，事务会自动回滚。可以用逗号分隔多个回滚规则。&#8220;－&#8221;前缀强制回滚，&#8220;＋&#8221;前缀指定提交（这允许即使抛出unchecked异常时也可以提交事务）。&#8220;</span><span style="color: navy; font-size: 10pt">PROPAGATION_REQUIRED</span><span style="color: black; font-size: 10pt">&#8221;指定事务传播范围。</span></div>
<div style="text-indent: 21pt"><span style="color: black; font-size: 10pt">TransactionProxyFactoryBean</span><span style="color: black; font-size: 10pt">允许你通过&#8220;preInterceptors&#8221;和&#8220;postInterceptors&#8221;属性设置前或后的拦截操作。可以设置任意数量的前和后通过，它们的类型可以是Ａdvistor（切入点），MethodInterceptor或被当前Spring配置支持的通知类型。例如：ＴhrowAdvice，AfterReturningAdvice或ＢeforeAdvice。这些通知必须支持实例共享模式。如果你需要高级AＯＰ特性操作事务，通过org.springframework.aop.framework.ProxyFactoryBean，而不是TransactionProxyFactory实用代理创建者。</span></div>
<div><span style="color: black; font-size: 10pt">②<span>&nbsp;&nbsp; </span>另一种声明方式：BeanNameAutoProxyCreator</span></div>
<div style="text-indent: 21pt"><span style="color: black; font-size: 10pt">使用TransactionProxyFactoryBean当事务代理包装对象，你可以完全控制代理。如果需要用一致方式包装大量bean。使用一个BeanFactoryPostProcessor的一个实现，BeanNameAutoProxyCreator，可以提供另外一种方法。(Spring中，一旦ApplicationContext读完它的初始化信息，它将初始化所有实现BeanPostProcessor接口的bean，并且让它们后处理ApplicationContext中所有其他的bean。所以使用这种机制，正确配置的BeanNameAutoProxyCreator可以用来后处理所有ApplicationContext中所有其他的bean),并且把它们用事务代理包装起来。真正生成的事务代理和使用TransactionProxyFactoryBean生成的基本一致。</span></div>
<div style="text-indent: 21pt"><span style="color: black; font-size: 10pt">最后，总结一下Spring的优点：</span></div>
<div style="text-indent: -30.75pt; margin-left: 89.25pt"><span style="color: black; font-size: 10pt">★<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="color: black; font-size: 10pt">为不同的事务ＡＰＩ提供一致的编程模型，如JTA、JDBC、Hibernate、iBATIS数据库层ＪＤＯ</span></div>
<div style="text-indent: -30.75pt; margin-left: 89.25pt"><span style="color: black; font-size: 10pt">★<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="color: black; font-size: 10pt">提供比大多数事务API更简单的、易于使用的编程式事务管理API</span></div>
<div style="text-indent: -30.75pt; margin-left: 89.25pt"><span style="color: black; font-size: 10pt">★<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="color: black; font-size: 10pt">整合Spring数据访问抽象</span></div>
<div style="text-indent: -30.75pt; margin-left: 89.25pt"><span style="color: black; font-size: 10pt">★<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="color: black; font-size: 10pt">支持Spring声明式事务管理</span></div>
<img src ="http://www.blogjava.net/jxhkwhy/aggbug/309971.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jxhkwhy/" target="_blank">纸飞机</a> 2010-01-18 16:57 <a href="http://www.blogjava.net/jxhkwhy/articles/309971.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java事务管理</title><link>http://www.blogjava.net/jxhkwhy/articles/309970.html</link><dc:creator>纸飞机</dc:creator><author>纸飞机</author><pubDate>Mon, 18 Jan 2010 08:55:00 GMT</pubDate><guid>http://www.blogjava.net/jxhkwhy/articles/309970.html</guid><wfw:comment>http://www.blogjava.net/jxhkwhy/comments/309970.html</wfw:comment><comments>http://www.blogjava.net/jxhkwhy/articles/309970.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jxhkwhy/comments/commentRss/309970.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jxhkwhy/services/trackbacks/309970.html</trackback:ping><description><![CDATA[<a href="http://blog.csdn.net/sunyadongwanghbjm/archive/2007/08/20/1751518.aspx">转自：http://blog.csdn.net/sunyadongwanghbjm/archive/2007/08/20/1751518.aspx</a><br />
<div><strong><font size="4">什么是事务:</font></strong></div>
<div>首先,说说什么事务。我认为事务，就是一组操作数据库的动作集合。</div>
<div>事务是现代数据库理论中的核心概念之一。如果一组处理步骤或者全部发生或者一步也不执行，我们称该组处理步骤为一个事务。当所有的步骤像一个操作一样被完整地执行，我们称该事务被提交。由于其中的一部分或多步执行失败，导致没有步骤被提交，则事务必须回滚到最初的系统状态。</div>
<div>事务必须服从ISO/IEC所制定的ACID原则。ACID是原子性（atomicity）、一致性（consistency）、隔离性（isolation）和持久性（durability）的缩写。事务的原子性表示事务执行过程中的任何失败都将导致事务所做的任何修改失效。一致性表示当事务执行失败时，所有被该事务影响的数据都应该恢复到事务执行前的状态。隔离性表示在事务执行过程中对数据的修改，在事务提交之前对其他事务不可见。持久性表示当系统或介质发生故障时，确保已提交事务的更新不能丢失。持久性通过数据库备份和恢复来保证。</div>
<br />
<div><strong><font size="4">Java中的事务处理</font></strong></div>
<div>一般J2EE服务器支持三种类型的事务管理。即：JDBC事务，JTA事务，容器管理事务。</div>
<div>最好不要在程序中同时使用上述三种事务类型；并且，事务要在尽可能短的时间内完成，不要在不同方法中实现事务的使用。下面举两个例子说明JDBC及JTA事务，容器管理事务是在特定的框架中实现的（如：Spring的事务管理）</div>
<div>1. JDBC事务</div>
<div>
<div style="border-bottom: windowtext 0.5pt solid; border-left: windowtext 0.5pt solid; padding-bottom: 4px; padding-left: 5.4pt; width: 95%; padding-right: 5.4pt; background: rgb(230,230,230) 0% 50%; border-top: windowtext 0.5pt solid; border-right: windowtext 0.5pt solid; padding-top: 4px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<div><img id="_32_556_Open_Image" onclick="this.style.display='none'; document.getElementById('_32_556_Open_Text').style.display='none'; document.getElementById('_32_556_Closed_Image').style.display='inline'; document.getElementById('_32_556_Closed_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" /><img style="display: none" id="_32_556_Closed_Image" onclick="this.style.display='none'; document.getElementById('_32_556_Closed_Text').style.display='none'; document.getElementById('_32_556_Open_Image').style.display='inline'; document.getElementById('_32_556_Open_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" /><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)">&nbsp;String&nbsp;delete(String&nbsp;id)&nbsp;</span><span style="border-bottom: rgb(128,128,128) 1px solid; border-left: rgb(128,128,128) 1px solid; background-color: rgb(255,255,255); display: none; border-top: rgb(128,128,128) 1px solid; border-right: rgb(128,128,128) 1px solid" id="_32_556_Closed_Text">...</span><span id="_32_556_Open_Text"><span style="color: rgb(0,0,0)">{<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />String&nbsp;ID&nbsp;</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">&nbsp;id;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　db&nbsp;</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">&nbsp;</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)">&nbsp;getConnection();<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　Connection&nbsp;con&nbsp;</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">&nbsp;db.getConnection();<br />
<img id="_120_449_Open_Image" onclick="this.style.display='none'; document.getElementById('_120_449_Open_Text').style.display='none'; document.getElementById('_120_449_Closed_Image').style.display='inline'; document.getElementById('_120_449_Closed_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="_120_449_Closed_Image" onclick="this.style.display='none'; document.getElementById('_120_449_Closed_Text').style.display='none'; document.getElementById('_120_449_Open_Image').style.display='inline'; document.getElementById('_120_449_Open_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" />　</span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)">&nbsp;</span><span style="border-bottom: rgb(128,128,128) 1px solid; border-left: rgb(128,128,128) 1px solid; background-color: rgb(255,255,255); display: none; border-top: rgb(128,128,128) 1px solid; border-right: rgb(128,128,128) 1px solid" id="_120_449_Closed_Text">...</span><span id="_120_449_Open_Text"><span style="color: rgb(0,0,0)">{<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />con.setAutoCommit(</span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">);<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />db.executeUpdate(</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">delete&nbsp;from&nbsp;helloworld&nbsp;where&nbsp;ID=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&nbsp;</span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)">&nbsp;ID);&nbsp;</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">更新操作1</span><span style="color: rgb(0,128,0)"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" /></span><span style="color: rgb(0,0,0)">　　db.executeUpdate(</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">delete&nbsp;from&nbsp;helloworld&nbsp;_book&nbsp;where&nbsp;ID=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&nbsp;</span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)">&nbsp;ID);&nbsp;</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">更新操作2</span><span style="color: rgb(0,128,0)"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" /></span><span style="color: rgb(0,0,0)">　　db.executeUpdate(</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">delete&nbsp;from&nbsp;helloworld_user&nbsp;where&nbsp;ID=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&nbsp;</span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)">&nbsp;ID);&nbsp;</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">更新操作3</span><span style="color: rgb(0,128,0)"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" /></span><span style="color: rgb(0,0,0)">　　con.commit();</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">提交JDBC事务</span><span style="color: rgb(0,128,0)"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" /></span><span style="color: rgb(0,0,0)">　　con.setAutoCommit(</span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">);&nbsp;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />db.close();<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" /></span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)">&nbsp;&#8220;success&#8221;;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" />　}</span></span><span style="color: rgb(0,0,0)"><br />
<img id="_472_554_Open_Image" onclick="this.style.display='none'; document.getElementById('_472_554_Open_Text').style.display='none'; document.getElementById('_472_554_Closed_Image').style.display='inline'; document.getElementById('_472_554_Closed_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="_472_554_Closed_Image" onclick="this.style.display='none'; document.getElementById('_472_554_Closed_Text').style.display='none'; document.getElementById('_472_554_Open_Image').style.display='inline'; document.getElementById('_472_554_Open_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" />　</span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)">&nbsp;(Exception&nbsp;e)&nbsp;</span><span style="border-bottom: rgb(128,128,128) 1px solid; border-left: rgb(128,128,128) 1px solid; background-color: rgb(255,255,255); display: none; border-top: rgb(128,128,128) 1px solid; border-right: rgb(128,128,128) 1px solid" id="_472_554_Closed_Text">...</span><span id="_472_554_Open_Text"><span style="color: rgb(0,0,0)">{<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　　con.rollBack();</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">回滚JDBC事务</span><span style="color: rgb(0,128,0)"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" /></span><span style="color: rgb(0,0,0)">　　e.printStackTrace();<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />db.close();<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" /></span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)">&nbsp;&#8220;fail&#8221;;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" />　　}</span></span><span style="color: rgb(0,0,0)"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: rgb(0,0,0)"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" /></span></div>
</div>
&nbsp;</div>
<div>如上例：更新操作1，2，3只有当三步操作都成功完成才进行提交，否则回滚已经进行的操作。这样，保证了数据的完整性，不会因为突然断电等特殊情况导致的数据错误。</div>
<div>&nbsp;</div>
<div>2．JTA事务</div>
<div>JTA是J2EE事务服务的解决方案、描述了J2EE模型事务接口。JTA具有三个主要的接口：UserTransaction、TransactionManager、Transaction接口。这些接口共享公共的事务操作，如：commit()、rollback()。同时各自也有自己的操作。举例说明：</div>
<div>&nbsp;</div>
<div>
<div style="border-bottom: windowtext 0.5pt solid; border-left: windowtext 0.5pt solid; padding-bottom: 4px; padding-left: 5.4pt; width: 95%; padding-right: 5.4pt; background: rgb(230,230,230) 0% 50%; border-top: windowtext 0.5pt solid; border-right: windowtext 0.5pt solid; padding-top: 4px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<div><img id="_32_660_Open_Image" onclick="this.style.display='none'; document.getElementById('_32_660_Open_Text').style.display='none'; document.getElementById('_32_660_Closed_Image').style.display='inline'; document.getElementById('_32_660_Closed_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" /><img style="display: none" id="_32_660_Closed_Image" onclick="this.style.display='none'; document.getElementById('_32_660_Closed_Text').style.display='none'; document.getElementById('_32_660_Open_Image').style.display='inline'; document.getElementById('_32_660_Open_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" /><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)">&nbsp;String&nbsp;delete(String&nbsp;id)&nbsp;</span><span style="border-bottom: rgb(128,128,128) 1px solid; border-left: rgb(128,128,128) 1px solid; background-color: rgb(255,255,255); display: none; border-top: rgb(128,128,128) 1px solid; border-right: rgb(128,128,128) 1px solid" id="_32_660_Closed_Text">...</span><span id="_32_660_Open_Text"><span style="color: rgb(0,0,0)">{<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />&nbsp;String&nbsp;ID&nbsp;</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">&nbsp;id;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　db&nbsp;</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">&nbsp;</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)">&nbsp;getConnection();<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　db.getConnection();<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　UserTransaction&nbsp;transaction&nbsp;</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">&nbsp;sessionContext.getUserTransaction();</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">获得JTA事务</span><span style="color: rgb(0,128,0)"><br />
<img id="_181_479_Open_Image" onclick="this.style.display='none'; document.getElementById('_181_479_Open_Text').style.display='none'; document.getElementById('_181_479_Closed_Image').style.display='inline'; document.getElementById('_181_479_Closed_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="_181_479_Closed_Image" onclick="this.style.display='none'; document.getElementById('_181_479_Closed_Text').style.display='none'; document.getElementById('_181_479_Open_Image').style.display='inline'; document.getElementById('_181_479_Open_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" /></span><span style="color: rgb(0,0,0)">　</span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)">&nbsp;</span><span style="border-bottom: rgb(128,128,128) 1px solid; border-left: rgb(128,128,128) 1px solid; background-color: rgb(255,255,255); display: none; border-top: rgb(128,128,128) 1px solid; border-right: rgb(128,128,128) 1px solid" id="_181_479_Closed_Text">...</span><span id="_181_479_Open_Text"><span style="color: rgb(0,0,0)">{<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　　transaction.begin();&nbsp;</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">开始JTA事务</span><span style="color: rgb(0,128,0)"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" /></span><span style="color: rgb(0,0,0)">　　db.executeUpdate(</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">delete&nbsp;from&nbsp;helloworld&nbsp;where&nbsp;ID=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&nbsp;</span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)">&nbsp;ID);<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　　db.executeUpdate(</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">delete&nbsp;from&nbsp;helloworld&nbsp;_book&nbsp;where&nbsp;ID=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&nbsp;</span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)">&nbsp;ID);<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　　db.executeUpdate(</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">delete&nbsp;from&nbsp;helloworld&nbsp;_user&nbsp;where&nbsp;ID=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&nbsp;</span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)">&nbsp;ID);<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　　transaction.commit();&nbsp;</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">提交JTA事务</span><span style="color: rgb(0,128,0)"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" /></span><span style="color: rgb(0,0,0)">　　db.close();<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　　</span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)">&#8221;success&#8221;;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" />　}</span></span><span style="color: rgb(0,0,0)"><br />
<img id="_502_658_Open_Image" onclick="this.style.display='none'; document.getElementById('_502_658_Open_Text').style.display='none'; document.getElementById('_502_658_Closed_Image').style.display='inline'; document.getElementById('_502_658_Closed_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="_502_658_Closed_Image" onclick="this.style.display='none'; document.getElementById('_502_658_Closed_Text').style.display='none'; document.getElementById('_502_658_Open_Image').style.display='inline'; document.getElementById('_502_658_Open_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" />　</span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)">&nbsp;(Exception&nbsp;e)&nbsp;</span><span style="border-bottom: rgb(128,128,128) 1px solid; border-left: rgb(128,128,128) 1px solid; background-color: rgb(255,255,255); display: none; border-top: rgb(128,128,128) 1px solid; border-right: rgb(128,128,128) 1px solid" id="_502_658_Closed_Text">...</span><span id="_502_658_Open_Text"><span style="color: rgb(0,0,0)">{<br />
<img id="_510_547_Open_Image" onclick="this.style.display='none'; document.getElementById('_510_547_Open_Text').style.display='none'; document.getElementById('_510_547_Closed_Image').style.display='inline'; document.getElementById('_510_547_Closed_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="_510_547_Closed_Image" onclick="this.style.display='none'; document.getElementById('_510_547_Closed_Text').style.display='none'; document.getElementById('_510_547_Open_Image').style.display='inline'; document.getElementById('_510_547_Open_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" />　　</span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)">&nbsp;</span><span style="border-bottom: rgb(128,128,128) 1px solid; border-left: rgb(128,128,128) 1px solid; background-color: rgb(255,255,255); display: none; border-top: rgb(128,128,128) 1px solid; border-right: rgb(128,128,128) 1px solid" id="_510_547_Closed_Text">...</span><span id="_510_547_Open_Text"><span style="color: rgb(0,0,0)">{<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　　　transaction.rollback();</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">事务回滚</span><span style="color: rgb(0,128,0)"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" /></span><span style="color: rgb(0,0,0)">　　}</span></span><span style="color: rgb(0,0,0)"><br />
<img id="_571_599_Open_Image" onclick="this.style.display='none'; document.getElementById('_571_599_Open_Text').style.display='none'; document.getElementById('_571_599_Closed_Image').style.display='inline'; document.getElementById('_571_599_Closed_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="_571_599_Closed_Image" onclick="this.style.display='none'; document.getElementById('_571_599_Closed_Text').style.display='none'; document.getElementById('_571_599_Open_Image').style.display='inline'; document.getElementById('_571_599_Open_Text').style.display='inline';" alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" />　　</span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)">&nbsp;(Exception&nbsp;e)&nbsp;</span><span style="border-bottom: rgb(128,128,128) 1px solid; border-left: rgb(128,128,128) 1px solid; background-color: rgb(255,255,255); display: none; border-top: rgb(128,128,128) 1px solid; border-right: rgb(128,128,128) 1px solid" id="_571_599_Closed_Text">...</span><span id="_571_599_Open_Text"><span style="color: rgb(0,0,0)">{<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　　　e.printStackTrace();<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" />　　}</span></span><span style="color: rgb(0,0,0)"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　　exc.printStackTrace();<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　　db.close();<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" />　　</span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)">&nbsp;&#8220;fail&#8221;;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" />　}</span></span><span style="color: rgb(0,0,0)"><br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: rgb(0,0,0)">&nbsp;<br />
<img alt="" align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" /></span></div>
</div>
3．容器事务管理</div>
<div>在Spring、Hibernate等框架中都有各自的事务管理功能。虽然表现形式有些差别，但都是在JAVA事务管理的基础上实现的。这里不进行介绍。</div><img src ="http://www.blogjava.net/jxhkwhy/aggbug/309970.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jxhkwhy/" target="_blank">纸飞机</a> 2010-01-18 16:55 <a href="http://www.blogjava.net/jxhkwhy/articles/309970.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>