<?xml version="1.0" encoding="UTF-8"?>
  <feed xmlns="http://www.w3.org/2005/Atom">
  <title type="html"><![CDATA[21 工作室 - spring]]></title>
  <subtitle type="html"><![CDATA[本站承接网页制作、软件项目开发、代写代码等业务，我们将和您真诚合作，以最低的价格提供最优越的服务。13488846575   联系人:潘劲]]></subtitle>
  <id>http://www.is21.cn/</id> 
  <link rel="alternate" type="text/html" href="http://www.is21.cn/" /> 
  <link rel="self" type="application/atom+xml" href="http://www.is21.cn/atom.asp" /> 
  <generator uri="http://www.pjhome.net/" version="2.4.1022">PJBlog2</generator> 
  <updated>2007-08-21T00:19:44+08:00</updated> 

  <entry>
	  <title type="html"><![CDATA[Spring spring IOC ]]></title>
	  <author>
		 <name>admin</name>
		 <uri>http://www.is21.cn/</uri>
		 <email>lianxiangpanjin@163.com</email>
	  </author>
	  <category term="" scheme="http://www.is21.cn/default.asp?cateID=5" label="spring" /> 
	  <updated>2007-08-21T00:19:44+08:00</updated>
	  <published>2007-08-21T00:19:44+08:00</published>
		  <summary type="html"><![CDATA[ioc:一种依赖性注入，即用配置文件控制bean]]></summary>
	  <link rel="alternate" type="text/html" href="http://www.is21.cn/default.asp?id=35" /> 
	  <id>http://www.is21.cn/default.asp?id=35</id> 
  </entry>	
		
  <entry>
	  <title type="html"><![CDATA[数据工厂与ioc区别(简单实例，能运行)]]></title>
	  <author>
		 <name>admin</name>
		 <uri>http://www.is21.cn/</uri>
		 <email>lianxiangpanjin@163.com</email>
	  </author>
	  <category term="" scheme="http://www.is21.cn/default.asp?cateID=5" label="spring" /> 
	  <updated>2007-08-12T02:22:00+08:00</updated>
	  <published>2007-08-12T02:22:00+08:00</published>
		  <summary type="html"><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Spring的模块化是很强的，各个功能模块都是独立的，我们可以选择的使用。这一章先从Spring的IoC开始。所谓IoC就是一个用XML来定义生成对象的模式，我们看看如果来使用的。<br/><br/><strong>数据模型</strong><br/><br/><strong>1、如下图所示有三个类，Human（人类）是接口，Chinese（中国人）是一个子类，American（美国人）是另外一个子类。</strong>&nbsp;<br/>源代码如下：<br/>package&nbsp;cn.com.chengang.spring;<br/>public&nbsp;interface&nbsp;Human&nbsp;{<br/>void&nbsp;eat();<br/>void&nbsp;walk();<br/>}<br/><br/>package&nbsp;cn.com.chengang.spring;<br/>public&nbsp;class&nbsp;Chinese&nbsp;implements&nbsp;Human&nbsp;{<br/>/*&nbsp;（非&nbsp;Javadoc）<br/>*&nbsp;@see&nbsp;cn.com.chengang.spring.Human#eat()<br/>*/<br/>public&nbsp;void&nbsp;eat()&nbsp;{<br/>System.out.println(&#34;中国人对吃很有一套&#34;);<br/>}<br/><br/>/*&nbsp;（非&nbsp;Javadoc）<br/>*&nbsp;@see&nbsp;cn.com.chengang.spring.Human#walk()<br/>*/<br/>public&nbsp;void&nbsp;walk()&nbsp;{<br/>System.out.println(&#34;中国人行如飞&#34;);<br/>}<br/>}<br/><br/>package&nbsp;cn.com.chengang.spring;<br/>public&nbsp;class&nbsp;American&nbsp;implements&nbsp;Human&nbsp;{<br/>/*&nbsp;（非&nbsp;Javadoc）<br/>*&nbsp;@see&nbsp;cn.com.chengang.spring.Human#eat()<br/>*/<br/>public&nbsp;void&nbsp;eat()&nbsp;{<br/>System.out.println(&#34;美国人主要以面包为主&#34;);<br/>}<br/><br/>/*&nbsp;（非&nbsp;Javadoc）<br/>*&nbsp;@see&nbsp;cn.com.chengang.spring.Human#walk()<br/>*/<br/>public&nbsp;void&nbsp;walk()&nbsp;{<br/>System.out.println(&#34;美国人以车代步，有四肢退化的趋势&#34;);<br/>}<br/>}&nbsp;<br/><br/><br/><strong>2、对以上对象采用工厂模式的用法如下</strong><br/>创建一个工厂类Factory，如下。这个工厂类里定义了两个字符串常量，所标识不同的人种。getHuman方法根据传入参数的字串，来判断要生成什么样的人种。<br/><br/>package&nbsp;cn.com.chengang.spring;<br/>public&nbsp;class&nbsp;Factory&nbsp;{<br/>public&nbsp;final&nbsp;static&nbsp;String&nbsp;CHINESE&nbsp;=&nbsp;&#34;Chinese&#34;;<br/>public&nbsp;final&nbsp;static&nbsp;String&nbsp;AMERICAN&nbsp;=&nbsp;&#34;American&#34;;<br/><br/>public&nbsp;Human&nbsp;getHuman(String&nbsp;ethnic)&nbsp;{<br/>if&nbsp;(ethnic.equals(CHINESE))<br/>return&nbsp;new&nbsp;Chinese();<br/>else&nbsp;if&nbsp;(ethnic.equals(AMERICAN))<br/>return&nbsp;new&nbsp;American();<br/>else<br/>throw&nbsp;new&nbsp;IllegalArgumentException(&#34;参数(人种)错误&#34;);<br/>}<br/>}&nbsp;<br/>&nbsp;<br/>下面是一个测试的程序，使用工厂方法来得到了不同的“人种对象”，并执行相应的方法。<br/>package&nbsp;cn.com.chengang.spring;<br/>public&nbsp;class&nbsp;ClientTest&nbsp;{<br/>public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;{<br/>Human&nbsp;human&nbsp;=&nbsp;null;<br/>human&nbsp;=&nbsp;new&nbsp;Factory().getHuman(Factory.CHINESE);<br/>human.eat();<br/>human.walk();<br/>human&nbsp;=&nbsp;new&nbsp;Factory().getHuman(Factory.AMERICAN);<br/>human.eat();<br/>human.walk();<br/>}<br/>}&nbsp;<br/><br/><strong>3、采用Spring的IoC的用法如下：</strong><br/>在项目根目录下创建一个bean.xml文件<br/><br/>＜?xml&nbsp;version=&#34;1.0&#34;&nbsp;encoding=&#34;UTF-8&#34;?＞<br/>＜!DOCTYPE&nbsp;beans&nbsp;PUBLIC&nbsp;&#34;-//SPRING//DTD&nbsp;BEAN//EN&#34;&nbsp;&#34;<a href="http://www.springframework.org/dtd/spring-beans.dtd" target="_blank">http://www.springframework.org/dtd/spring-beans.dtd</a>&#34;＞<br/>＜beans＞<br/>＜bean&nbsp;id=&#34;Chinese&#34;&nbsp;class=&#34;cn.com.chengang.spring.Chinese&#34;/＞<br/>＜bean&nbsp;id=&#34;American&#34;&nbsp;class=&#34;cn.com.chengang.spring.American&#34;/＞<br/>＜/beans＞&nbsp;<br/><br/>bean.xml的位置如下图，注意不要看花眼把它看成是lib目录下的了，它是在myspring目录下的。<br/><br/>修改ClientTest程序如下：<br/>package&nbsp;cn.com.chengang.spring;<br/>import&nbsp;o&#114;g.springframework.context.ApplicationContext;<br/>import&nbsp;o&#114;g.springframework.context.support.FileSystemXmlApplicationContext;<br/>public&nbsp;class&nbsp;ClientTest&nbsp;{<br/>public&nbsp;final&nbsp;static&nbsp;String&nbsp;CHINESE&nbsp;=&nbsp;&#34;Chinese&#34;;<br/>public&nbsp;final&nbsp;static&nbsp;String&nbsp;AMERICAN&nbsp;=&nbsp;&#34;American&#34;;<br/><br/>public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;{<br/>//&nbsp;Human&nbsp;human&nbsp;=&nbsp;null;<br/>//&nbsp;human&nbsp;=&nbsp;new&nbsp;Factory().getHuman(Factory.CHINESE);<br/>//&nbsp;human.eat();<br/>//&nbsp;human.walk();<br/>//&nbsp;human&nbsp;=&nbsp;new&nbsp;Factory().getHuman(Factory.AMERICAN);<br/>//&nbsp;human.eat();<br/>//&nbsp;human.walk();<br/><br/>ApplicationContext&nbsp;ctx&nbsp;=&nbsp;new&nbsp;FileSystemXmlApplicationContext(&#34;bean.xml&#34;);<br/>Human&nbsp;human&nbsp;=&nbsp;null;<br/>human&nbsp;=&nbsp;(Human)&nbsp;ctx.getBean(CHINESE);<br/>human.eat();<br/>human.walk();<br/>human&nbsp;=&nbsp;(Human)&nbsp;ctx.getBean(AMERICAN);<br/>human.eat();<br/>human.walk();<br/>}<br/>}&nbsp;<br/>从这个程序可以看到，ctx就相当于原来的Factory工厂，原来的Factory就可以删除掉了。然后又把Factory里的两个常量移到了ClientTest类里，整个程序结构基本一样。<br/><br/>再回头看原来的bean.xml文件的这一句：<br/><br/>＜bean&nbsp;id=&#34;Chinese&#34;&nbsp;class=&#34;cn.com.chengang.spring.Chinese&#34;/＞&nbsp;<br/>id就是ctx.getBean的参数值，一个字符串。class就是一个类（包名＋类名）。然后在ClientTest类里获得Chinese对象就是这么一句<br/><br/>human&nbsp;=&nbsp;(Human)&nbsp;ctx.getBean(CHINESE);&nbsp;&nbsp;<br/><br/>因为getBean方法返回的是Object类型，所以前面要加一个类型转换。<br/><br/><br/><br/><strong>总结</strong><br/><br/>（1）也许有人说，IoC和工厂模式不是一样的作用吗，用IoC好象还麻烦一点。<br/><br/>举个例子，如果用户需求发生变化，要把Chinese类修改一下。那么前一种工厂模式，就要更改Factory类的方法，并且重新编译布署。而IoC只需要将class属性改变一下，并且由于IoC利用了Java反射机制，这些对象是动态生成的，这时我们就可以热插拨Chinese对象（不必把原程序停止下来重新编译布署）<br/><br/>（2）也许有人说，即然IoC这么好，那么我把系统所有对象都用IoC方式来生成。<br/><br/>注意，IoC的灵活性是有代价的：设置步骤麻烦、生成对象的方式不直观、反射比正常生成对象在效率上慢一点。因此使用IoC要看有没有必要，我认为比较通用的判断方式是：用到工厂模式的地方都可以考虑用IoC模式。<br/><br/>（3）在上面的IoC的方式里，还有一些可以变化的地方。比如，bean.xml不一定要放在项目录下，也可以放在其他地方，比如cn.com.chengang.spring包里。不过在使用时也要变化一下，如下所示：<br/><br/>new&nbsp;FileSystemXmlApplicationContext(&#34;src/cn/com/chengang/spring/bean.xml&#34;);&nbsp;&nbsp;<br/><br/>另外，bean.xml也可以改成其他名字。这样我们在系统中就可以分门别类的设置不同的bean.xml。<br/><br/>（4）关于IoC的低侵入性。<br/><br/>什么是低侵入性？如果你用过Struts或EJB就会发现，要继承一些接口或类，才能利用它们的框架开发。这样，系统就被绑定在Struts、EJB上了，对系统的可移植性产生不利的影响。如果代码中很少涉及某一个框架的代码，那么这个框架就可以称做是一个低侵入性的框架。<br/><br/>Spring的侵入性很低，Humen.java、Chinese.java等几个类都不必继承什么接口或类。但在ClientTest里还是有一些Spring的影子：FileSystemXmlApplicationContext类和ctx.getBean方式等。<br/>现在，低侵入性似乎也成了判定一个框架的实现技术好坏的标准之一。<br/><br/>（5）关于bean.xml的用法<br/><br/>bean.xml的用法还有很多，其中内容是相当丰富的。假设Chinese类里有一个humenName属性（姓名），那么原的bean.xml修改如下。此后生成Chinese对象时，“陈刚”这个值将自动设置到Chinese类的humenName属性中。而且由于singleton为true这时生成Chinese对象将采用单例模式，系统仅存在一个Chinese对象实例。<br/><br/>＜?xml&nbsp;version=&#34;1.0&#34;&nbsp;encoding=&#34;UTF-8&#34;?＞<br/>＜!DOCTYPE&nbsp;beans&nbsp;PUBLIC&nbsp;&#34;-//SPRING//DTD&nbsp;BEAN//EN&#34;&nbsp;&#34;<a href="http://www.springframework.org/dtd/spring-beans.dtd" target="_blank">http://www.springframework.org/dtd/spring-beans.dtd</a>&#34;＞<br/>＜beans＞<br/>＜bean&nbsp;id=&#34;Chinese&#34;&nbsp;class=&#34;cn.com.chengang.spring.Chinese&#34;&nbsp;singleton=&#34;true&#34;＞<br/>＜property&nbsp;name=&#34;humenName&#34;＞<br/>＜value＞陈刚＜/value＞<br/>＜/property＞<br/>＜/bean＞<br/>＜bean&nbsp;id=&#34;American&#34;&nbsp;class=&#34;cn.com.chengang.spring.American&#34;/＞<br/>＜/beans＞&nbsp;<br/><br/>关于bean.xml的其它用法，不再详细介绍了，大家自己拿Spring的文档一看就明白了。<br/><br/><br/>数据工厂例子:<br/><img src="http://www.is21.cn/images/download.gif" alt="下载文件" style="margin:0px 2px -4px 0px"/> <a href="http://www.is21.cn/attachments/month_0708/s200781223353.rar" target="_blank">点击下载此文件</a><br/><br/>ioc:(spring例子)<br/><img src="http://www.is21.cn/images/download.gif" alt="下载文件" style="margin:0px 2px -4px 0px"/> <a href="http://www.is21.cn/attachments/month_0708/r200781223431.rar" target="_blank">点击下载此文件</a><br/>]]></summary>
	  <link rel="alternate" type="text/html" href="http://www.is21.cn/default.asp?id=12" /> 
	  <id>http://www.is21.cn/default.asp?id=12</id> 
  </entry>	
		
  <entry>
	  <title type="html"><![CDATA[Spring 简单例子(未运行)]]></title>
	  <author>
		 <name>admin</name>
		 <uri>http://www.is21.cn/</uri>
		 <email>lianxiangpanjin@163.com</email>
	  </author>
	  <category term="" scheme="http://www.is21.cn/default.asp?cateID=5" label="spring" /> 
	  <updated>2007-08-11T08:56:21+08:00</updated>
	  <published>2007-08-11T08:56:21+08:00</published>
		  <summary type="html"><![CDATA[开发环境使用的是&nbsp;Eclipse3.0&nbsp;+&nbsp;myEclipse3.8.4。<br/>其实很简单创先建一个&nbsp;Java&nbsp;Project&nbsp;。&nbsp;<br/>1.&nbsp;向工程添加所要使用的类包：Spring.jar,&nbsp;junit.jar,&nbsp;commons-logging.jar,&nbsp;log4j-1.2.8.jar。<br/><br/>2.&nbsp;在&nbsp;src&nbsp;下添加一个接口：<br/>package&nbsp;com.ivan.springtest;<br/>public&nbsp;interface&nbsp;Action&nbsp;{<br/>&nbsp;public&nbsp;String&nbsp;execute(String&nbsp;str);<br/>}<br/><br/>3.&nbsp;添加两个继承此接口的类：<br/>package&nbsp;com.ivan.springtest;<br/>public&nbsp;class&nbsp;ByeAction&nbsp;implements&nbsp;Action&nbsp;{<br/>&nbsp;public&nbsp;String&nbsp;execute(String&nbsp;str)&nbsp;{<br/>&nbsp;&nbsp;return&nbsp;&#34;Bye,&nbsp;&#34;&nbsp;+&nbsp;str;<br/>&nbsp;}<br/>}<br/>package&nbsp;com.ivan.springtest;<br/>public&nbsp;class&nbsp;HelloAction&nbsp;implements&nbsp;Action&nbsp;{<br/>&nbsp;public&nbsp;String&nbsp;execute(String&nbsp;str)&nbsp;{<br/>&nbsp;&nbsp;return&nbsp;&#34;Hello,&nbsp;&#34;&nbsp;+&nbsp;str;<br/>&nbsp;}<br/>}<br/><br/>4.&nbsp;编写&nbsp;Spring&nbsp;的配置文件&nbsp;bean.xml，放置在项目下&nbsp;:<br/>&lt;?xml&nbsp;version=&#34;1.0&#34;&nbsp;encoding=&#34;UTF-8&#34;?&gt;<br/>&lt;!DOCTYPE&nbsp;beans&nbsp;PUBLIC&nbsp;&#34;-//SPRING//DTD&nbsp;BEAN//EN&#34;&nbsp;&#34;<a href="http://www.springframework.org/dtd/spring-beans.dtd" target="_blank">http://www.springframework.org/dtd/spring-beans.dtd</a>&#34;&gt;<br/>&lt;beans&gt;<br/>&nbsp;&lt;description&gt;Spring&nbsp;Quick&nbsp;Start&lt;/description&gt;<br/>&nbsp;&lt;bean&nbsp;id=&#34;TheAction&#34;<br/>&nbsp;&nbsp;class=&#34;com.ivan.springtest.HelloAction&#34;&gt;<br/>&nbsp;&lt;/bean&gt;<br/>&lt;/beans&gt;<br/><br/>5.&nbsp;编写&nbsp;log&nbsp;设定文件，放置在&nbsp;src&nbsp;下，log4j.properties:<br/>log4j.rootLogger=DEBUG,&nbsp;stdout<br/>log4j.appender.stdout=org.apache.log4j.ConsoleAppender<br/>log4j.appender.stdout.layout=org.apache.log4j.PatternLayout<br/>log4j.appender.stdout.layout.ConversionPattern=%c{1}&nbsp;-&nbsp;%m%n<br/><br/>6.&nbsp;编写测试用例：<br/>package&nbsp;test;<br/>import&nbsp;o&#114;g.springframework.context.ApplicationContext;<br/>import&nbsp;o&#114;g.springframework.context.support.FileSystemXmlApplicationContext;<br/>import&nbsp;com.ivan.springtest.Action;<br/>import&nbsp;junit.framework.TestCase;<br/>public&nbsp;class&nbsp;SpringTest&nbsp;extends&nbsp;TestCase&nbsp;{<br/>&nbsp;public&nbsp;void&nbsp;testQuickStart()&nbsp;{<br/>&nbsp;&nbsp;ApplicationContext&nbsp;ctx=new&nbsp;FileSystemXmlApplicationContext(&#34;bean.xml&#34;);<br/>&nbsp;&nbsp;Action&nbsp;action&nbsp;=&nbsp;(Action)&nbsp;ctx.getBean(&#34;TheAction&#34;);<br/>&nbsp;&nbsp;System.out.println(action.execute(&#34;Rod&nbsp;Johnson&#34;));<br/>&nbsp;}<br/>}<br/><br/><br/>]]></summary>
	  <link rel="alternate" type="text/html" href="http://www.is21.cn/default.asp?id=9" /> 
	  <id>http://www.is21.cn/default.asp?id=9</id> 
  </entry>	
		
</feed>