﻿<?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/J2EEHOME/category/36093.html</link><description>One Target,One Dream</description><language>zh-cn</language><lastBuildDate>Thu, 27 Nov 2008 08:08:31 GMT</lastBuildDate><pubDate>Thu, 27 Nov 2008 08:08:31 GMT</pubDate><ttl>60</ttl><item><title>Spring中ApplicationContext加载机制</title><link>http://www.blogjava.net/J2EEHOME/articles/242704.html</link><dc:creator>J2EE Home工作室</dc:creator><author>J2EE Home工作室</author><pubDate>Wed, 26 Nov 2008 00:18:00 GMT</pubDate><guid>http://www.blogjava.net/J2EEHOME/articles/242704.html</guid><wfw:comment>http://www.blogjava.net/J2EEHOME/comments/242704.html</wfw:comment><comments>http://www.blogjava.net/J2EEHOME/articles/242704.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/J2EEHOME/comments/commentRss/242704.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/J2EEHOME/services/trackbacks/242704.html</trackback:ping><description><![CDATA[<div class="tit">Spring中ApplicationContext加载机制</div>
<div class="date"></div>
<table style="table-layout: fixed">
    <tbody>
        <tr>
            <td>
            <div class="cnt">
            <div class="cnt"><font color="#333333"><br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 加载器目前有两种选择：ContextLoaderListener和ContextLoaderServlet。 <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这两者在功能上完全等同，只是一个是基于Servlet2.3版本中新引入的Listener接口实现，而另一个基于Servlet接口实现。开发中可根据目标Web容器的实际情况进行选择。 <br />
            <br />
            </font>
            <table width="672" border="0">
                <tbody>
                    <tr>
                        <td width="388"><font color="#333333">配置非常简单，在web.xml中增加： <br />
                        &lt;listener&gt; <br />
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;listener-class&gt; <br />
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; org.springframework.web.context.ContextLoaderListener<br />
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/listener-class&gt; <br />
                        &lt;/listener&gt; <br />
                        或： <br />
                        &lt;servlet&gt; <br />
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;servlet-name&gt;context&lt;/servlet-name&gt; <br />
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;servlet-class&gt; <br />
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; org.springframework.web.context.ContextLoaderServlet <br />
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/servlet-class&gt; <br />
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; <br />
                        &lt;/servlet&gt; </font></td>
                        <td width="274"></td>
                    </tr>
                </tbody>
            </table>
            <font color="#333333">
            <p><br />
            通过以上配置，Web容器会自动加载/WEB-INF/applicationContext.xml初始化 <br />
            ApplicationContext实例，如果需要指定配置文件位置，可通过context-param加以指定： <br />
            &lt;context-param&gt; <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;param-value&gt;/WEB-INF/myApplicationContext.xml&lt;/param-value&gt; <br />
            &lt;/context-param&gt; <br />
            <br />
            配置完成之后，即可通过 <br />
            WebApplicationContextUtils.getWebApplicationContext方法在Web应用中获取ApplicationContext引用。 <br />
            <br />
            如：ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(); <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LoginAction action=(LoginAction)ctx.getBean("action");</p>
            <p>&nbsp;</p>
            <p>&nbsp;</p>
            <p>-------------------------------------------------------------------------------------------</p>
            <p><u><font color="#800080">spring为ApplicationContext提供有三种实现（举例） </font></u></p>
            <div class="postText">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; spring为ApplicationContext提供的3种实现分别为：ClassPathXmlApplicationContext，FileSystemXmlApplicationContext和XmlWebApplicationContext，其中XmlWebApplicationContext是专为Web工程定制的。使用举例如下：<br />
            &nbsp;&nbsp;<strong> 1. FileSystemXmlApplicationContext</strong><br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eg1. ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml"); //加载单个配置文件<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eg2. <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ApplicationContext ctx = new FileSystemXmlApplicationContext(locations ); //加载多</div>
            </font><font color="#333333">个配置文件<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eg3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <div style="text-indent: 31.5pt" align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ApplicationContext ctx =new FileSystemXmlApplicationContext("D:/project/bean.xml");//根据具体路径加载文件<br />
            &nbsp;&nbsp;<strong>2. ClassPathXmlApplicationContext</strong><br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eg1.&nbsp;&nbsp;<span>ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eg2. <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ApplicationContext ctx = new ClassPathXmlApplication(locations);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 注：其中FileSystemXmlApplicationContext和ClassPathXmlApplicationContext与BeanFactory的xml文件定位方式一样是基于路径的。<br />
            <strong>3. XmlWebApplicationContext</strong><br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eg1. ServletContext servletContext = request.getSession().getServletContext();&nbsp;&nbsp;&nbsp;&nbsp;<br />
            <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ApplicationContext ctx = Web<span>ApplicationContextUtils.getWeb<span>ApplicationContext(servletContext);</span></span></span></span></div>
            <div style="text-indent: 31.5pt" align="left"><span><span><span></span></span></span></div>
            <div style="text-indent: 31.5pt" align="left"><span><span><span><span>注 : 一般是 ApplicationContext ctx = Web<span>ApplicationContextUtils.getWeb<span>ApplicationContext(this.getServletContext());</span></span></span></span></span></span></div>
            </font></div>
            </div>
            </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.blogjava.net/J2EEHOME/aggbug/242704.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/J2EEHOME/" target="_blank">J2EE Home工作室</a> 2008-11-26 08:18 <a href="http://www.blogjava.net/J2EEHOME/articles/242704.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>轻松学习Spring&lt;一&gt; IoC容器和Dependency Injection模式 </title><link>http://www.blogjava.net/J2EEHOME/articles/241451.html</link><dc:creator>J2EE Home工作室</dc:creator><author>J2EE Home工作室</author><pubDate>Wed, 19 Nov 2008 10:34:00 GMT</pubDate><guid>http://www.blogjava.net/J2EEHOME/articles/241451.html</guid><wfw:comment>http://www.blogjava.net/J2EEHOME/comments/241451.html</wfw:comment><comments>http://www.blogjava.net/J2EEHOME/articles/241451.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/J2EEHOME/comments/commentRss/241451.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/J2EEHOME/services/trackbacks/241451.html</trackback:ping><description><![CDATA[<p><span style="color: black; font-family: 宋体">最近公司需要，项目中要用到</span><span style="color: black; font-family: 'Lucida Bright'">Spring</span><span style="color: black; font-family: 宋体">和</span><span style="color: black; font-family: 'Lucida Bright'">Ibatis</span><span style="color: black; font-family: 宋体">。趁着过年好好学习学习。</span><span style="color: black; font-family: 'Lucida Bright'">Ibatis</span><span style="color: black; font-family: 宋体">就如同</span><span style="color: black; font-family: 'Lucida Bright'">Hibernate</span><span style="color: black; font-family: 宋体">一样的持久层技术，学习起来难度不大，但</span><span style="color: black; font-family: 'Lucida Bright'">Spring</span><span style="color: black; font-family: 宋体">可不一样，揣着</span><span style="color: black; font-family: 'Lucida Bright'">Ioc</span><span style="color: black; font-family: 宋体">，</span><span style="color: black; font-family: 'Lucida Bright'">DJ</span><span style="color: black; font-family: 宋体">和</span><span style="color: black; font-family: 'Lucida Bright'">AOP</span><span style="color: black; font-family: 宋体">，四处走红。学起来可不容易。就市面上而言，就一本《</span><span style="color: black; font-family: 'Lucida Bright'">expert one-on-one J2EE Development without EJB</span><span style="color: black; font-family: 宋体">中文版》值得参考，为了生活，再贵也得买。这本书的前五章都是说</span><span style="color: black; font-family: 'Lucida Bright'">EJB</span><span style="color: black; font-family: 宋体">的不是，从第六章开始进入正题，介绍控制反转，以后基本都是说</span><span style="color: black; font-family: 'Lucida Bright'">Spring</span><span style="color: black; font-family: 宋体">了。</span></p>
