﻿<?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/terry711/category/20615.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 02 Apr 2008 04:18:42 GMT</lastBuildDate><pubDate>Wed, 02 Apr 2008 04:18:42 GMT</pubDate><ttl>60</ttl><item><title>转 Spring提供的Hibernate申明式事务管理有两种办法 </title><link>http://www.blogjava.net/terry711/articles/190168.html</link><dc:creator>terryliu</dc:creator><author>terryliu</author><pubDate>Tue, 01 Apr 2008 09:49:00 GMT</pubDate><guid>http://www.blogjava.net/terry711/articles/190168.html</guid><wfw:comment>http://www.blogjava.net/terry711/comments/190168.html</wfw:comment><comments>http://www.blogjava.net/terry711/articles/190168.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/terry711/comments/commentRss/190168.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/terry711/services/trackbacks/190168.html</trackback:ping><description><![CDATA[<p>Spring提供的Hibernate申明式事务管理有两种办法 </p>
<p>a) 配合使用org.springframework.transaction.interceptor.TransactionInterceptor和org.springframework.orm.hibernate.HibernateTransactionManager，下面是spring reference的例子 <br />
</p>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
    <tbody>
        <tr>
            <td><span class="genmed"><strong>代码:</strong></span></td>
        </tr>
        <tr>
            <td class="code">&lt;beans&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; ... <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &lt;bean id="myTransactionManager" <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="org.springframework.orm.hibernate.HibernateTransactionManager"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="sessionFactory"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ref bean="mySessionFactory"/&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/property&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &lt;/bean&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &lt;bean id="myTransactionInterceptor" <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="org.springframework.transaction.interceptor.TransactionInterceptor"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="transactionManager"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ref bean="myTransactionManager"/&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/property&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="transactionAttributeSource"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;value&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; product.ProductService.increasePrice*=PROPAGATION_REQUIRED <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; product.ProductService.someOtherBusinessMethod=PROPAGATION_MANDATORY <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/value&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/property&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &lt;/bean&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &lt;bean id="myProductServiceTarget" class="product.ProductServiceImpl"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="productDao"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ref bean="myProductDao"/&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/property&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &lt;/bean&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &lt;bean id="myProductService" class="org.springframework.aop.framework.ProxyFactoryBean"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="proxyInterfaces"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;value&gt;product.ProductService&lt;/value&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/property&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="target"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ref local="myProductServiceTarget&lt;"/&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/property&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="interceptorNames"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;list&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;value&gt;myTransactionInterceptor&lt;/value&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/list&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/property&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &lt;/bean&gt; <br />
            &nbsp; &nbsp; &lt;/beans&gt;</td>
        </tr>
    </tbody>
</table>
<span class="postbody"><br />
<br />
HibernateInterceptor和事务无关，它的用途在javadocs中描述如下： <br />
</span>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
    <tbody>
        <tr>
            <td><span class="genmed"><strong>引用:</strong></span></td>
        </tr>
        <tr>
            <td class="quote">This interceptor binds a new Hibernate Session to the thread before a method <br />
            call, closing and removing it afterwards in case of any method outcome. <br />
            If there already was a pre-bound Session (e.g. from HibernateTransactionManager, <br />
            or from a surrounding Hibernate-intercepted method), the interceptor simply <br />
            takes part in it.</td>
        </tr>
    </tbody>
</table>
<span class="postbody"><br />
<br />
b)使用TransactionProxyFactoryBean，下面是Spring Reference中的例子 <br />
</span>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
    <tbody>
        <tr>
            <td><span class="genmed"><strong>代码:</strong></span></td>
        </tr>
        <tr>
            <td class="code"><br />
            &nbsp; &nbsp; &lt;beans&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; ... <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &lt;bean id="myTransactionManager" <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="org.springframework.orm.hibernate.HibernateTransactionManager"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="sessionFactory"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ref bean="mySessionFactory"/&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/property&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &lt;/bean&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &lt;bean id="myProductServiceTarget" class="product.ProductServiceImpl"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="productDao"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ref bean="myProductDao"/&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/property&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &lt;/bean&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &lt;bean id="myProductService" <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="transactionManager"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ref bean="myTransactionManager"/&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/property&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="target"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ref bean="myProductServiceTarget"/&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/property&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name="transactionAttributes"&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;props&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;prop key="increasePrice*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;prop key="someOtherBusinessMethod"&gt;PROPAGATION_MANDATORY&lt;/prop&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/props&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/property&gt; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &lt;/bean&gt; <br />
            &nbsp; &nbsp; &lt;/beans&gt; <br />
            &nbsp; &nbsp; </td>
        </tr>
    </tbody>