<p><span style="color: black; font-family: 'Lucida Bright'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: black; font-family: 宋体">可能本人比较愚笨，控制反转弄得不明白。这样就得上网上找答案了。最后在一个叫</span><span style="font-family: 'Lucida Bright'">Bromon</span><span style="font-family: 宋体">的</span><span style="font-family: 'Lucida Bright'">blog</span><span style="font-family: 宋体">上找到的浅显易懂的答案。下面就是引用他说的话：</span></p>
<hr width="100%" size="2" />
<p><font color="#006400"><span style="font-family: 'Lucida Bright'">IoC</span><span style="font-family: 宋体">与</span><span style="font-family: 'Lucida Bright'">DI</span></font></p>
<p><font color="#006400"><span style="font-family: 宋体">　　</span><span style="font-family: 宋体">首先想说说</span><span style="font-family: 'Lucida Bright'">IoC</span><span style="font-family: 宋体">（</span><span style="font-family: 'Lucida Bright'">Inversion of Control</span><span style="font-family: 宋体">，控制倒转）。这是</span><span style="font-family: 'Lucida Bright'">spring</span><span style="font-family: 宋体">的核心，贯穿始终。所谓</span><span style="font-family: 'Lucida Bright'">IoC</span><span style="font-family: 宋体">，对于</span><span style="font-family: 'Lucida Bright'">spring</span><span style="font-family: 宋体">框架来说，就是由</span><span style="font-family: 'Lucida Bright'">spring</span><span style="font-family: 宋体">来负责控制对象的生命周期和</span><span style="font-family: 宋体">对象间的关系。这是什么意思呢，举个简单的例子，我们是如何找女朋友的？常见的情况是，我们到处去看哪里有长得漂亮身材又好的</span><span style="font-family: 'Lucida Bright'">mm</span><span style="font-family: 宋体">，然后打听她们的兴趣爱</span><span style="font-family: 宋体">好、</span><span style="font-family: 'Lucida Bright'">qq</span><span style="font-family: 宋体">号、电话号、</span><span style="font-family: 'Lucida Bright'">ip</span><span style="font-family: 宋体">号、</span><span style="font-family: 'Lucida Bright'">iq</span><span style="font-family: 宋体">号</span><span style="font-family: 'Lucida Bright'">&#8230;&#8230;&#8230;</span><span style="font-family: 宋体">，想办法认识她们，投其所好送其所要，然后嘿嘿</span><span style="font-family: 'Lucida Bright'">&#8230;&#8230;</span><span style="font-family: 宋体">这个过程是复杂深奥的，我们必须自己设计和面对每个环节。传</span><span style="font-family: 宋体">统的程序开发也是如此，在一个对象中，如果要使用另外的对象，就必须得到它（自己</span><span style="font-family: 'Lucida Bright'">new</span><span style="font-family: 宋体">一个，或者从</span><span style="font-family: 'Lucida Bright'">JNDI</span><span style="font-family: 宋体">中查询一个），使用完之后还要将对象销毁（比</span><span style="font-family: 宋体">如</span><span style="font-family: 'Lucida Bright'">Connection</span><span style="font-family: 宋体">等），对象始终会和其他的接口或类藕合起来。</span></font></p>
<p><font color="#006400"><span style="font-family: 宋体">　　那么</span><span style="font-family: 'Lucida Bright'">IoC</span><span style="font-family: 宋体">是如何做的呢？有点像通过婚介找女朋友，在我和女朋友之间引入了一个第三者：婚姻介绍所。婚介管理了很多男男女女的资料，我可以向婚</span><span style="font-family: 宋体">介提出一个列表，告诉它我想找个什么样的女朋友，比如长得像李嘉欣，身材像林熙雷，唱歌像周杰伦，速度像卡洛斯，技术像齐达内之类的，然后婚介就会按照我</span><span style="font-family: 宋体">们的要求，提供一个</span><span style="font-family: 'Lucida Bright'">mm</span><span style="font-family: 宋体">，我们只需要去和她谈恋爱、结婚就行了。简单明了，如果婚介给我们的人选不符合要求，我们就会抛出异常。整个过程不再由我自己控</span><span style="font-family: 宋体">制，而是有婚介这样一个类似容器的机构来控制。</span><span style="font-family: 'Lucida Bright'">Spring</span><span style="font-family: 宋体">所倡导的开发方式就是如此，所有的类都会在</span><span style="font-family: 'Lucida Bright'">spring</span><span style="font-family: 宋体">容器中登记，告诉</span><span style="font-family: 'Lucida Bright'">spring</span><span style="font-family: 宋体">你是个什</span><span style="font-family: 宋体">么东西，你需要什么东西，然后</span><span style="font-family: 'Lucida Bright'">spring</span><span style="font-family: 宋体">会在系统运行到适当的时候，把你要的东西主动给你，同时也把你交给其他需要你的东西。所有的类的创建、销毁都由</span><span style="font-family: 'Lucida Bright'"> spring</span><span style="font-family: 宋体">来控制，也就是说控制对象生存周期的不再是引用它的对象，而是</span><span style="font-family: 'Lucida Bright'">spring</span><span style="font-family: 宋体">。对于某个具体的对象而言，以前是它控制其他对象，现在是所有对象</span><span style="font-family: 宋体">都被</span><span style="font-family: 'Lucida Bright'">spring</span><span style="font-family: 宋体">控制，所以这叫控制反转。如果你还不明白的话，我决定放弃。</span></font></p>
<p style="text-indent: 21pt"><font color="#006400"><span style="font-family: 'Lucida Bright'">IoC</span><span style="font-family: 宋体">的一个重点是在系统运行中，动态的向某个对象提供它所需要的其他对象。这一点是通过</span><span style="font-family: 'Lucida Bright'">DI</span><span style="font-family: 宋体">（</span><span style="font-family: 'Lucida Bright'">Dependency Injection</span><span style="font-family: 宋体">，依赖注入）来实现的。比如对象</span><span style="font-family: 'Lucida Bright'">A</span><span style="font-family: 宋体">需要操作数据库，以前我们总是要在</span><span style="font-family: 'Lucida Bright'">A</span><span style="font-family: 宋体">中自己编写代码来获得一个</span><span style="font-family: 'Lucida Bright'">Connection</span><span style="font-family: 宋体">对象，有了</span><span style="font-family: 'Lucida Bright'"> spring</span><span style="font-family: 宋体">我们就只需要告诉</span><span style="font-family: 'Lucida Bright'">spring</span><span style="font-family: 宋体">，</span><span style="font-family: 'Lucida Bright'">A</span><span style="font-family: 宋体">中需要一个</span><span style="font-family: 'Lucida Bright'">Connection</span><span style="font-family: 宋体">，至于这个</span><span style="font-family: 'Lucida Bright'">Connection</span><span style="font-family: 宋体">怎么构造，何时构造，</span><span style="font-family: 'Lucida Bright'">A</span><span style="font-family: 宋体">不需要知道。在系统</span><span style="font-family: 宋体">运行时，</span><span style="font-family: 'Lucida Bright'">spring</span><span style="font-family: 宋体">会在适当的时候制造一个</span><span style="font-family: 'Lucida Bright'">Connection</span><span style="font-family: 宋体">，然后像打针一样，注射到</span><span style="font-family: 'Lucida Bright'">A</span><span style="font-family: 宋体">当中，这样就完成了对各个对象之间关系的控制。</span><span style="font-family: 'Lucida Bright'">A</span><span style="font-family: 宋体">需要依赖</span><span style="font-family: 'Lucida Bright'"> Connection</span><span style="font-family: 宋体">才能正常运行，而这个</span><span style="font-family: 'Lucida Bright'">Connection</span><span style="font-family: 宋体">是由</span><span style="font-family: 'Lucida Bright'">spring</span><span style="font-family: 宋体">注入到</span><span style="font-family: 'Lucida Bright'">A</span><span style="font-family: 宋体">中的，依赖注入的名字就这么来的。那么</span><span style="font-family: 'Lucida Bright'">DI</span><span style="font-family: 宋体">是如何实现的呢？</span><span style="font-family: 'Lucida Bright'"> Java 1.3</span><span style="font-family: 宋体">之后一个重要特征是反射（</span><span style="font-family: 'Lucida Bright'">reflection</span><span style="font-family: 宋体">），它允许程序在运行的时候动态的生成对象、执行对象的方法、改变对象的属性，</span><span style="font-family: 'Lucida Bright'">spring</span><span style="font-family: 宋体">就是通过反射来实现注入的。关于反射的相关资料请查阅</span><span style="font-family: 'Lucida Bright'">java doc</span><span style="font-family: 宋体">。</span></font></p>
<hr width="100%" size="2" />
<p><span style="font-family: 宋体">&nbsp;&nbsp;&nbsp; 我想通过</span><span style="font-family: 'Lucida Bright'">Bromon</span><span style="font-family: 宋体">的介绍，大家对</span><span style="font-family: 'Lucida Bright'">IoC</span><span style="font-family: 宋体">和</span><span style="font-family: 'Lucida Bright'">DI</span><span style="font-family: 宋体">都有了比较生动的理解了。再来看看《</span><span style="font-family: 'Lucida Bright'">expert one-on-one J2EE Development without EJB</span><span style="font-family: 宋体">中文版》是怎么解释这两个概念的。书上是这么说的：</span></p>
<p style="text-indent: 21pt"><span style="font-family: 'Lucida Bright'">IoC</span><span style="font-family: 宋体">是一个很大的概念，可以用不同的方式来实现。主要的实现形式有两种</span><span style="font-family: 'Lucida Bright'">:</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">依赖查找：容器提供回调接口和上下文环境给组件。</span><span style="font-family: 'Lucida Bright'">EJB</span><span style="font-family: 宋体">和</span><span style="font-family: 'Lucida Bright'">Apache Avalon</span><span style="font-family: 宋体">都是使用这种方式。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">依赖注入：组件不做定位查询，只是提供普通的</span><span style="font-family: 'Lucida Bright'">Java</span><span style="font-family: 宋体">方法让容器去决定依赖关系。容器全权负责组件的装配，它会把符合依赖关系的对象通过</span><span style="font-family: 'Lucida Bright'">JavaBean</span><span style="font-family: 宋体">属性或者构造子传递给需要的对象。通过</span><span style="font-family: 'Lucida Bright'">JavaBean</span><span style="font-family: 宋体">属性注射依赖关系的做法称为设值方法注入（</span><span style="font-family: 'Lucida Bright'">Setter Injection</span><span style="font-family: 宋体">）；将依赖关系作为构造子参数传入的做法称为构造子注入（</span><span style="font-family: 'Lucida Bright'">Constructor Injection</span><span style="font-family: 宋体">）。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">附图说明：</span></p>
<br />
<p style="text-indent: 21pt"><img height="269" alt="spring_2.jpg" src="http://www.blogjava.net/images/blogjava_net/rickhunter/easyspring/spring_2.jpg" width="555" border="0" /></p>
&nbsp;&nbsp;&nbsp;
<p style="text-indent: 21pt"><span style="font-family: 宋体">到这里，大家应该对</span><span style="font-family: 'Lucida Bright'">IoC</span><span style="font-family: 宋体">与</span><span style="font-family: 'Lucida Bright'">DI</span><span style="font-family: 宋体">都有了初步的认识了。其实就</span><span style="font-family: 'Lucida Bright'">Spring</span><span style="font-family: 宋体">来说，就是</span><span style="font-family: 'Lucida Bright'">JavaBean</span><span style="font-family: 宋体">由</span><span style="font-family: 'Lucida Bright'">Spring</span><span style="font-family: 宋体">来管理组装，表面上看就少了几个</span><span style="font-family: 'Lucida Bright'">new</span><span style="font-family: 宋体">字，其实就是为了降低耦合度，这也是我们做软件的目标之一。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">至于</span><span style="font-family: 'Lucida Bright'">Spring</span><span style="font-family: 宋体">是怎样实现</span><span style="font-family: 'Lucida Bright'">IoC</span><span style="font-family: 宋体">的，</span><span style="font-family: 宋体">《</span><span style="font-family: 'Lucida Bright'">expert one-on-one J2EE Development without EJB</span><span style="font-family: 宋体">中文版》第七章&#8220;</span><span style="font-family: 'Lucida Bright'">Spring</span><span style="font-family: 宋体">框架介绍&#8221;很详细的列举了多种方法。说实在，一下子看这么多，我真有点糊涂了。我还是自己写个</span><span style="font-family: 'Lucida Bright'">Demo</span><span style="font-family: 宋体">熟悉一下大名鼎鼎的</span><span style="font-family: 'Lucida Bright'">Spring</span><span style="font-family: 宋体">吧。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">首先得下载</span><span style="font-family: 'Lucida Bright'">Spring</span><span style="font-family: 宋体">。</span><span style="font-family: 'Lucida Bright'">Spring</span><span style="font-family: 宋体">网上有两种</span><span style="font-family: 'Lucida Bright'">Spring </span><span style="font-family: 宋体">包一种是</span><span style="font-family: 'Lucida Bright'">spring-framework-1.2.6-with-dependencies.zip</span><span style="font-family: 宋体">，另一种是</span><span style="font-family: 'Lucida Bright'">spring-framework-1.2.6.zip</span><span style="font-family: 宋体">。当然最好是下载</span><span style="font-family: 'Lucida Bright'">spring-framework-1.2.6-with-dependencies.zip</span><span style="font-family: 宋体">形式的，因为里面包括了更多的东东。</span><span style="font-family: 'Lucida Bright'">spring-framework-1.2.6-with-dependencies.zip</span><span style="font-family: 宋体">的下载地址是：</span><span style="font-family: 'Lucida Bright'"><a href="http://prdownloads.sourceforge.net/springframework/spring-framework-1.2.6-with-dependencies.zip">http://prdownloads.sourceforge.net/springframework/spring-framework-1.2.6-with-dependencies.zip</a></span><span style="font-family: 宋体">。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">下载下来，解压后，你会发现里面有很多</span><span style="font-family: 'Lucida Bright'">jar</span><span style="font-family: 宋体">文件。因为刚刚接触</span><span style="font-family: 'Lucida Bright'">Spring</span><span style="font-family: 宋体">，因此我只需要</span><span style="font-family: 'Lucida Bright'">spring-core.jar</span><span style="font-family: 宋体">（</span><span style="font-family: 'Lucida Bright'">spring-framework-1.2.6"dist</span><span style="font-family: 宋体">），将其导入</span><span style="font-family: 'Lucida Bright'">eclipse</span><span style="font-family: 宋体">的构建路径中。另外，</span><span style="font-family: 'Lucida Bright'">log</span><span style="font-family: 宋体">日志是需要的，这也是为了养成良好的编程习惯。将</span><span style="font-family: 'Lucida Bright'">log4j-1.2.9.jar</span><span style="font-family: 宋体">（</span><span style="font-family: 'Lucida Bright'">spring-framework-1.2.6"lib"log4j</span><span style="font-family: 宋体">）和</span><span style="font-family: 'Lucida Bright'">commons-logging.jar</span><span style="font-family: 宋体">（</span><span style="font-family: 'Lucida Bright'">spring-framework-1.2.6"lib"jakarta-commons</span><span style="font-family: 宋体">）导入到构建路径中。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">准备就绪，开始写</span><span style="font-family: 'Lucida Bright'">Demo</span><span style="font-family: 宋体">了。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">我的工程的结构是：</span></p>
&nbsp;&nbsp; <img height="226" alt="spring_1.jpg" src="http://www.blogjava.net/images/blogjava_net/rickhunter/easyspring/spring_1.jpg" width="251" border="0" /><br />
<p style="margin-left: 39pt; text-indent: -18pt">&lt;!--[if !supportLists]--&gt;<span style="font-family: 'Lucida Bright'">1、</span>&lt;!--[endif]--&gt;<span style="font-family: 'Lucida Bright'">log4j.properties</span><span style="font-family: 宋体">代码：</span></p>
<div style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: rgb(204,204,204) 1px solid; width: 98%; padding-top: 4px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(238,238,238)"><span style="color: rgb(0,0,0)">log4j.rootLogger</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">Debug,&nbsp;stdout<br />
log4j.appender.stdout</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">org.apache.log4j.ConsoleAppender<br />
log4j.appender.stdout.layout</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">org.apache.log4j.PatternLayout<br />
log4j.appender.stdout.layout.ConversionPattern</span><span style="color: rgb(0,0,0)">=%</span><span style="color: rgb(0,0,0)">c{</span><span style="color: rgb(0,0,0)">1</span><span style="color: rgb(0,0,0)">}&nbsp;</span><span style="color: rgb(0,0,0)">-</span>&nbsp;<span style="color: rgb(0,0,0)">%</span><span style="color: rgb(0,0,0)">m</span><span style="color: rgb(0,0,0)">%</span><span style="color: rgb(0,0,0)">n<br />
</span></div>
<p style="margin-left: 21pt"><span style="color: rgb(97,77,179); font-family: 宋体">如何使用</span><span style="color: rgb(97,77,179); font-family: 'Lucida Bright'">Log4j</span><span style="color: rgb(97,77,179); font-family: 宋体">，请看我的另一篇转贴的文章：</span><a href="http://www.blogjava.net/rickhunter/articles/28133.html"><span style="font-family: 宋体">如何使用</span><span style="font-family: 'Lucida Bright'">Log4J</span></a><span style="font-family: 宋体">。</span></p>
<p style="margin-left: 39pt; text-indent: -18pt">&lt;!--[if !supportLists]--&gt;<span style="font-family: 'Lucida Bright'">2、</span>&lt;!--[endif]--&gt;<span style="font-family: 'Lucida Bright'">HelloBean</span><span style="font-family: 宋体">的代码：</span></p>
<div style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: rgb(204,204,204) 1px solid; width: 98%; padding-top: 4px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(238,238,238)"><span style="color: rgb(0,0,255)">package</span><span style="color: rgb(0,0,0)">&nbsp;com;<br />
<br />
</span><span style="color: rgb(0,0,255)">public</span>&nbsp;<span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)">&nbsp;HelloBean&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,255)">private</span><span style="color: rgb(0,0,0)">&nbsp;String&nbsp;helloworld</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">Hello!World!</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)">&nbsp;String&nbsp;getHelloworld()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)">&nbsp;helloworld;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,255)">public</span>&nbsp;<span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)">&nbsp;setHelloworld(String&nbsp;helloworld)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,255)">this</span><span style="color: rgb(0,0,0)">.helloworld&nbsp;</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">&nbsp;helloworld;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
</span></div>
<br />
<p style="margin-left: 21pt"><span style="font-family: 宋体">这是一个简单的</span><span style="font-family: 'Lucida Bright'">JavaBean</span><span style="font-family: 宋体">，有个</span><span style="font-family: 'Lucida Bright'">String</span><span style="font-family: 宋体">类型的</span><span style="font-family: 'Lucida Bright'">helloworld</span><span style="font-family: 宋体">属性，初始值是</span><span style="font-family: 'Lucida Bright'">"Hello!World!"</span><span style="font-family: 宋体">。</span>&nbsp;</p>
<p style="margin-left: 39pt; text-indent: -18pt">&lt;!--[if !supportLists]--&gt;<span style="font-family: 'Lucida Bright'">3、</span>&lt;!--[endif]--&gt;<span style="font-family: 'Lucida Bright'">Bean.xml</span><span style="font-family: 宋体">代码：</span></p>
<div style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: rgb(204,204,204) 1px solid; width: 98%; padding-top: 4px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(238,238,238)"><span style="color: rgb(0,0,0)">&lt;?</span><span style="color: rgb(0,0,0)">xml&nbsp;version</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">1.0</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&nbsp;encoding</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">GBK</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">?&gt;</span><span style="color: rgb(0,0,0)"><br />
</span><span style="color: rgb(0,0,0)">&lt;!</span><span style="color: rgb(0,0,0)">DOCTYPE&nbsp;beans&nbsp;PUBLIC&nbsp;</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">-//SPRING/DTD&nbsp;BEAN/EN</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">http://www.springframework.org/dtd/spring-beans.dtd</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
<br />
</span><span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">beans</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">bean&nbsp;id</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">helloBean</span><span style="color: rgb(0,0,0)">"</span>&nbsp;<span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">com.HelloBean</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">property&nbsp;name</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">helloworld</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">value</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)">Hello</span><span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,0)">Rick</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">value</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">property</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">bean</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">beans</span><span style="color: rgb(0,0,0)">&gt;<br />
</span></div>
<p style="margin-left: 39pt; text-indent: -18pt"><span style="color: black; font-family: 'Lucida Bright'">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Spirng重点之一就是配置文件，上面是个相当简单的配置文件，我想大家都应该看得懂。最后就是写应用程序了。</span></p>
<p style="margin-left: 39pt; text-indent: -18pt"><span style="color: black; font-family: 'Lucida Bright'">4、</span>&lt;!--[endif]--&gt;<span style="background: white 0% 50%; color: black; font-family: 'Lucida Bright'; moz-background-clip: -moz-initial; moz-background-origin: -moz-initial; moz-background-inline-policy: -moz-initial">Test</span><span style="background: white 0% 50%; color: black; font-family: 宋体; moz-background-clip: -moz-initial; moz-background-origin: -moz-initial; moz-background-inline-policy: -moz-initial">的代码：</span></p>
<div style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: rgb(204,204,204) 1px solid; width: 98%; padding-top: 4px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(238,238,238)"><span style="color: rgb(0,0,255)">package</span><span style="color: rgb(0,0,0)">&nbsp;com;<br />
<br />
</span><span style="color: rgb(0,0,255)">import</span><span style="color: rgb(0,0,0)">&nbsp;org.springframework.beans.factory.</span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(0,0,0)">;<br />
</span><span style="color: rgb(0,0,255)">import</span><span style="color: rgb(0,0,0)">&nbsp;org.springframework.beans.factory.xml.XmlBeanFactory;<br />
</span><span style="color: rgb(0,0,255)">import</span><span style="color: rgb(0,0,0)">&nbsp;org.springframework.core.io.ClassPathResource;<br />
</span><span style="color: rgb(0,0,255)">import</span><span style="color: rgb(0,0,0)">&nbsp;org.springframework.core.io.Resource;<br />
<br />
</span><span style="color: rgb(0,0,255)">public</span>&nbsp;<span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)">&nbsp;Test&nbsp;{<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,255)">public</span>&nbsp;<span style="color: rgb(0,0,255)">static</span>&nbsp;<span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)">&nbsp;main(String[]&nbsp;args)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">实例化JavaBean，主要是为了比较new对象和依赖注入两者的区别</span><span style="color: rgb(0,128,0)"><br />
</span><span style="color: rgb(0,0,0)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HelloBean&nbsp;hellobean</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)">&nbsp;HelloBean();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(hellobean.getHelloworld());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">通过Spring访问JavaBean组件</span><span style="color: rgb(0,128,0)"><br />
</span><span style="color: rgb(0,0,0)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Resource&nbsp;resource</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)">&nbsp;ClassPathResource(</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">com/bean.xml</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BeanFactory&nbsp;factory</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)">&nbsp;XmlBeanFactory(resource);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hellobean</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">(HelloBean)factory.getBean(</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">helloBean</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(hellobean.getHelloworld());<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
</span></div>
&nbsp;&nbsp;&nbsp; 这个Demo很好的阐述了Spring的Ioc，其实就Spring而言，就是通过配置文件，让Spring如同一个管家一样来管理所有的Bean类。<br />
<br />
&nbsp;&nbsp;&nbsp; Spring的依赖注入相对复杂一点，主要是明白调用别的Bean，不是通过实例化对象来调用，而是告诉Spring，我需要什么Bean，然后Spring再向你的Bean里面注入你所需要的Bean对象。<br />
&nbsp;&nbsp;&nbsp; 接下来说说代码实现，我只是在刚刚的例子上再添加一点东东。<br />
&nbsp;&nbsp;&nbsp; 首先要增加一个HelloImp的接口，这是问什么呢，那你得问Spring，它定的规矩：JavaBean的实现要有两个部分，一个接口，一个默认实现。你不照做就不行。<br />
&nbsp;&nbsp;&nbsp; HelloImp代码：&nbsp;&nbsp; <br />
&nbsp; &nbsp;&nbsp;&nbsp;
<div style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: rgb(204,204,204) 1px solid; width: 98%; padding-top: 4px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(238,238,238)"><span style="color: rgb(0,0,255)">package</span><span style="color: rgb(0,0,0)">&nbsp;com;<br />
<br />
</span><span style="color: rgb(0,0,255)">public</span>&nbsp;<span style="color: rgb(0,0,255)">interface</span><span style="color: rgb(0,0,0)">&nbsp;HelloImp&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,255)">public</span>&nbsp;<span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)">&nbsp;getName();<br />
}</span><span style="color: rgb(0,0,0)"><br />
</span></div>
&nbsp;&nbsp;&nbsp; <span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp; 实现HelloImp的Hello代码：<br />
</span>&nbsp;&nbsp;
<div style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: rgb(204,204,204) 1px solid; width: 98%; padding-top: 4px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(238,238,238)"><span style="color: rgb(0,0,255)">package</span><span style="color: rgb(0,0,0)">&nbsp;com;<br />
<br />
</span><span style="color: rgb(0,0,255)">public</span>&nbsp;<span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)">&nbsp;Hello&nbsp;</span><span style="color: rgb(0,0,255)">implements</span><span style="color: rgb(0,0,0)">&nbsp;HelloImp&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,255)">public</span>&nbsp;<span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)">&nbsp;getName(){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">Jack</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</span></div>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <br />
<span style="color: black; font-family: 宋体">&nbsp;&nbsp;&nbsp; 接着就是在</span><span style="color: black; font-family: 'Lucida Bright'">HelloBean</span><span style="color: black; font-family: 宋体">中调用</span><span style="color: black; font-family: 'Lucida Bright'">Hello</span><span style="color: black; font-family: 宋体">了。</span><span style="color: black; font-family: 'Lucida Bright'">Spring</span><span style="color: black; font-family: 宋体">的不同之处也在这体现出来。</span>
<div style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: rgb(204,204,204) 1px solid; width: 98%; padding-top: 4px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(238,238,238)"><span style="color: rgb(0,0,255)">package</span><span style="color: rgb(0,0,0)">&nbsp;com;<br />
<br />
</span><span style="color: rgb(0,0,255)">public</span>&nbsp;<span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)">&nbsp;HelloBean&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,255)">private</span><span style="color: rgb(0,0,0)">&nbsp;String&nbsp;helloworld</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">Hello!World!</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">;<br />
&nbsp;&nbsp;&nbsp;<strong>&nbsp;</strong></span><strong><font color="#006400"><font face="Courier New"><span style="color: rgb(0,0,255)">private</span></font><span style="color: rgb(0,0,0)"><font face="Courier New">&nbsp;HelloImp&nbsp;hello;&nbsp;</font>&nbsp;</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">注意这个私有对象是借口</span></font></strong><span style="color: rgb(0,128,0)"><br />
</span><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)">&nbsp;String&nbsp;getHelloworld()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)">&nbsp;helloworld;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,255)">public</span>&nbsp;<span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)">&nbsp;setHelloworld(String&nbsp;helloworld)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,255)">this</span><span style="color: rgb(0,0,0)">.helloworld&nbsp;</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">&nbsp;helloworld;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><font face="Courier New"><span style="color: rgb(0,0,255)">public</span>&nbsp;<span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)">&nbsp;setHello(HelloImp&nbsp;hello)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,255)">this</span><span style="color: rgb(0,0,0)">.hello&nbsp;</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">&nbsp;hello;<br />
&nbsp; &nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp; </span><span style="color: rgb(0,0,255)">public</span>&nbsp;<span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)">&nbsp;get(){<br />
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; </span><span style="color: rgb(0,0,255)">this</span></font></strong><span style="color: rgb(0,0,0)"><strong><font face="Courier New">.hello.getName();<br />
&nbsp;&nbsp; }</font></strong><br />
}<br />
</span></div>
<span style="font-family: 宋体">&nbsp;&nbsp;&nbsp; 注意字体加粗的地方。<br />
</span><span style="font-family: 宋体"><br />
&nbsp;&nbsp;&nbsp; 配置文件也需要增加一点东西：</span> <br />
&nbsp;&nbsp;&nbsp;
<div style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: rgb(204,204,204) 1px solid; width: 98%; padding-top: 4px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(238,238,238)"><span style="color: rgb(0,0,0)">&lt;?</span><span style="color: rgb(0,0,0)">xml&nbsp;version</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">1.0</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&nbsp;encoding</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">GBK</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">?&gt;</span><span style="color: rgb(0,0,0)"><br />
</span><span style="color: rgb(0,0,0)">&lt;!</span><span style="color: rgb(0,0,0)">DOCTYPE&nbsp;beans&nbsp;PUBLIC&nbsp;</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">-//SPRING/DTD&nbsp;BEAN/EN</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">http://www.springframework.org/dtd/spring-beans.dtd</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
<br />
</span><span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">beans</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
</span><span style="color: rgb(0,0,0)">&lt;!</span><span style="color: rgb(0,0,0)">—注意引用的类是具体的类Hello</span><span style="color: rgb(0,0,0)">--&gt;</span><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">bean&nbsp;id</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">myHello</span><span style="color: rgb(0,0,0)">"</span>&nbsp;<span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">com.Hello</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">bean</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">bean&nbsp;id</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">helloBean</span><span style="color: rgb(0,0,0)">"</span>&nbsp;<span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">com.HelloBean</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">property&nbsp;name</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">helloworld</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">value</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)">Hello</span><span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,0)">Rick</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">value</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">property</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">property&nbsp;name</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">hello</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">ref&nbsp;bean</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">myHello</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">&gt;&lt;/</span><span style="color: rgb(0,0,0)">ref</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">property</span><span style="color: rgb(0,0,0)">&gt;</span></strong><span style="color: rgb(0,0,0)"><br />
&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">bean</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">beans</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)"><br />
</span></div>
<br />
&nbsp;&nbsp;&nbsp; 注意字体加粗的部分。<br />
<br />
&nbsp;&nbsp;&nbsp; <span style="color: black; font-family: 宋体">最后在</span><span style="color: black; font-family: 'Lucida Bright'">Test</span><span style="color: black; font-family: 宋体">中添加一句</span><span style="color: black; font-family: 'Lucida Bright'">hellobean.get();</span><span style="color: black; font-family: 宋体">就可以看到结果了。<br />
<br />
<div style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: rgb(204,204,204) 1px solid; width: 98%; padding-top: 4px; border-bottom: rgb(204,204,204) 1px solid; background-color: rgb(238,238,238)"><span style="color: rgb(0,0,255)">package</span><span style="color: rgb(0,0,0)">&nbsp;com;<br />
<br />
</span><span style="color: rgb(0,0,255)">import</span><span style="color: rgb(0,0,0)">&nbsp;org.springframework.beans.factory.</span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(0,0,0)">;<br />
</span><span style="color: rgb(0,0,255)">import</span><span style="color: rgb(0,0,0)">&nbsp;org.springframework.beans.factory.xml.XmlBeanFactory;<br />
</span><span style="color: rgb(0,0,255)">import</span><span style="color: rgb(0,0,0)">&nbsp;org.springframework.core.io.ClassPathResource;<br />
</span><span style="color: rgb(0,0,255)">import</span><span style="color: rgb(0,0,0)">&nbsp;org.springframework.core.io.Resource;<br />
</span><span style="color: rgb(0,0,0)"><br />
</span><span style="color: rgb(0,0,255)">public</span>&nbsp;<span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)">&nbsp;Test&nbsp;{<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0,0,255)">public</span>&nbsp;<span style="color: rgb(0,0,255)">static</span>&nbsp;<span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)">&nbsp;main(String[]&nbsp;args)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HelloBean&nbsp;hellobean</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)">&nbsp;HelloBean();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(hellobean.getHelloworld());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Resource&nbsp;resource</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)">&nbsp;ClassPathResource(</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">com/bean.xml</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BeanFactory&nbsp;factory</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)">&nbsp;XmlBeanFactory(resource);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hellobean</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">(HelloBean)factory.getBean(</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">helloBean</span><span style="color: rgb(0,0,0)">"</span><span style="color: rgb(0,0,0)">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(hellobean.getHelloworld());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>hellobean.get();</strong><br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
</span></div>
</span><br />
到这，Spring的IoC和DI总算有了一定的认识，也算是敲开了Spring的大门了。
<img src ="http://www.blogjava.net/J2EEHOME/aggbug/241451.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/J2EEHOME/" target="_blank">J2EE Home工作室</a> 2008-11-19 18:34 <a href="http://www.blogjava.net/J2EEHOME/articles/241451.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>