</table>
<span class="postbody"><br />
<br />
在没有其他AOP interceptor情况下，使用TransactionProxyFactoryBean是比较方便的。 <br />
事务划分一般是的业务层，而不是在DAO一层。 <br />
<br />
2.代理工厂返回的是接口AddressDao的应用，通过接口最终调用target的方法。 <br />
3.TransactionDefinition定义了所有的事务属性</span><span class="gensmall"><br />
</span><br />
<br />
<p id="TBPingURL">Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=396860</p>
<img src ="http://www.blogjava.net/terry711/aggbug/190168.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/terry711/" target="_blank">terryliu</a> 2008-04-01 17:49 <a href="http://www.blogjava.net/terry711/articles/190168.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转 Spring+Hibernate配置事务  </title><link>http://www.blogjava.net/terry711/articles/190159.html</link><dc:creator>terryliu</dc:creator><author>terryliu</author><pubDate>Tue, 01 Apr 2008 09:36:00 GMT</pubDate><guid>http://www.blogjava.net/terry711/articles/190159.html</guid><wfw:comment>http://www.blogjava.net/terry711/comments/190159.html</wfw:comment><comments>http://www.blogjava.net/terry711/articles/190159.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/terry711/comments/commentRss/190159.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/terry711/services/trackbacks/190159.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &lt;?xml&nbsp;version="1.0"&nbsp;encoding="UTF-8"?&gt;&lt;beans&nbsp;xmlns="http://www.springframework.org/schema/beans"&nbsp;&nbsp;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&nbsp;&nb...&nbsp;&nbsp;<a href='http://www.blogjava.net/terry711/articles/190159.html'>阅读全文</a><img src ="http://www.blogjava.net/terry711/aggbug/190159.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/terry711/" target="_blank">terryliu</a> 2008-04-01 17:36 <a href="http://www.blogjava.net/terry711/articles/190159.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Spring中bean的基本xml配置</title><link>http://www.blogjava.net/terry711/articles/103640.html</link><dc:creator>terryliu</dc:creator><author>terryliu</author><pubDate>Tue, 13 Mar 2007 14:14:00 GMT</pubDate><guid>http://www.blogjava.net/terry711/articles/103640.html</guid><wfw:comment>http://www.blogjava.net/terry711/comments/103640.html</wfw:comment><comments>http://www.blogjava.net/terry711/articles/103640.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/terry711/comments/commentRss/103640.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/terry711/services/trackbacks/103640.html</trackback:ping><description><![CDATA[在spring容器内拼凑bean叫作装配。装配bean的时候，你是在告诉容器，需要哪些bean，以及容器如何使用依赖注入将它们配合在一起。<br />    理论上，bean装配可以从任何资源获得，包括属性文件，关系数据库等，但xml是最常见的spring 应用系统配置源。Spring中的几种容器都支持使用xml装配bean，包括：<br />    XmlBeanFactory ，<br />    ClassPathXmlApplicationContext ，<br />    FileSystemXmlApplicationContext ，<br />    XmlWebApplicationContext <br /><br />    基本的xml配置包括如下几个方面：<br />    <br />    1．添加一个bean<br />    2．设置bean的属性<br />        2.1 手动设置<br />            2.1.1 通过Setter方法<br />            2.1.2 通过构造器<br />        2.2 自动设置<br />    其中bean的属性即为bean里的成员变量，这些成员变量值的获得可以通过setter方法，例如某个属性为name,则setter方法为setName(String name)；或者通过构造器在类被实例化时初始化。Setter方法(例如setName方法)或者构造器的调用都可以通过在xml文件里进行配置，从而实现让spring容器来自动进行。<br /><br />1．添加一个bean<br />    以下是一个例子：<br />    &lt;bean <br />        id = “mybean”<br />        Class = “blog.spring.MyBean”<br />        Singleton = “false”<br />        init-method = “initMethod”<br />        destroy-method = “destroyMethod”<br />        autowire = “autowire type”<br />    /&gt;<br />    下面是对该标签里各个属性的解释：<br />    Id : 标识该bean的名称，通过factory.getBean(“id”)来获得实例。<br />    Class : 该bean的类路径。<br />    Singleton : 默认为true，即单实例模式，每次getBean(“id”)时获取的都是同<br />一个实例，如果设置为false，即原型模式，则每次获取的是新创建<br />的实例。<br />    Init-method : 在bean实例化后要调用的方法(bean里定义好的方法)。<br />    Destroy-method : bean从容器里删除之前要调用的方法。<br />    Autowire : 其属性要通过何种方法进行属性的自动装配。<br />    对于上述的各个属性，id和class是必要的，其他的则可以省略。例如如果设置了autowire的值，则表明需要自动装配，否则是手动装配。<br /><br />2．通过Setter方法手动设置bean里的属性<br />    Bean里的属性通过&lt;property&gt;标签来标识。有以下几种情况：<br />    ● 简单类型属性<br />        &lt;bean id = “mybean” class = “blog.spring.MyBean”&gt;<br />            &lt;property name = “name”&gt;<br />                &lt;value&gt;springTest&lt;/value&gt;<br />            &lt;/property&gt;<br />        &lt;/bean&gt;<br />    ● 引用其他bean<br />        &lt;bean id = “mybean” class = “blog.spring.MyBean” /&gt;<br />        &lt;bean id = “mybean1” class = “blog.spring.MyBean1”&gt;<br />            &lt;property name = “name”&gt;<br />                &lt;ref bean = “mybean” /&gt;<br />            &lt;/property&gt;<br />        &lt;/bean&gt;<br />也可以将&lt;ref&gt;改为<br />    &lt;bean class = “..”&gt;<br />这样叫做内部bean，缺点是无法在其他地方重用这个bean的实例。<br />    ● 装配集合<br />        共有以下几种集合的装配：<br />    ****装配List和数组****<br />        &lt;property name = ”nameList”&gt;<br />            &lt;list&gt;<br />                &lt;value&gt;something&lt;/value&gt;<br />                &lt;ref bean = “blog.spring.MyBean” /&gt;<br />                &lt;value&gt;otherThing&lt;/value&gt;<br />            &lt;/list&gt;<br />        &lt;/property&gt;<br />    ****装配Set****<br />        &lt;property name = ”nameList”&gt;<br />            &lt;set&gt;<br />                &lt;value&gt;something&lt;/value&gt;<br />                &lt;ref bean = “blog.spring.MyBean” /&gt;<br />                &lt;value&gt;otherThing&lt;/value&gt;<br />            &lt;/set&gt;<br />        &lt;/property&gt;<br />    ****装配Map****<br />        &lt;property name = ”nameList”&gt;<br />            &lt;map&gt;<br />                &lt;entry key = “key1”&gt;<br />                    &lt;value&gt;value1&lt;/value&gt;<br />                &lt;/entry&gt;<br />                &lt;entry key = “key2”&gt;<br />                    &lt;ref bean = “mybean” /&gt;<br />                &lt;/entry&gt;<br />            &lt;/map&gt;<br />        &lt;/property&gt;<br />    ****装配Properties****<br />        &lt;property name = ”nameList”&gt;<br />            &lt;props&gt;<br />                &lt;prop key = “prop1”&gt;value1&lt;/prop&gt;<br />                &lt;prop key = “prop2”&gt;value2&lt;/prop&gt;<br />            &lt;/props&gt;<br />        &lt;/property&gt;<br />    ● 设置null<br />        要将一个属性null，需要通过&lt;null /&gt;标签，如果不设置，则属性为默认值(在实例化时)而不是null。<br />        &lt;property name=”name”&gt; &lt;null /&gt; &lt;/property&gt;<br /><br />3．通过构造器手动设置bean里的属性<br />    假设有如下一个bean：<br />    Public class MyBean {<br />        Public MyBean( String arg1, MyBean1 arg2, String arg3 )<br />    }<br />则可以在xml里这样配置该bean：<br />&lt;bean id = “mybean” class = “blog.spring.MyBean”&gt;<br />        &lt;constructor-arg index = “1”&gt;<br />            &lt;value&gt;springTest&lt;/value&gt;<br />        &lt;constructor-arg&gt;<br />        &lt;constructor-arg index = “0”&gt;<br />            &lt;ref bean = “mybean1” /&gt;<br />        &lt;constructor-arg&gt;<br />&lt;/bean&gt;<br />其中的index是用来标识该参数在构造函数里的位置的，并从0开始。<br /><br />4．让spring完成自动装配<br />    例如：<br />&lt;bean <br />id = “mybean” <br />class = “blog.spring.MyBean”<br />autowire = “autowire type”<br />/&gt;<br />下面是几种autowire type的说明：<br />● byname : 试图在容器中寻找和需要自动装配的属性名相同的bean或id，如果没有找到相应的bean，则这个属性未被装配上。<br />● byType : 试图在容器中寻找一个与需要自动装配的属性类型相同的bean或id，如果没有找到，则该属性未被装配上。<br />● constructor : 试图在容器中寻找与需要自动装配的bean的构造函数参数一致的一个或多个bean，如果没找到则抛出异常。<br />● autodetect : 首先尝试使用constructor来自动装配，然后再使用byType方式。<br />从上面可以看出，如果某个bean不手动设置autowire属性，则默认为手动装配。如果需要将所有bean都设置为自动装配时，可以通过在&lt;beans&gt;标签中设置default-autowire属性。&lt;beans&gt;标签是整个xml文档的根，在它下面就是一个个的&lt;bean&gt;。<br />其中default-autowire的值也有byName，byType，constructor，autodetect四种。<br />例如配置如下：<br />&lt;beans default-autowire = “byName”&gt;<br />    ...<br />&lt;/beans&gt;<br /><br />    自动装配可能带来不确定性问题。例如使用byType时可能同时发现两个相同的类型，则不知道该采用哪一个。所以可能混合采用自动和手动装配。例如，对某个bean设置为自动装配，而对其某个属性则手动明确的设置其值，例如：<br />&lt;bean id = “mybean” class = “blog.spring.MyBean”<br />    Autowire = “byType”<br />&gt;<br />    &lt;property name = “name”&gt;<br />        &lt;ref bean = “myBean1”&gt;<br />    &lt;/property&gt;<br />&lt;/bean&gt;<br />通过这样的配置，对mybean里的name属性进行手动装配，而对除name外的其他属性就进行自动装配。<br /><img src ="http://www.blogjava.net/terry711/aggbug/103640.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/terry711/" target="_blank">terryliu</a> 2007-03-13 22:14 <a href="http://www.blogjava.net/terry711/articles/103640.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Spring控制反转(IoC)的理解</title><link>http://www.blogjava.net/terry711/articles/103639.html</link><dc:creator>terryliu</dc:creator><author>terryliu</author><pubDate>Tue, 13 Mar 2007 14:13:00 GMT</pubDate><guid>http://www.blogjava.net/terry711/articles/103639.html</guid><wfw:comment>http://www.blogjava.net/terry711/comments/103639.html</wfw:comment><comments>http://www.blogjava.net/terry711/articles/103639.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/terry711/comments/commentRss/103639.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/terry711/services/trackbacks/103639.html</trackback:ping><description><![CDATA[Spring框架的核心就是控制反转(Inversion of Control)和依赖注入(Dependency Injection)，通过这两方面来实现松耦合。<br /><br />    使用IoC，对象是被动的接受依赖类，而不是自己主动的去找。容器在实例化的时候主动将它的依赖类注入给它。可以这样理解：控制反转将类的主动权转移到接口上，依赖注入通过xml配置文件在类实例化时将其依赖类注入。通过下面的实例来逐步的理解：<br /><br /><br />    首先假设有一个需求，类Business需要调用类Dependency的方法f()，按照日常的做法，得到下面的代码：<br />//**类Dependency**<br />public class Dependency {<br />    public void f() {};<br />}<br />//**类Business**<br />public  class Business {<br />    Dependency d;<br />    public Business() {<br />    d = new Dependency();<br />    }<br />    public void doSth() {<br />        d.f();<br />    }<br />}<br /><br /><br />    对上述实现做出如下修改：<br />    首先，将Business里的Dependency实例的获得该为setter方式，其次，将Dependency类改为某个接口的实现。故可以得到下面新的代码：<br />//**接口IDependency**<br />public inte***ce IDependency {<br />    void f();<br />}<br />//**类Dependency**<br />public class Dependency {<br />    public void f() {};<br />}<br />//**类Business**<br />public  class Business {<br />    IDependency d;<br />    public Business() {}<br />    public void doSth() {<br />     d.f();<br />    }<br />    public void setDependency(IDependency d) {<br />        this.d = d;<br />    }<br />}<br /><br /><br />    在新的代码中，首先Business的变量d可以接收任何IDependency的实例，另外，Dependency的实例不是通过Business来获得，而是通过setter(也可以用构造器)来由外部传给它。这似乎跟我们往常的代码没什么不同，但这已经是一个良好的设计。关键就是Dependency的实例如何从外部注入给Business呢？<br />这就要通过xml来实现了。<br /><br />    创建一个SpringFirst.xml，进行简单的配置：<br />&lt;beans&gt;<br />    &lt;bean id = "dependency" class = "aopfirst.business.Dependency" /&gt;<br />    &lt;bean<br />        id = "business"<br />        class = "aopfirst.business.Business"<br />    &gt;<br />        &lt;property name = "dependency"&gt;<br />            &lt;ref bean = "dependency" /&gt;<br />        &lt;/property&gt;<br />    &lt;/bean&gt;<br />&lt;/beans&gt;<br />    这个配置文件里将Dependency类和Business类加入，并将Dependency作为Business的一个参数。<br /><br /><br />    单有了这个xml文件还不够，还需要一个测试类来加载该xml文件，spring提供了现成的API，在加载上面的xml的时候，就进行了如下工作：实例化Dependency类，实例化Business类，并将Dependency的实例作为参数赋给了Business实例的<br />setDependency()方法。下面是该测试程序：<br /><br /><br />public class StartServer {<br />    public static void main(String [] args) {<br />     ClassPathResource cr = new ClassPathResource("SpringFirst.xml");<br />     BeanFactory factory = new XmlBeanFactory(cr);<br />     Business b = (Business)factory.getBean("business");<br />     b.doSth();<br />    }<br />}<br /><br /><br />    上面的程序加载了xml以后，获得id为"business"的bean，即Business类的实例，并调用了其doSth()方法。由此可见，Business的依赖类Dependency是通过xml来注入的，而且Business是通过接口IDependency来接收Dependency实例。因此，当我们又有新的IDependency的实现时，只需要修改xml文件即可，测试程序只需要根据xml里的id值来获得需要的参数。<br /><br />    总结上面的例子，对控制反转和依赖注入已经能理解了。依赖类(Dependency)是通过外部(xml)来注入的，而不是由使用它的类(Business)来自己制造，这就是依赖的注入。另一方面，Business对类Dependency的依赖转移到对接口IDependency的依赖，控制权由类转移到了接口，即由"实现"转移到"抽象"中。这就是控制反转。<br /><img src ="http://www.blogjava.net/terry711/aggbug/103639.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/terry711/" target="_blank">terryliu</a> 2007-03-13 22:13 <a href="http://www.blogjava.net/terry711/articles/103639.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>