<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>涂0实验室 &#187; java</title>
	<atom:link href="http://www.sulong.info/archives/tag/java/feed" rel="self" type="application/rss+xml" />
	<link>http://www.sulong.info</link>
	<description>一个程序员的成长之路</description>
	<lastBuildDate>Fri, 27 Aug 2010 01:54:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>静态域的作用范围</title>
		<link>http://www.sulong.info/archives/443</link>
		<comments>http://www.sulong.info/archives/443#comments</comments>
		<pubDate>Wed, 25 Aug 2010 09:35:54 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[程序]]></category>
		<category><![CDATA[classloader]]></category>

		<guid isPermaLink="false">http://www.sulong.info/?p=443</guid>
		<description><![CDATA[静态域的作用范围是在同一个类之内，类不仅有名称上的区别，还有classloader，即类加载器的问题，同一个class文件被不同的类加载器加载后，即便它们有相同的名称，但是它们并不是同一个类。因此在不同类加载器中同名类的静态变量是不共享的。下面做一个测试，来验证它。下面的Test类有一个静态域value，两个方法分别增加value的值和打印状态。 public class Test &#123; &#160; &#160; private static int value = 1; &#160; &#160; &#160; &#160; public void increase&#40;&#41; &#123; &#160; &#160; &#160; &#160; value++; &#160; &#160; &#125; &#160; &#160; &#160; &#160; public void print&#40;&#41; &#123; &#160; &#160; &#160; &#160; System.out.println&#40;&#34;My Class Loader: &#34; &#160; &#160; &#160; &#160; &#160; &#160; + this.getClass&#40;&#41;.getClassLoader&#40;&#41; + &#160;&#34;, [...]]]></description>
			<content:encoded><![CDATA[<p>静态域的作用范围是在同一个类之内，类不仅有名称上的区别，还有classloader，即类加载器的问题，同一个class文件被不同的类加载器加载后，即便它们有相同的名称，但是它们并不是同一个类。因此在<strong>不同类加载器中同名类的静态变量是不共享的</strong>。下面做一个测试，来验证它。下面的Test类有一个静态域value，两个方法分别增加value的值和打印状态。</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> value <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> increase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; value<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> print<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;My Class Loader: &quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getClassLoader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> &nbsp;<span style="color: #0000ff;">&quot;, Value: &quot;</span> <span style="color: #339933;">+</span> value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>下面的类StaticTest，运行时创建两个类加载器，分别载入同一个class文件，并通过反射依次调用print, increase,print方法。如果静态域在整个JVM内共享，那么value被加了两次，最后会输出3；否则应该输出2。由于类加载器会先向父类加载器请求类，找不到的时候才会自己加载，所以在运行这个测试时，一定不要把Test类先放到classpath中。</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.Method</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URL</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URLClassLoader</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StaticTest <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurl+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">URL</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> classURLs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurl+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">URL</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurl+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">URL</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;file:/home/sulong/tmp/&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurlclassloader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">URLClassLoader</span></a> classLoader1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurlclassloader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">URLClassLoader</span></a><span style="color: #009900;">&#40;</span>classURLs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurlclassloader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">URLClassLoader</span></a> classLoader2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurlclassloader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">URLClassLoader</span></a><span style="color: #009900;">&#40;</span>classURLs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; test<span style="color: #009900;">&#40;</span>classLoader1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;--------------------&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; test<span style="color: #009900;">&#40;</span>classLoader2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> test<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aclassloader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">ClassLoader</span></a> loader<span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">Class</span> clazz <span style="color: #339933;">=</span> loader.<span style="color: #006633;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> testObj <span style="color: #339933;">=</span> clazz.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; invoke<span style="color: #009900;">&#40;</span>clazz, testObj, <span style="color: #0000ff;">&quot;print&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; invoke<span style="color: #009900;">&#40;</span>clazz, testObj, <span style="color: #0000ff;">&quot;increase&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; invoke<span style="color: #009900;">&#40;</span>clazz, testObj, <span style="color: #0000ff;">&quot;print&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> invoke<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> clazz, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> obj, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name<span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amethod+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Method</span></a> method1 <span style="color: #339933;">=</span> clazz.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span>name, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; method1.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>obj, <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>下面是我的机器上运行时的测试结果：</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">My Class Loader: java.net.URLClassLoader@7987aeca, Value: 1<br />
My Class Loader: java.net.URLClassLoader@7987aeca, Value: 2<br />
--------------------<br />
My Class Loader: java.net.URLClassLoader@5d0385c1, Value: 1<br />
My Class Loader: java.net.URLClassLoader@5d0385c1, Value: 2</div></div>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/443/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>让JAXB生成序列化的类</title>
		<link>http://www.sulong.info/archives/332</link>
		<comments>http://www.sulong.info/archives/332#comments</comments>
		<pubDate>Fri, 20 Nov 2009 13:36:30 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[技巧]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jaxb]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.sulong.info/?p=332</guid>
		<description><![CDATA[默认jaxb生成的类是没有序列化的，但是我们经常需要他们序列化。jaxb ri有生成序列化的类的这样的功能，但由于这不属于标准的功能，所以需要手动的开启。开启的方法是在用来生成java类的xml schema文件头添加如下内容： &#60;annotation&#62; &#160; &#160; &#60;appinfo&#62; &#160; &#160; &#160; &#160; &#60;jaxb:globalBindings generateIsSetMethod=&#34;true&#34;&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;xjc:serializable uid=&#34;1&#34;/&#62; &#160; &#160; &#160; &#160; &#60;/jaxb:globalBindings&#62; &#160; &#160; &#60;/appinfo&#62; &#60;/annotation&#62; 另外，在使用jaxb的 xjc 编译器时，要加上 -extension 的参数。]]></description>
			<content:encoded><![CDATA[<p>默认jaxb生成的类是没有序列化的，但是我们经常需要他们序列化。jaxb ri有生成序列化的类的这样的功能，但由于这不属于标准的功能，所以需要手动的开启。开启的方法是在用来生成java类的xml schema文件头添加如下内容：</p>
<pre>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;annotation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appinfo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxb:globalBindings</span> <span style="color: #000066;">generateIsSetMethod</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xjc:serializable</span> <span style="color: #000066;">uid</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxb:globalBindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/appinfo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/annotation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
</pre>
<p>另外，在使用jaxb的 xjc 编译器时，要加上 -extension 的参数。</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/332/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>可恶的乱码问题</title>
		<link>http://www.sulong.info/archives/324</link>
		<comments>http://www.sulong.info/archives/324#comments</comments>
		<pubDate>Thu, 12 Nov 2009 14:41:40 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[技巧]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[乱码]]></category>

		<guid isPermaLink="false">http://www.sulong.info/?p=324</guid>
		<description><![CDATA[在打印单据的时候，有两个汉字“玥”和“芃”，始终显示为“？”，这一问题困扰了业务人员一段时间了。以前没有空，就没有投入精力去查原因，上次得了空，终于把这个问题解决掉了。 这个乱码的汉字从哪里来的呢？首先是业务人员通过一个遗留的PHP应用，输入到一个mysql 4数据库中，然后我们的java程序从这个mysql 4中读取，再写入到新的mysql5的数据库中，最后通过java程序生成html在用户的浏览器里显示，结果就看到了这两个汉字乱码。那么这个问题如何解决呢？ 只有两个汉字乱码，而不是所有的汉字都乱码，所以我们的程序一定使用了一种包含汉字较少的字符集，而这个出错的汉字恰好不在此字符集中，否则，就应当是大部分汉字乱码，而不仅仅是这两个了。很自然的就让人想到了 gb2312, gbk, gb18030, utf8等字符集之间的关系。其中gb2312中只有六千多个汉字，其它几个字符集支持的汉字数相差不多，我们程序一定是在该使用gbk, gb18030或utf8的地方使用了含有汉字较少的gb2312。 经查阅相关文档，果然发现那两个汉字都不在gb2312里，而在gbk, utf8字符集里。我们的mysql5使用的utf8字符集，生成的html也是utf8编码，如果mysql5中的汉字是正确的话，那么就应该能正常显示，因此，数据进入mysql5的时候就应该出错了。java程序在连接mysql5的时候，在jdbc连接中设置了utf8，所以如果从mysql4数据库中读取出来的是正确的，就不应该写入出问题，因此数据在mysql4中可能就错了，也可能是从mysql4中读取的时候出错了。php的兄弟们说mysql的数据库是gb2312字符集的，mysql的配置文件里也是这么配置的。如果果真如此，那么这个两个汉字在mysql4的数据库中就应该不正常了，所有的程序读出之后都应该是乱码，可是php的录入程序那边确实是能正常显示的。我断定这个数据库里放的实际上是gbk字符集的汉字。选用和mysql4相对应的mysql j connector 3.14，并在jdbc连接url里设置了characterEncoding=gbk，读出来的仍旧不正确！查阅了mysql j connector的文档，原来3 版本的mysql connector根本就不支持characterEncoding这个选项。看来只能修改mysql的配置文件了。修改之后，果然这两个汉字可以正常显示了。 如果计算机是中国人发明的，那么计算机也许一开始就得支持汉字，就再也没有乱码的问题。如果世界上只有一种汉字的编码方案，也不会有乱码的问题，可惜汉字偏偏有很多中编码方案。所以在我们的程序中，一定得搞清楚，我们的汉字数据来自于哪里，使用的哪种编码，每个环节都得配置得当，否则就会有乱码问题。 对于一般的web应用来说，需要关心编码的地方有： 1，数据库以什么编码存储数据 2，程序使用什么字符集连接数据库 3，程序内部运行时使用什么字符集 4，程序源码使用什么字符集 5，生成的html文件是什么字符集 6，程序告知浏览器的字符集 如果以上这些地方的字符集有配置不正确的地方，往往就会乱码。 如果可以的话，最好在所有的地方都使用一种编码，比如对于java应用来说，在所有的地方都使用utf8编码，就可以解决问题。需要注意的是，在windows环境下，默认的文件编码是gb18030的，而不是utf8。 另外，极端鄙视故意使用生僻字火星文的脑残族！！]]></description>
			<content:encoded><![CDATA[<p>在打印单据的时候，有两个汉字“玥”和“芃”，始终显示为“？”，这一问题困扰了业务人员一段时间了。以前没有空，就没有投入精力去查原因，上次得了空，终于把这个问题解决掉了。</p>
<p>这个乱码的汉字从哪里来的呢？首先是业务人员通过一个遗留的PHP应用，输入到一个mysql 4数据库中，然后我们的java程序从这个mysql 4中读取，再写入到新的mysql5的数据库中，最后通过java程序生成html在用户的浏览器里显示，结果就看到了这两个汉字乱码。那么这个问题如何解决呢？</p>
<p>只有两个汉字乱码，而不是所有的汉字都乱码，所以我们的程序一定使用了一种包含汉字较少的字符集，而这个出错的汉字恰好不在此字符集中，否则，就应当是大部分汉字乱码，而不仅仅是这两个了。很自然的就让人想到了 gb2312, gbk, gb18030, utf8等字符集之间的关系。其中gb2312中只有六千多个汉字，其它几个字符集支持的汉字数相差不多，我们程序一定是在该使用gbk, gb18030或utf8的地方使用了含有汉字较少的gb2312。</p>
<p>经查阅相关文档，果然发现那两个汉字都不在gb2312里，而在gbk, utf8字符集里。我们的mysql5使用的utf8字符集，生成的html也是utf8编码，如果mysql5中的汉字是正确的话，那么就应该能正常显示，因此，数据进入mysql5的时候就应该出错了。java程序在连接mysql5的时候，在jdbc连接中设置了utf8，所以如果从mysql4数据库中读取出来的是正确的，就不应该写入出问题，因此数据在mysql4中可能就错了，也可能是从mysql4中读取的时候出错了。php的兄弟们说mysql的数据库是gb2312字符集的，mysql的配置文件里也是这么配置的。如果果真如此，那么这个两个汉字在mysql4的数据库中就应该不正常了，所有的程序读出之后都应该是乱码，可是php的录入程序那边确实是能正常显示的。我断定这个数据库里放的实际上是gbk字符集的汉字。选用和mysql4相对应的mysql j connector 3.14，并在jdbc连接url里设置了characterEncoding=gbk，读出来的仍旧不正确！查阅了mysql j connector的文档，原来3 版本的mysql connector根本就不支持characterEncoding这个选项。看来只能修改mysql的配置文件了。修改之后，果然这两个汉字可以正常显示了。</p>
<p>如果计算机是中国人发明的，那么计算机也许一开始就得支持汉字，就再也没有乱码的问题。如果世界上只有一种汉字的编码方案，也不会有乱码的问题，可惜汉字偏偏有很多中编码方案。所以在我们的程序中，一定得搞清楚，我们的汉字数据来自于哪里，使用的哪种编码，每个环节都得配置得当，否则就会有乱码问题。</p>
<p>对于一般的web应用来说，需要关心编码的地方有：<br />
1，数据库以什么编码存储数据<br />
2，程序使用什么字符集连接数据库<br />
3，程序内部运行时使用什么字符集<br />
4，程序源码使用什么字符集<br />
5，生成的html文件是什么字符集<br />
6，程序告知浏览器的字符集<br />
如果以上这些地方的字符集有配置不正确的地方，往往就会乱码。</p>
<p>如果可以的话，最好在所有的地方都使用一种编码，比如对于java应用来说，在所有的地方都使用utf8编码，就可以解决问题。需要注意的是，在windows环境下，默认的文件编码是gb18030的，而不是utf8。</p>
<p>另外，极端鄙视故意使用生僻字火星文的脑残族！！</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/324/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>漫谈PHP和Java</title>
		<link>http://www.sulong.info/archives/321</link>
		<comments>http://www.sulong.info/archives/321#comments</comments>
		<pubDate>Sat, 26 Sep 2009 16:31:51 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[程序]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[思考]]></category>
		<category><![CDATA[经验]]></category>

		<guid isPermaLink="false">http://www.sulong.info/?p=321</guid>
		<description><![CDATA[我为公司内部交流而写的PPT，谈论PHP和JAVA相关的话题。在这里查看： 漫谈PHP和Java]]></description>
			<content:encoded><![CDATA[<p>我为公司内部交流而写的PPT，谈论PHP和JAVA相关的话题。在这里查看：</p>
<p><a href="http://docs.google.com/present/view?id=dc6zf3d4_83gf7b77c4">漫谈PHP和Java</a><br/></p>
<p><iframe src="http://docs.google.com/present/embed?id=dc6zf3d4_83gf7b77c4&#038;size=m" frameborder="0" width="555" height="451"></iframe></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/321/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>ActiveMQ使用经验</title>
		<link>http://www.sulong.info/archives/207</link>
		<comments>http://www.sulong.info/archives/207#comments</comments>
		<pubDate>Thu, 18 Jun 2009 09:56:41 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[经验]]></category>
		<category><![CDATA[jms]]></category>

		<guid isPermaLink="false">http://www.sulong.info/?p=207</guid>
		<description><![CDATA[ActiveMQ是apache的一个开源JMS服务器，不仅具备标准JMS的功能，还有很多额外的功能。公司里引入ActiveMQ后，ActiveMQ成里我们公司业务系统中最重要的一个环节。所有应用都通过jms集成，如果ActiveMQ出了故障，整个系统就瘫痪了。因此，头对ActiveMQ的性能，可靠性，以及如何正确使用，是非常的关心的，而我就被指派来做关于ActiveMQ的调研，本文对此做了些总结。 1 使用jms需要注意的问题 一下所述的问题，不仅是对ActiveMQ，对于其他的JMS也一样有效。 1.1 不要频繁的建立和关闭连接 JMS使用长连接方式，一个程序，只要和JMS服务器保持一个连接就可以了，不要频繁的建立和关闭连接。频繁的建立和关闭连接，对程序的性能影响还是很大的。这一点和jdbc还是不太一样的。 1.2 Connection的start()和stop()方法代价很高 JMS的Connection的start()和stop()方法代价很高，不能经常调用。我们试用的时候，写了个jms的connection pool，每次将connection取出pool时调用start()方法，归还时调用stop()方法，然而后来用jprofiler发现，一般的cpu时间都耗在了这两个方法上。 1.3 start()后才能收消息 Connection的start()方法调用后，才能收到jms消息。如果不调用这个方法，能发出消息，但是一直收不到消息。不知道其它的jms服务器也是这样。 1.4 显示关闭Session 如果忘记了最后关闭Connection或Session对象，都会导致内存泄漏。这个在我测试的时候也发现了。本来以为关闭了Connection，由这个Connection生成的Session也会被自动关闭，结果并非如此，Session并没有关闭，导致内存泄漏。所以一定要显示的关闭Connection和Session。 1.5 对Session做对象池 对Session做对象池，而不是Connection。Session也是昂贵的对象，每次使用都新建和关闭，代价也非常高。而且后来我们发现，原来Connection是线程安全的，而Session不是，所以后来改成了对Session做对象池，而只保留一个Connection。 2 集群 ActiveMQ有强大而灵活的集群功能，但是使用起来还是会有很多陷阱。 2.1 broker cluster和 master-slave ActiveMQ可以做broker的集群，也可以做master-slave方式的集群。前者能在多个broker之前fail-over和load-balance，但是在某个节点出故障时，可能导致消息丢失；而后者能实时备份消息，和fail-over，但是不能load-balance。broker cluser的方式，在一个broker上发送的消息可以在其它的broker上收到。当一个broker失效时，客户端可以自动的转到别的broker上运行，多个broker可以同时提供服务，但是消息只存储在一个broker上，如果那个broker失效了，那么客户端直到它重新启动后才能收到该broker上的消息，假如很不幸，那个broker的存储介质坏了，那么消息就丢失掉了。 Master-slave方式中，只有master提供服务，slave只是实时的备份master的数据，所以消息不会丢失。当master失效时，slave会自动升为master，客户端会自动转到slave上工作，所以能fail-over。由于只有master提供服务，所以不能将负载分到多个broker上。 其实单个broker的性能已经是相当的惊人了，在我们公司的机器上能达到每秒收发4000个消息，没个消息4K字节这样的速度，足够公司目前的需要了，而公司并不希望丢失任何数据，所以我们选择使用master-slave模式。 2.2 多种master-slave模式 master-slave也有多种实现方式。它们的不同只是在共享数据和锁机制上。 2.2.1 Pure master-slave Pure master-slave，显示的在配置文件中指定一个broker做为另一个broker的slave。运行时，slave同过网络自动从master出复制数据，同时在和master失去连接时自动升级为master。当master失效，slave成为master后，如果要让原先的master重新投入运行，需要停掉运行中的slave(现在升级为master了)，手动复制slave中的数据到master中。再重新启动master和slave。这种方式最简单，效率也不错，但是只能有两台做集群，只能fail-over一次，而且需要停机回复master-slave结构。 2.2.2 JDBC master-slave 这种方式不需要特殊的配置，只要让所有的节点都把数据存储到同一个数据库中。先拿到数据库表的锁的节点成为master，一旦它失效了，其它的节点获得锁，就可以成为master。因为数据通过数据库共享，放在一个地方，不需要停机恢复master-slave。这种方式，需要额外的数据库服务器，如果数据库失效了，那么就全失效了，而且速度不是很快。我们在用mysql测试时，并没有成功，master失效后，其他的节点始终没有升级成slave，可能是数据库配置的问题。 2.2.3 Share file master-slave 这种方式类似于前者，也不需要特别的配置，只是通过共享文件系统来共享数据，靠文件锁实现只有一台成为master。共享文件系统的方式有很多，我们测试了nfs v4 (v3有bug，不行)， 最终在稳定性，效率等方面不是很满意，可能是通过网络太慢了。 测试过众多master-slave模式后发现，pure方式管理起来麻烦，jdbc方式成本高，效率低，share file方式需要高性能的共享文件，都有缺点。鉴于单台activeMQ很可靠，而我们的基础平台组愿意用硬件备份，最终还是决定不用master-slave了，也不用broker cluster，就用单台，通过硬件冗余保证数据不会丢失，并找另外一台刀片机做冷备，在主服务器失效时顶替。]]></description>
			<content:encoded><![CDATA[<p>ActiveMQ是apache的一个开源JMS服务器，不仅具备标准JMS的功能，还有很多额外的功能。公司里引入ActiveMQ后，ActiveMQ成里我们公司业务系统中最重要的一个环节。所有应用都通过jms集成，如果ActiveMQ出了故障，整个系统就瘫痪了。因此，头对ActiveMQ的性能，可靠性，以及如何正确使用，是非常的关心的，而我就被指派来做关于ActiveMQ的调研，本文对此做了些总结。</p>
<h2>1 使用jms需要注意的问题</h2>
<p>一下所述的问题，不仅是对ActiveMQ，对于其他的JMS也一样有效。</p>
<h3>1.1 不要频繁的建立和关闭连接</h3>
<p>JMS使用长连接方式，一个程序，只要和JMS服务器保持一个连接就可以了，不要频繁的建立和关闭连接。频繁的建立和关闭连接，对程序的性能影响还是很大的。这一点和jdbc还是不太一样的。</p>
<h3>1.2 Connection的start()和stop()方法代价很高</h3>
<p>JMS的Connection的start()和stop()方法代价很高，不能经常调用。我们试用的时候，写了个jms的connection pool，每次将connection取出pool时调用start()方法，归还时调用stop()方法，然而后来用jprofiler发现，一般的cpu时间都耗在了这两个方法上。</p>
<h3>1.3 start()后才能收消息</h3>
<p>Connection的start()方法调用后，才能收到jms消息。如果不调用这个方法，能发出消息，但是一直收不到消息。不知道其它的jms服务器也是这样。</p>
<h3>1.4 显示关闭Session</h3>
<p>如果忘记了最后关闭Connection或Session对象，都会导致内存泄漏。这个在我测试的时候也发现了。本来以为关闭了Connection，由这个Connection生成的Session也会被自动关闭，结果并非如此，Session并没有关闭，导致内存泄漏。所以一定要显示的关闭Connection和Session。</p>
<h3>1.5 对Session做对象池</h3>
<p>对Session做对象池，而不是Connection。Session也是昂贵的对象，每次使用都新建和关闭，代价也非常高。而且后来我们发现，原来Connection是线程安全的，而Session不是，所以后来改成了对Session做对象池，而只保留一个Connection。</p>
<h2>2 集群</h2>
<p>ActiveMQ有强大而灵活的集群功能，但是使用起来还是会有很多陷阱。</p>
<h3>2.1 broker cluster和 master-slave</h3>
<p>ActiveMQ可以做broker的集群，也可以做master-slave方式的集群。前者能在多个broker之前fail-over和load-balance，但是在某个节点出故障时，可能导致消息丢失；而后者能实时备份消息，和fail-over，但是不能load-balance。broker cluser的方式，在一个broker上发送的消息可以在其它的broker上收到。当一个broker失效时，客户端可以自动的转到别的broker上运行，多个broker可以同时提供服务，但是消息只存储在一个broker上，如果那个broker失效了，那么客户端直到它重新启动后才能收到该broker上的消息，假如很不幸，那个broker的存储介质坏了，那么消息就丢失掉了。<br />
Master-slave方式中，只有master提供服务，slave只是实时的备份master的数据，所以消息不会丢失。当master失效时，slave会自动升为master，客户端会自动转到slave上工作，所以能fail-over。由于只有master提供服务，所以不能将负载分到多个broker上。<br />
其实单个broker的性能已经是相当的惊人了，在我们公司的机器上能达到每秒收发4000个消息，没个消息4K字节这样的速度，足够公司目前的需要了，而公司并不希望丢失任何数据，所以我们选择使用master-slave模式。</p>
<h3>2.2 多种master-slave模式</h3>
<p>master-slave也有多种实现方式。它们的不同只是在共享数据和锁机制上。</p>
<h4>2.2.1 Pure master-slave</h4>
<p>Pure master-slave，显示的在配置文件中指定一个broker做为另一个broker的slave。运行时，slave同过网络自动从master出复制数据，同时在和master失去连接时自动升级为master。当master失效，slave成为master后，如果要让原先的master重新投入运行，需要停掉运行中的slave(现在升级为master了)，手动复制slave中的数据到master中。再重新启动master和slave。这种方式最简单，效率也不错，但是只能有两台做集群，只能fail-over一次，而且需要停机回复master-slave结构。</p>
<h4>2.2.2 JDBC master-slave</h4>
<p>这种方式不需要特殊的配置，只要让所有的节点都把数据存储到同一个数据库中。先拿到数据库表的锁的节点成为master，一旦它失效了，其它的节点获得锁，就可以成为master。因为数据通过数据库共享，放在一个地方，不需要停机恢复master-slave。这种方式，需要额外的数据库服务器，如果数据库失效了，那么就全失效了，而且速度不是很快。我们在用mysql测试时，并没有成功，master失效后，其他的节点始终没有升级成slave，可能是数据库配置的问题。</p>
<h4>2.2.3 Share file master-slave</h4>
<p>这种方式类似于前者，也不需要特别的配置，只是通过共享文件系统来共享数据，靠文件锁实现只有一台成为master。共享文件系统的方式有很多，我们测试了nfs v4 (v3有bug，不行)， 最终在稳定性，效率等方面不是很满意，可能是通过网络太慢了。</p>
<p>测试过众多master-slave模式后发现，pure方式管理起来麻烦，jdbc方式成本高，效率低，share file方式需要高性能的共享文件，都有缺点。鉴于单台activeMQ很可靠，而我们的基础平台组愿意用硬件备份，最终还是决定不用master-slave了，也不用broker cluster，就用单台，通过硬件冗余保证数据不会丢失，并找另外一台刀片机做冷备，在主服务器失效时顶替。</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/207/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>我为公司制定的Java代码规范</title>
		<link>http://www.sulong.info/archives/186</link>
		<comments>http://www.sulong.info/archives/186#comments</comments>
		<pubDate>Fri, 05 Jun 2009 06:20:07 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.sulong.info/?p=186</guid>
		<description><![CDATA[Java代码规范 本Java代码规范以SUN的标准Java代码规范为基础，为适应我们公司的实际需要，可能会做一些修改。本文档中没有说明的地方，请参看SUN Java标准代码规范。如果两边有冲突，以本文档为准。 1. 标识符命名规范 1.1 概述 标识符的命名力求做到统一、达意和简洁。 1.1.1 统一 统一是指，对于同一个概念，在程序中用同一种表示方法，比如对于供应商，既可以用supplier，也可以用provider，但是我们只能选定一个使用，至少在一个Java项目中保持统一。统一是作为重要的，如果对同一概念有不同的表示方法，会使代码混乱难以理解。即使不能取得好的名称，但是只要统一，阅读起来也不会太困难，因为阅读者只要理解一次。 1.1.2 达意 达意是指，标识符能准确的表达出它所代表的意义，比如： newSupplier, OrderPaymentGatewayService等；而 supplier1, service2，idtts等则不是好的命名方式。准确有两成含义，一是正确，而是丰富。如果给一个代表供应商的变量起名是 order，显然没有正确表达。同样的，supplier1, 远没有targetSupplier意义丰富。 1.1.3 简洁 简洁是指，在统一和达意的前提下，用尽量少的标识符。如果不能达意，宁愿不要简洁。比如：theOrderNameOfTheTargetSupplierWhichIsTransfered 太长， transferedTargetSupplierOrderName则较好，但是transTgtSplOrdNm就不好了。省略元音的缩写方式不要使用，我们的英语往往还没有好到看得懂奇怪的缩写。 1.1.4 骆驼法则 Java中，除了包名，静态常量等特殊情况，大部分情况下标识符使用骆驼法则，即单词之间不使用特殊符号分割，而是通过首字母大写来分割。比如: SupplierName, addNewContract，而不是 supplier_name, add_new_contract。 1.1.5 英文 vs 拼音 尽量使用通俗易懂的英文单词，如果不会可以向队友求助，实在不行则使用汉语拼音，避免拼音与英文混用。比如表示归档，用archive比较好, 用pigeonhole则不好，用guiDang尚可接受。 1.2 包名 使用小写字母如 com.xxx.settlment ，不要 com.xxx.Settlement 单词间不要用字符隔开，比如 com.xxx.settlment.jsfutil ，而不要 com.xxx.settlement.jsf_util 1.3 类名 1.3.1 首字母大写 类名要首字母大写，比如 SupplierService, PaymentOrderAction；不要 [...]]]></description>
			<content:encoded><![CDATA[<h1>Java代码规范</h1>
<p>本Java代码规范以SUN的标准Java代码规范为基础，为适应我们公司的实际需要，可能会做一些修改。本文档中没有说明的地方，请参看SUN Java标准代码规范。如果两边有冲突，以本文档为准。</p>
<h2>1. 标识符命名规范</h2>
<h3>1.1 概述</h3>
<p>标识符的命名力求做到统一、达意和简洁。</p>
<h4>1.1.1 统一</h4>
<p>统一是指，对于同一个概念，在程序中用同一种表示方法，比如对于供应商，既可以用supplier，也可以用provider，但是我们只能选定一个使用，至少在一个Java项目中保持统一。统一是作为重要的，如果对同一概念有不同的表示方法，会使代码混乱难以理解。即使不能取得好的名称，但是只要统一，阅读起来也不会太困难，因为阅读者只要理解一次。</p>
<h4>1.1.2 达意</h4>
<p>达意是指，标识符能准确的表达出它所代表的意义，比如： newSupplier, OrderPaymentGatewayService等；而 supplier1, service2，idtts等则不是好的命名方式。准确有两成含义，一是正确，而是丰富。如果给一个代表供应商的变量起名是 order，显然没有正确表达。同样的，supplier1, 远没有targetSupplier意义丰富。</p>
<h4>1.1.3 简洁</h4>
<p>简洁是指，在统一和达意的前提下，用尽量少的标识符。如果不能达意，宁愿不要简洁。比如：theOrderNameOfTheTargetSupplierWhichIsTransfered 太长， transferedTargetSupplierOrderName则较好，但是transTgtSplOrdNm就不好了。省略元音的缩写方式不要使用，我们的英语往往还没有好到看得懂奇怪的缩写。</p>
<h4>1.1.4 骆驼法则</h4>
<p>Java中，除了包名，静态常量等特殊情况，大部分情况下标识符使用骆驼法则，即单词之间不使用特殊符号分割，而是通过首字母大写来分割。比如: SupplierName, addNewContract，而不是 supplier_name, add_new_contract。</p>
<h4>1.1.5 英文 vs 拼音</h4>
<p>尽量使用通俗易懂的英文单词，如果不会可以向队友求助，实在不行则使用汉语拼音，避免拼音与英文混用。比如表示归档，用archive比较好, 用pigeonhole则不好，用guiDang尚可接受。</p>
<h3>1.2 包名</h3>
<p>使用小写字母如</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">com.<span style="color: #006633;">xxx</span>.<span style="color: #006633;">settlment</span></div></div>
<p>，不要</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">com.<span style="color: #006633;">xxx</span>.<span style="color: #006633;">Settlement</span></div></div>
<p>单词间不要用字符隔开，比如</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">com.<span style="color: #006633;">xxx</span>.<span style="color: #006633;">settlment</span>.<span style="color: #006633;">jsfutil</span></div></div>
<p>，而不要</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">com.<span style="color: #006633;">xxx</span>.<span style="color: #006633;">settlement</span>.<span style="color: #006633;">jsf_util</span></div></div>
<h3>1.3 类名</h3>
<h4>1.3.1 首字母大写</h4>
<p>类名要首字母大写，比如 SupplierService, PaymentOrderAction；不要 supplierService, paymentOrderAction.</p>
<h4>1.3.2 后缀</h4>
<p>类名往往用不同的后缀表达额外的意思，如下表：</p>
<table border="1px">
<tr>
<td>后缀名</td>
<td>意义</td>
<td>举例</td>
</tr>
<tr>
<td>Service</td>
<td>表明这个类是个服务类，里面包含了给其他类提同业务服务的方法</td>
<td>PaymentOrderService</td>
</tr>
<tr>
<td>Impl</td>
<td>这个类是一个实现类，而不是接口</td>
<td>PaymentOrderServiceImpl</td>
</tr>
<tr>
<td>Inter</td>
<td>这个类是一个接口</td>
<td>LifeCycleInter</td>
</tr>
<tr>
<td>Dao</td>
<td>这个类封装了数据访问方法</td>
<td>PaymentOrderDao</td>
</tr>
<tr>
<td>Action</td>
<td>直接处理页面请求，管理页面逻辑了类</td>
<td>UpdateOrderListAction</td>
</tr>
<tr>
<td>Listener</td>
<td>响应某种事件的类</td>
<td>	PaymentSuccessListener</td>
</tr>
<tr>
<td>Event</td>
<td>这个类代表了某种事件</td>
<td>PaymentSuccessEvent</td>
</tr>
<tr>
<td>Servlet</td>
<td>一个Servlet</td>
<td>PaymentCallbackServlet</td>
</tr>
<tr>
<td>Factory</td>
<td>生成某种对象工厂的类</td>
<td>PaymentOrderFactory</td>
</tr>
<tr>
<td>Adapter</td>
<td>用来连接某种以前不被支持的对象的类</td>
<td>DatabaseLogAdapter</td>
</tr>
<tr>
<td>Job</td>
<td>某种按时间运行的任务</td>
<td>PaymentOrderCancelJob</td>
</tr>
<tr>
<td>Wrapper</td>
<td>这是一个包装类，为了给某个类提供没有的能力</td>
<td>	SelectableOrderListWrapper</td>
</tr>
<tr>
<td>Bean</td>
<td>这是一个POJO</td>
<td>	MenuStateBean</td>
</tr>
</table>
<h3>1.4 方法名</h3>
<p>首字母小写，如 addOrder() 不要 AddOrder()<br />
动词在前，如 addOrder()，不要orderAdd()<br />
动词前缀往往表达特定的含义，如下表：</p>
<table border="1px">
<tr>
<td>前缀名</td>
<td>	意义</td>
<td>	举例</td>
</tr>
<tr>
<td>create</td>
<td>	创建</td>
<td>	createOrder()</td>
</tr>
<tr>
<td>delete</td>
<td>	删除</td>
<td>	deleteOrder()</td>
</tr>
<tr>
<td>add</td>
<td>	创建，暗示新创建的对象属于某个集合</td>
<td>	addPaidOrder()</td>
</tr>
<tr>
<td>remove</td>
<td>	删除</td>
<td>	removeOrder()</td>
</tr>
<tr>
<td>init或则initialize</td>
<td>	初始化，暗示会做些诸如获取资源等特殊动作</td>
<td>	initializeObjectPool</td>
</tr>
<tr>
<td>destroy</td>
<td>	销毁，暗示会做些诸如释放资源的特殊动作</td>
<td>	destroyObjectPool</td>
</tr>
<tr>
<td>open</td>
<td>	打开</td>
<td>	openConnection()</td>
</tr>
<tr>
<td>close</td>
<td>	关闭</td>
<td>	closeConnection()<</td>
</tr>
<tr>
<td>read</td>
<td>	读取</td>
<td>	readUserName()</td>
</tr>
<tr>
<td>write</td>
<td>	写入</td>
<td>	writeUserName()</td>
</tr>
<tr>
<td>get</td>
<td>	获得</td>
<td>	getName()</td>
</tr>
<tr>
<td>set</td>
<td>	设置</td>
<td>	setName()</td>
</tr>
<tr>
<td>prepare</td>
<td>	准备</td>
<td>	prepareOrderList()</td>
</tr>
<tr>
<td>copy</td>
<td>	复制</td>
<td>	copyCustomerList()</td>
</tr>
<tr>
<td>modity</td>
<td>	修改</td>
<td>	modifyActualTotalAmount()</td>
</tr>
<tr>
<td>calculate</td>
<td>	数值计算</td>
<td>	calculateCommission()</td>
</tr>
<tr>
<td>do	</td>
<td>执行某个过程或流程</td>
<td>	doOrderCancelJob()</td>
</tr>
<tr>
<td>dispatch</td>
<td>	判断程序流程转向</td>
<td>	dispatchUserRequest()</td>
</tr>
<tr>
<td>start</td>
<td>	开始</td>
<td>	startOrderProcessing()</td>
</tr>
<tr>
<td>stop</td>
<td>	结束</td>
<td>	stopOrderProcessing()</td>
</tr>
<tr>
<td>send</td>
<td>	发送某个消息或事件</td>
<td>	sendOrderPaidMessage()</td>
</tr>
<tr>
<td>receive</td>
<td>	接受消息或时间</td>
<td>	receiveOrderPaidMessgae()</td>
</tr>
<tr>
<td>respond</td>
<td>	响应用户动作</td>
<td>	responseOrderListItemClicked()</td>
</tr>
<tr>
<td>find</td>
<td>	查找对象</td>
<td>	findNewSupplier()</td>
</tr>
<tr>
<td>update</td>
<td>	更新对象</td>
<td>	updateCommission()</td>
</tr>
</table>
<p>find方法在业务层尽量表达业务含义，比如 findUnsettledOrders()，查询未结算订单，而不要findOrdersByStatus()。 数据访问层，find,update等方法可以表达要执行的sql，比如findByStatusAndSupplierIdOrderByName(Status.PAID, 345)</p>
<h3>1.5 域（field）名</h3>
<h4>1.5.1 静态常量</h4>
<p>全大写用下划线分割，如</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> find <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> ORDER_PAID_EVENT <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ORDER_PAID_EVENT&quot;</span><span style="color: #339933;">;</span></div></div>
<h4>1.5.2 枚举</h4>
<p>全大写，用下划线分割，如</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">enum</span> Events <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; ORDER_PAID,<br />
&nbsp; &nbsp; ORDER_CREATED<br />
<span style="color: #009900;">&#125;</span></div></div>
<h4>1.5.3 其他<br />
<h4>
<p>首字母小写，骆驼法则，如：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> orderName<span style="color: #339933;">;</span></div></div>
<h3>1.6 局部变量名</h3>
<p>参数和局部变量名首字母小写，骆驼法则。尽量不要和域冲突，尽量表达这个变量在方法中的意义。</p>
<h2>2. 代码格式</h2>
<p>用空格字符缩进源代码，不要用tab，每个缩进4个空格。</p>
<h3>2.1 源文件编码</h3>
<p>源文件使用utf-8编码，结尾用unix n 分格。</p>
<h3>2.2 行宽</h3>
<p>行宽度不要超过130。</p>
<h3>2.3 包的导入</h3>
<p>删除不用的导入，尽量不要使用整个包的导入。在eclipse下经常使用快捷键 ctrl+shift+o 修正导入。</p>
<h3>2.4 类格式</h3>
<h3>2.5 域格式</h3>
<p>每行只能声明一个域。<br />
域的声明用空行隔开。</p>
<h3>2.5 方法格式</h3>
<h3>2.6 代码块格式</h3>
<h4>2.6.1 缩进风格</h4>
<p>大括号的开始在代码块开始的行尾，闭合在和代码块同一缩进的行首，例如：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.test</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestStyle <span style="color: #000000; font-weight: bold;">extends</span> SomeClass <span style="color: #000000; font-weight: bold;">implements</span> AppleInter, BananaInter <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> THIS_IS_CONST <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CONST VALUE&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> localVariable <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> compute<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> arg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>arg.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> &nbsp;<span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>arg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>arg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>condition<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; otherMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>condition<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callFunctionb<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h4>2.6.2 空格的使用</h4>
<h5>2.6.2.1 表示分割时用一个空格</h5>
<p>不能这样：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">if</span> &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a <span style="color: #339933;">&gt;</span> &nbsp; &nbsp; &nbsp; &nbsp;b &nbsp; <span style="color: #009900;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//do something here</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<h5>2.6.2.2 二元三元运算符两边用一个空格隔开</h5>
<p>如下：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">a <span style="color: #339933;">+</span> b <span style="color: #339933;">=</span> c<span style="color: #339933;">;</span><br />
b <span style="color: #339933;">-</span> d <span style="color: #339933;">=</span> e<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">return</span> a <span style="color: #339933;">==</span> b <span style="color: #339933;">?</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></div></div>
<p>不能如下：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">a<span style="color: #339933;">+</span>b<span style="color: #339933;">=</span>c<span style="color: #339933;">;</span><br />
b<span style="color: #339933;">-</span>d<span style="color: #339933;">=</span>e<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">==</span>b<span style="color: #339933;">?</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></div></div>
<h5>2.6.2.3 逗号语句后如不还行，紧跟一个空格</h5>
<p>如下：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">call<span style="color: #009900;">&#40;</span>a, b, c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>不能如下：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">call<span style="color: #009900;">&#40;</span>a,b,c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h4>2.6.3 空行的使用</h4>
<p>空行可以表达代码在语义上的分割，注释的作用范围，等等。将类似操作，或一组操作放在一起不用空行隔开，而用空行隔开不同组的代码， 如图：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">order <span style="color: #339933;">=</span> orderDao.<span style="color: #006633;">findOrderById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//update properties</span><br />
order.<span style="color: #006633;">setUserName</span><span style="color: #009900;">&#40;</span>userName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
order.<span style="color: #006633;">setPrice</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">456</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
order.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span>PAID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
orderService.<span style="color: #006633;">updateTotalAmount</span><span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
session.<span style="color: #006633;">saveOrUpdate</span><span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>上例中的空行，使注释的作用域很明显.</p>
<ul>
<li>连续两行的空行代表更大的语义分割。</li>
<li>方法之间用空行分割</li>
<li>域之间用空行分割</li>
<li>超过十行的代码如果还不用空行分割，就会增加阅读困难</li>
</ul>
<h2>3. 注释规范</h2>
<h3>3.1 注释 vs 代码</h3>
<ul>
<li>注释宜少二精，不宜多而滥，更不能误导</li>
<li>命名达意，结构清晰， 类和方法等责任明确，往往不需要，或者只需要很少注释，就可以让人读懂；相反，代码混乱，再多的注释都不能弥补。所以，应当先在代码本身下功夫。</li>
<li>不能正确表达代码意义的注释，只会损害代码的可读性。</li>
<li>过于详细的注释，对显而易见的代码添加的注释，罗嗦的注释，还不如不写</li>
<p>。</p>
<li>注释要和代码同步，过多的注释会成为开发的负担</li>
<li>注释不是用来管理代码版本的，如果有代码不要了，直接删除，svn会有记录的，不要注释掉，否则以后没人知道那段注释掉的代码该不该删除。</li>
</ul>
<h3>3.2 Java Doc</h3>
<p>表明类、域和方法等的意义和用法等的注释，要以javadoc的方式来写。Java Doc是个类的使用者来看的，主要介绍 是什么，怎么用等信息。凡是类的使用者需要知道，都要用Java Doc 来写。非Java Doc的注释，往往是个代码的维护者看的，着重告述读者为什么这样写，如何修改，注意什么问题等。 如下：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
* This is a class comment<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestClass <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; * This is a field comment<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; * This is a method comment<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> call<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>3.3 块级别注释</h3>
<h4>3.3.1 块级别注释，单行时用 //, 多行时用 /* .. */。</h4>
<h4>3.3.2 较短的代码块用空行表示注释作用域</h4>
<h4>3.3.3 较长的代码块要用</h4>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*------ start: ------*/</span><br />
和<br />
<span style="color: #666666; font-style: italic;">/*-------- end: -------*/</span></div></div>
<p>包围<br />
如：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*----------start: 订单处理 ------- */</span><br />
<span style="color: #666666; font-style: italic;">//取得dao</span><br />
OrderDao dao <span style="color: #339933;">=</span> Factory.<span style="color: #006633;">getDao</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;OrderDao&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">/* 查询订单 */</span><br />
Order order <span style="color: #339933;">=</span> dao.<span style="color: #006633;">findById</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">456</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//更新订单</span><br />
order.<span style="color: #006633;">setUserName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;uu&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
order.<span style="color: #006633;">setPassword</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
order.<span style="color: #006633;">setPrice</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ddd&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
orderDao.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">/*----------end: 订单处理 ------- */</span></div></div>
<h4>3.3.4 可以考虑使用大括号来表示注释范围</h4>
<p>使用大括号表示注释作用范围的例子：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*----------订单处理 ------- */</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//取得dao</span><br />
&nbsp; &nbsp; OrderDao dao <span style="color: #339933;">=</span> Factory.<span style="color: #006633;">getDao</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;OrderDao&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* 查询订单 */</span><br />
&nbsp; &nbsp; Order order <span style="color: #339933;">=</span> dao.<span style="color: #006633;">findById</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">456</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//更新订单</span><br />
&nbsp; &nbsp; order.<span style="color: #006633;">setUserName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;uu&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; order.<span style="color: #006633;">setPassword</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; order.<span style="color: #006633;">setPrice</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ddd&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; orderDao.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>3.4 行内注释</h3>
<p>行内注释用 // 写在行尾</p>
<h2>4 最佳实践和禁忌</h2>
<h3>4.1 每次保存的时候，都让你的代码是最美的</h3>
<p>程序员都是懒惰的，不要想着等我完成了功能，再来优化代码的格式和结构，等真的把功能完成，很少有人会再愿意回头调整代码。</p>
<h3>4.2 使用log而不是System.out.println()</h3>
<p>log可以设定级别，可以控制输出到哪里，容易区分是在代码的什么地方打印的，而System.out.print则不行。而且，System.out.print的速度很慢。所以，除非是有意的，否则，都要用log。至少在提交到svn之前把System.out.print换成log。</p>
<h3>4.3 每个if while for等语句，都不要省略大括号{}</h3>
<p>看下面的代码：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">&gt;</span> b<span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; a<span style="color: #339933;">++;</span></div></div>
<p>如果在以后维护的时候，需要在a > b 时，把b++，一步小心就会写成：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">&gt;</span> b<span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; a<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; b<span style="color: #339933;">++;</span></div></div>
<p>这样就错了，因为无论a和b是什么关系，b++都会执行。 如果一开始就这样写：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">&gt;</span> b<span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; a<span style="color: #339933;">++;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>相信没有哪个笨蛋会把b++添加错的。而且，这个大括号使作用范围更明显，尤其是后面那行很长要折行时。</p>
<h3>4.4 善用TODO:</h3>
<p>在代码中加入 //TODO: ，大部分的ide都会帮你提示，让你知道你还有什么事没有做。比如：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>order.<span style="color: #006633;">isPaid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//TODO: 更新订单</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>4.5 在需要留空的地方放一个空语句或注释，告述读者，你是故意的</h3>
<p>比如：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>exists<span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>或：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>exists<span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//nothing to do</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>4.6 不要再对boolean值做true false判断</h3>
<p>比如：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>order.<span style="color: #006633;">isPaid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Do something here</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>不如写成：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>order.<span style="color: #006633;">isPaid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Do something here</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>后者读起来就很是 if order is paid, &#8230;. 要比 if order&#8217;s isPaid method returns true, … 更容易理解</p>
<h3>4.7 减少代码嵌套层次</h3>
<p>代码嵌套层次达3层以上时，一般人理解起来都会困难。下面的代码是一个简单的例子：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> demo<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> a, <span style="color: #000066; font-weight: bold;">int</span> b, <span style="color: #000066; font-weight: bold;">int</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">&gt;</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>b <span style="color: #339933;">&gt;</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doJobA<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>b <span style="color: #339933;">&lt;</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doJobB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>b <span style="color: #339933;">&gt;</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">&lt;</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doJobC<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
<span style="color: #009900;">&#125;</span></div></div>
<p>减少嵌套的方法有很多：</p>
<ul>
<li>合并条件</li>
<li>利用 return 以省略后面的else</li>
<li>利用子方法</li>
</ul>
<p>比如上例，合并条件后成为：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> demo<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> a, <span style="color: #000066; font-weight: bold;">int</span> b, <span style="color: #000066; font-weight: bold;">int</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">&gt;</span> b <span style="color: #339933;">&amp;&amp;</span> b <span style="color: #339933;">&gt;</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doJobA<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">&gt;</span> b <span style="color: #339933;">&amp;&amp;</span> c <span style="color: #339933;">&gt;</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doJobB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">&lt;=</span> b <span style="color: #339933;">&amp;&amp;</span> c <span style="color: #339933;">&lt;</span> b <span style="color: #339933;">&amp;&amp;</span> a <span style="color: #339933;">&lt;</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doJobC<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>如果利用return 则成为：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> demo<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> a, <span style="color: #000066; font-weight: bold;">int</span> b, <span style="color: #000066; font-weight: bold;">int</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">&gt;</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>b <span style="color: #339933;">&gt;</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doJobA<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doJobB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>b <span style="color: #339933;">&gt;</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">&lt;</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doJobC<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
<span style="color: #009900;">&#125;</span></div></div>
<p>利用子方法，就是将嵌套的程序提取出来放到另外的方法里。</p>
<h3>4.8 程序职责单一</h3>
<p>关注点分离是软件开发的真理。人类自所以能够完成复杂的工作，就是因为人类能够将工作分解到较小级别的任务上，在做每个任务时关注更少的东西。让程序单元的职责单一，可以使你在编写这段程序时关注更少的东西，从而降低难度，减少出错。</p>
<h3>4.9 变量的声明，初始化和被使用尽量放到一起</h3>
<p>比方说如下代码：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">int</span> orderNum<span style="color: #339933;">=</span> getOrderNum<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//do something withou orderNum here</span><br />
<br />
call<span style="color: #009900;">&#40;</span>orderNum<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>上例中的注释处代表了一段和orderNum不相关的代码。orderNum的声明和初始化离被使用的地方相隔了很多行的代码，这样做不好，不如这样：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//do something withou orderNum here</span><br />
<br />
<span style="color: #000066; font-weight: bold;">int</span> orderNum<span style="color: #339933;">=</span> getOrderNum<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
call<span style="color: #009900;">&#40;</span>orderNum<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>4.10 缩小变量的作用域</h3>
<p>能用局部变量的，不要使用实例变量，能用实例变量的，不要使用类变量。变量的生存期越短，以为着它被误用的机会越小，同一时刻程序员要关注的变量的状态越少。实例变量和类变量默认都不是线程安全的，局部变量是线程安全的。比如如下代码：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> OrderPayAction<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Order order<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; order <span style="color: #339933;">=</span> orderDao.<span style="color: #006633;">findOrder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doJob1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doJob2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> doJob1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doSomething<span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> doJob2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doOtherThing<span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>上例中order只不过担当了在方法间传递参数之用，用下面的方法更好：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> OrderPayAction<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; order <span style="color: #339933;">=</span> orderDao.<span style="color: #006633;">findOrder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doJob1<span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doJob2<span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> doJob1<span style="color: #009900;">&#40;</span>Order order<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doSomething<span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> doJob2<span style="color: #009900;">&#40;</span>Order order<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doOtherThing<span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>4.11 尽量不要用参数来带回方法运算结果</h3>
<p>比如：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> calculate<span style="color: #009900;">&#40;</span>Order order<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> result <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//do lots of computing and store it in the result</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; order.<span style="color: #006633;">setResult</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> action<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; order <span style="color: #339933;">=</span> orderDao.<span style="color: #006633;">findOrder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; calculate<span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// do lots of things about order</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>例子中calculate方法通过传入的order对象来存储结果， 不如如下写：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> calculate<span style="color: #009900;">&#40;</span>Order order<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> result <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//do lots of computing and store it in the result</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> action<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; order <span style="color: #339933;">=</span> orderDao.<span style="color: #006633;">findOrder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; order.<span style="color: #006633;">setResult</span><span style="color: #009900;">&#40;</span>calculate<span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// do lots of things about order</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/186/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>为什么java没有php开发web容易？</title>
		<link>http://www.sulong.info/archives/176</link>
		<comments>http://www.sulong.info/archives/176#comments</comments>
		<pubDate>Tue, 02 Jun 2009 03:34:17 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.sulong.info/?p=176</guid>
		<description><![CDATA[为什么java没有php开发web容易？原因可能有很多种，就我这半年的php经验来看，主要有以下几点原因： 1. java没有php那样好用的map(在php里叫做array) 这一点很重要，直接看代码吧： php的: $values= array&#40; &#160; &#160; 'login_name'=&#62;'sulong', &#160; &#160; 'password'=&#62;'', &#160; &#160; 'login_result'=&#62;array&#40; &#160; &#160; &#160; &#160; 'error_no'=&#62;3, &#160; &#160; &#160; &#160; 'msg'=&#62; 'account disabled' &#160; &#160; &#41; &#41;; java的: Map values = new HashMap&#40;&#41;; values.put&#40;&#34;login_name&#34;, &#34;sulong&#34;&#41;; values.put&#40;&#34;password&#34;, &#34;&#34;&#41;; Mapresult = new HashMap&#40;&#41;; result.put&#40;&#34;error_no&#34;, &#34;3&#34;&#41;; result. put&#40;&#34;msg&#34;, &#34;account disabled&#34;&#41;; values.put&#40;&#34;login_result&#34;, result&#41;; 很多时候，java程序员宁愿选择建一个新的类来做数据的载体，也就是常说的bean了。但是很多时候，我只想要一个简洁好用的Map而已。 [...]]]></description>
			<content:encoded><![CDATA[<p>为什么java没有php开发web容易？原因可能有很多种，就我这半年的php经验来看，主要有以下几点原因：</p>
<h2>1. java没有php那样好用的map(在php里叫做array)</h2>
<p>这一点很重要，直接看代码吧：<br />
php的:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$values</span><span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'login_name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'sulong'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #0000ff;">'password'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #0000ff;">'login_result'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'error_no'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'msg'</span><span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'account disabled'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>java的:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amap+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Map</span></a> values <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ahashmap+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">HashMap</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
values.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;login_name&quot;</span>, <span style="color: #0000ff;">&quot;sulong&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
values.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;password&quot;</span>, <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
Mapresult <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ahashmap+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">HashMap</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
result.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error_no&quot;</span>, <span style="color: #0000ff;">&quot;3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
result. <span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;msg&quot;</span>, <span style="color: #0000ff;">&quot;account disabled&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
values.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;login_result&quot;</span>, result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>很多时候，java程序员宁愿选择建一个新的类来做数据的载体，也就是常说的bean了。但是很多时候，我只想要一个简洁好用的Map而已。</p>
<p>最好的是groovy：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">values <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; login_name<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;sulong&quot;</span>, <br />
&nbsp; &nbsp; password<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span>, <br />
&nbsp; &nbsp; login_result<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; error_no<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;3&quot;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; msg<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;account disabled&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#93;</span></div></div>
<p>仅凭这一点，我就觉得在groovy克服其它问题后，必会成为java在web开发上的利器。</p>
<h2>2. java经常要重启服务器</h2>
<p>用java开发web，只要改动了java代码，就要重新启动服务器，至少要重新部署一下应用，才能看到新代码的效果，而php就不需要。所以，开发php的时候，经常是一边写，一边运行看效果，快速的迭代和开发。相比之下，java要不断的编译重新发布，重启服务器，比php慢多了。如果java开发也可以即时生效，不用不断的重启的话，那么java的开发效率必然会有很大的提升。</p>
<h2>3. 动态类型</h2>
<p>确实，动态类型意味这较弱的IDE支持，意味着要进行更多的测试，以避免意想不到的效果，但是，另一方面，动态类型将代码简化，让程序员关注于做什么，而不是怎么做，提升了生产率。比如，用动态类型语言的时候，我们只要说，dog.bark()，那么我们不用关心dog到底是不是dog，只要他能bark()，那bark()就好了。而在静态类型的语言中，我们一定要先搞清楚那是什么，能不能bark,如果不能bark怎么办？是不是要做一层包装？做类型的转换？等等。严谨固然有他的好处，但自由简明显然开发起来更加的快速。</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/176/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>企业应用集成&#8211;java</title>
		<link>http://www.sulong.info/archives/166</link>
		<comments>http://www.sulong.info/archives/166#comments</comments>
		<pubDate>Tue, 28 Apr 2009 07:34:24 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[程序]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[jms]]></category>
		<category><![CDATA[思考]]></category>
		<category><![CDATA[经验]]></category>

		<guid isPermaLink="false">http://www.sulong.info/?p=166</guid>
		<description><![CDATA[java企业应用最终往往运行在某种应用服务器中，比如tomcat，jboss等。部署的单元是war, jar 或者 ear文件，开始的时候，我们可能会将多个应用放到一个单元里来开发管理和部署。随着应用的增多，为了能够让每个应用能够独立的运行不受干扰，也为了开发工作能很好的安排，往往会将各应用分开，于是会有多个war, jar,或ear文件。试想一下，假设你有订单系统，产品系统，客服系统，等，如果这些应用都被放在同一个部署单元里，当需要更新客服系统时，会短暂的导致订单系统不能正常工作，而这种情况往往是不能被接受的。另外，如果客服系统非常繁忙，由于部署在同一台物理服务器上，共享硬件资源，订单系统会受到干扰，而这也不是我们想要的。将单独的应用单独打包，单独部署成为必然。那么就面临着让这些应用通信，将它们集成的问题。 与java和php之间集成相比，java与java之间的集成，不仅可以使用共享数据库，soap，或基于http的简单的文档交换，还可以使用jms，rmi等多种方案。其中jms是j2ee众多的标准中最为成功的协议之一，很少有人针对它发表过不满意见。我们在工作中就采用了jms。和http相比，它有如下优势: 为java量身定做的 jms不仅可以传输普通的字符串，还可以直接传输java对象，并和java的其它服务结合起来，接口api也符合java的使用习惯。如果用http，我们还得要考虑如何把对象序列化等问题，比较麻烦。 使用长连接 http主要用在客户端和服务器交互上，为了能让服务器支持大量的客户端，http往往都以一种无状态和短连接的方式工作。而jms主要用在java应用之间的交互，不需要支持那么多的客户端，用长连接更加适合。典型的http服务器，可能同时要给数千数万人提供服务，如果服务器和每个客户端保持一个连接，那对服务器来说，显然是一个非常大的负担。所以，通常客户端和http的一次交互完成后，连接就断开，而下次访问时，还得要重新建立连接。jms服务器面对的客户端一般只有几个，至多几十个，和每个客户端保持连接的开销是可以接受的。典型的企业可能有如下系统：订单系统，产品系统，结算系统，等等，这些系统，每个都和集中的jms服务器保持一个连接，总连接数也是很少的，所以可以保持长连接。这客户端和jms服务器通信的时候，不必每次都建立连接，减少了网络操作，提升了性能。 异步 http操作的过程是同步的，如果要使用异步的方式工作，需要程序员再做些编码工作。而jms天生就以异步的方式来工作的，不必做过多的编码。消息的生产者一旦把消息发送出去，就可以立刻返回不必等待消息的消费结果，后续的消息消费可以自动的异步执行。将不必要立刻执行的动作异步执行，可以大大降低系统的响应时间。 可靠的消息传递 在使用http通信的过程中，如果发生了网络异常，是否要做重发，以及如何重发，会是一件很痛苦的事情。jms提供了persitent的传递模式，在这种模式时，客户端代理在发送消息之前，会先把消息持久，如果发生网络异常，客户端代理会自动处理重发，而不需要应用程序关心。jms消息的唯一编号，可以有效的阻止一个消息被重复处理的问题。 在java应用集成时，jms比http更适合。成熟的jms服务器软件很多，除了ibm的mq，jboss mq, apache activemq 都是很不错的选择。]]></description>
			<content:encoded><![CDATA[<p>java企业应用最终往往运行在某种应用服务器中，比如tomcat，jboss等。部署的单元是war, jar 或者 ear文件，开始的时候，我们可能会将多个应用放到一个单元里来开发管理和部署。随着应用的增多，为了能够让每个应用能够独立的运行不受干扰，也为了开发工作能很好的安排，往往会将各应用分开，于是会有多个war, jar,或ear文件。试想一下，假设你有订单系统，产品系统，客服系统，等，如果这些应用都被放在同一个部署单元里，当需要更新客服系统时，会短暂的导致订单系统不能正常工作，而这种情况往往是不能被接受的。另外，如果客服系统非常繁忙，由于部署在同一台物理服务器上，共享硬件资源，订单系统会受到干扰，而这也不是我们想要的。将单独的应用单独打包，单独部署成为必然。那么就面临着让这些应用通信，将它们集成的问题。<br />
<br/><br />
与java和php之间集成相比，java与java之间的集成，不仅可以使用共享数据库，soap，或基于http的简单的文档交换，还可以使用jms，rmi等多种方案。其中jms是j2ee众多的标准中最为成功的协议之一，很少有人针对它发表过不满意见。我们在工作中就采用了jms。和http相比，它有如下优势:</p>
<h3>为java量身定做的</h3>
<p>jms不仅可以传输普通的字符串，还可以直接传输java对象，并和java的其它服务结合起来，接口api也符合java的使用习惯。如果用http，我们还得要考虑如何把对象序列化等问题，比较麻烦。</p>
<h3>使用长连接</h3>
<p>http主要用在客户端和服务器交互上，为了能让服务器支持大量的客户端，http往往都以一种无状态和短连接的方式工作。而jms主要用在java应用之间的交互，不需要支持那么多的客户端，用长连接更加适合。典型的http服务器，可能同时要给数千数万人提供服务，如果服务器和每个客户端保持一个连接，那对服务器来说，显然是一个非常大的负担。所以，通常客户端和http的一次交互完成后，连接就断开，而下次访问时，还得要重新建立连接。jms服务器面对的客户端一般只有几个，至多几十个，和每个客户端保持连接的开销是可以接受的。典型的企业可能有如下系统：订单系统，产品系统，结算系统，等等，这些系统，每个都和集中的jms服务器保持一个连接，总连接数也是很少的，所以可以保持长连接。这客户端和jms服务器通信的时候，不必每次都建立连接，减少了网络操作，提升了性能。</p>
<h3>异步</h3>
<p>http操作的过程是同步的，如果要使用异步的方式工作，需要程序员再做些编码工作。而jms天生就以异步的方式来工作的，不必做过多的编码。消息的生产者一旦把消息发送出去，就可以立刻返回不必等待消息的消费结果，后续的消息消费可以自动的异步执行。将不必要立刻执行的动作异步执行，可以大大降低系统的响应时间。</p>
<h3>可靠的消息传递</h3>
<p>在使用http通信的过程中，如果发生了网络异常，是否要做重发，以及如何重发，会是一件很痛苦的事情。jms提供了persitent的传递模式，在这种模式时，客户端代理在发送消息之前，会先把消息持久，如果发生网络异常，客户端代理会自动处理重发，而不需要应用程序关心。jms消息的唯一编号，可以有效的阻止一个消息被重复处理的问题。<br />
<br/></p>
<p>在java应用集成时，jms比http更适合。成熟的jms服务器软件很多，除了ibm的mq，jboss mq, apache activemq 都是很不错的选择。</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/166/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>企业应用集成&#8211;java与php</title>
		<link>http://www.sulong.info/archives/162</link>
		<comments>http://www.sulong.info/archives/162#comments</comments>
		<pubDate>Thu, 23 Apr 2009 03:18:56 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[程序]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[思考]]></category>
		<category><![CDATA[经验]]></category>
		<category><![CDATA[集成]]></category>

		<guid isPermaLink="false">http://www.sulong.info/?p=162</guid>
		<description><![CDATA[随着企业的发展，企业的业务种类越来越多，业务量越来越大，越来越复杂，部署在单一的服务器上的单一的应用很难再满足业务系统在可靠性，性能，易于维护和扩展等很多方面的需求。进而把企业系统拆分成很多的独立应用，部署在多台服务器上，成为必须。拆分开的多个应用并不能独立的完成所有的业务，应用间必然需要集成。用什么样的方式把各应用集成，不仅能满足系统的功能需要，还能做到易于维护，易于扩展，高性能，高可靠，就成为了一个难题。 公司最开始用php做所有的内部(指面向公司内部职员)和外部的应用（外部指面向internet用户的），之后公司开始采用java来逐步代替php来开发内部应用。所以，我们java程序不但面临着如何集成java内部的各个应用的问题，还面临着如何和php集成的问题。我们使用用过以下方法和php集成： 数据库共享： java和php使用相同的数据库，为了减少混乱，同一张表，只能由java或php其中一方负责写。这种方法使用起来直观，易用，但还是有很多的问题。 首先，java和php对数据库的一些行为是不一致的。mysql数据库的datatime类型，再插入不正确的数据时，mysql会在里面插入&#8221;0000-00-00 00:00:00&#8243;, php在读取这样的日期时没有问题，反正，他是字符串。但是java这边却会出错，应为jdbc会默认的将它转化成java.sql.Date类型，而在转化的是时候会发生异常。更大的阻力来自于php和java两个团队在使用数据库习惯上的差异。 其次，数据库共享功能不够强。比如，php完成某个业务时，需要及时通知java，但是仅通过数据库共享不好做，除非java对数据库轮询，但这样做显然很难在及时性和性能两者之间找到平衡。 再次，可扩展性差，但需要做出变更时，变更数据库显然是一件更加痛苦的事。这不仅要处理遗留的数据，还要调节两边的数据库访问程序。 所以，正是后续的项目里就没有再使用过这一方案。 SOAP： 按理说soap这种语言中立又规范化的技术应该非常适合于这种场景，但是在使用过程中还是会出现很多的问题。 首先，php和java两边对soap的理解并不一样。Java生成的布尔值在soap的xml里并表示为true或则false，但是在php看来只要不是0，都是true，所以&#8221;false&#8221;也是true。也许是php那边所使用的soap支持程序有问题，但是这个问题确实给我们带来了很多麻烦。还有一个要命的问题，就是字符集的问题，php以前一直使用gb2312和gb18030，但是soap的xml并不支持gb18030里的某些奇特字符，一旦遇到这样的字符，xml解析就失败。后来，不得不把可能出现此类字符的内容用base64编码。顺便发一下牢骚，这些奇怪的字符网网都来自于那些爱使用脑残体火星文的新新人类，这些人简直就是弱智！ 基于http的简单文档交互: 这种方法很简单，就是通过http post个xml或其它文档来实现交互。这种方式就是原始的使用http的soap，只是soap后来加了一大堆的标准（ws-*），在这些标准对我们来说没有意义，而php和java两边对标准的支持不兼容的时候，soap就成了鸡肋。后来，我们就干脆抛弃了soap，而通过我们自己的程序来发送和接收我们自定义的没有歧义的xml。目前为止，这是两边都乐意使用且很少出问题的方式。这种方式需要两边很好的配合，需要规范化的文档，否则以后只会变得混乱。 我们也曾想过使用基于java的php实现，来达到让java和php在语言级别集成，这种方法对于php遗留系统来说风险太大，而且php程序员面并不乐意生活在java的虚拟机内。也许未来这能成为一个成熟的方案。 正因为使用不同技术的应用之间的集成存在很多的问题，在实际项目中应该尽量避免。]]></description>
			<content:encoded><![CDATA[<p>随着企业的发展，企业的业务种类越来越多，业务量越来越大，越来越复杂，部署在单一的服务器上的单一的应用很难再满足业务系统在可靠性，性能，易于维护和扩展等很多方面的需求。进而把企业系统拆分成很多的独立应用，部署在多台服务器上，成为必须。拆分开的多个应用并不能独立的完成所有的业务，应用间必然需要集成。用什么样的方式把各应用集成，不仅能满足系统的功能需要，还能做到易于维护，易于扩展，高性能，高可靠，就成为了一个难题。</p>
<p>公司最开始用php做所有的内部(指面向公司内部职员)和外部的应用（外部指面向internet用户的），之后公司开始采用java来逐步代替php来开发内部应用。所以，我们java程序不但面临着如何集成java内部的各个应用的问题，还面临着如何和php集成的问题。我们使用用过以下方法和php集成：</p>
<h3>数据库共享：</h3>
<p>java和php使用相同的数据库，为了减少混乱，同一张表，只能由java或php其中一方负责写。这种方法使用起来直观，易用，但还是有很多的问题。<br />
首先，java和php对数据库的一些行为是不一致的。mysql数据库的datatime类型，再插入不正确的数据时，mysql会在里面插入&#8221;0000-00-00 00:00:00&#8243;, php在读取这样的日期时没有问题，反正，他是字符串。但是java这边却会出错，应为jdbc会默认的将它转化成java.sql.Date类型，而在转化的是时候会发生异常。更大的阻力来自于php和java两个团队在使用数据库习惯上的差异。<br />
其次，数据库共享功能不够强。比如，php完成某个业务时，需要及时通知java，但是仅通过数据库共享不好做，除非java对数据库轮询，但这样做显然很难在及时性和性能两者之间找到平衡。<br />
再次，可扩展性差，但需要做出变更时，变更数据库显然是一件更加痛苦的事。这不仅要处理遗留的数据，还要调节两边的数据库访问程序。<br />
所以，正是后续的项目里就没有再使用过这一方案。</p>
<h3>SOAP：</h3>
<p>按理说soap这种语言中立又规范化的技术应该非常适合于这种场景，但是在使用过程中还是会出现很多的问题。<br />
首先，php和java两边对soap的理解并不一样。Java生成的布尔值在soap的xml里并表示为true或则false，但是在php看来只要不是0，都是true，所以&#8221;false&#8221;也是true。也许是php那边所使用的soap支持程序有问题，但是这个问题确实给我们带来了很多麻烦。还有一个要命的问题，就是字符集的问题，php以前一直使用gb2312和gb18030，但是soap的xml并不支持gb18030里的某些奇特字符，一旦遇到这样的字符，xml解析就失败。后来，不得不把可能出现此类字符的内容用base64编码。顺便发一下牢骚，这些奇怪的字符网网都来自于那些爱使用脑残体火星文的新新人类，这些人简直就是弱智！</p>
<h3>基于http的简单文档交互:</h3>
<p>这种方法很简单，就是通过http post个xml或其它文档来实现交互。这种方式就是原始的使用http的soap，只是soap后来加了一大堆的标准（ws-*），在这些标准对我们来说没有意义，而php和java两边对标准的支持不兼容的时候，soap就成了鸡肋。后来，我们就干脆抛弃了soap，而通过我们自己的程序来发送和接收我们自定义的没有歧义的xml。目前为止，这是两边都乐意使用且很少出问题的方式。这种方式需要两边很好的配合，需要规范化的文档，否则以后只会变得混乱。</p>
<p>我们也曾想过使用基于java的php实现，来达到让java和php在语言级别集成，这种方法对于php遗留系统来说风险太大，而且php程序员面并不乐意生活在java的虚拟机内。也许未来这能成为一个成熟的方案。</p>
<p>正因为使用不同技术的应用之间的集成存在很多的问题，在实际项目中应该尽量避免。</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/162/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>推荐个很不错图形化keytool</title>
		<link>http://www.sulong.info/archives/153</link>
		<comments>http://www.sulong.info/archives/153#comments</comments>
		<pubDate>Tue, 10 Feb 2009 07:36:54 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[keytool]]></category>
		<category><![CDATA[技巧]]></category>

		<guid isPermaLink="false">http://www.sulong.info/archives/153</guid>
		<description><![CDATA[Java的keytool命令实在是太不好用了，尤其是缺少这方面知识的时候。今题找到了一个keytool的图形化前端，非常好用！在这里记下，强烈推荐！ http://yellowcat1.free.fr/keytool_iui.html]]></description>
			<content:encoded><![CDATA[<p>Java的keytool命令实在是太不好用了，尤其是缺少这方面知识的时候。今题找到了一个keytool的图形化前端，非常好用！在这里记下，强烈推荐！</p>
<p>http://yellowcat1.free.fr/keytool_iui.html</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/153/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaEE组件的并发与无状态</title>
		<link>http://www.sulong.info/archives/146</link>
		<comments>http://www.sulong.info/archives/146#comments</comments>
		<pubDate>Mon, 05 Jan 2009 12:22:00 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[技巧]]></category>
		<category><![CDATA[程序]]></category>
		<category><![CDATA[java EE]]></category>
		<category><![CDATA[经验]]></category>

		<guid isPermaLink="false">http://www.sulong.info/?p=146</guid>
		<description><![CDATA[并发与线程安全 串行运行时正确的程序在并发运行时可能会出错，这是由于并发运行的多个任务（进程或线程）之间共享了变量。在Java EE环境下很多的组件最终都是在多线程环境下并发运行的，应该牢记住这一点，避免发生诡异的错误。在上个项目中，我们的程序就出现过诡异的错误，在并发量小的测试环境下，它很少出现，但是随着并发量的增大，它出现的次数增多。经查，发现如下类似代码: &#160; &#160; public class SomeServlet extends HttpServlet &#123; &#160; &#160; &#160; &#160; private SomeType someVariable; &#160; &#160; &#160; &#160; public void doPost&#40;HttpServletRequest req, HttpServletRespons rsp&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; method1&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; method2&#40;&#41;; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; void method1&#40;&#41; &#123; [...]]]></description>
			<content:encoded><![CDATA[<h3>并发与线程安全</h3>
<p>串行运行时正确的程序在并发运行时可能会出错，这是由于并发运行的多个任务（进程或线程）之间共享了变量。在Java EE环境下很多的组件最终都是在多线程环境下并发运行的，应该牢记住这一点，避免发生诡异的错误。在上个项目中，我们的程序就出现过诡异的错误，在并发量小的测试环境下，它很少出现，但是随着并发量的增大，它出现的次数增多。经查，发现如下类似代码:<br/></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeServlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> SomeType someVariable<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doPost<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletRespons rsp<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> method1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006633;">someVariable</span> <span style="color: #339933;">=</span> someValue<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> method2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006633;">someVariable</span> <span style="color: #339933;">=</span> someValue<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>method1() 和method2()都访问到了someVariable，而且程序员的本意是想在method2()使用由method1()产生的 someVariable，但是Servlet可能被多个线程共享，上面的代码不能正常工作。当Servlet容器用thread1和thread2两个线程来服务两个请求request1和request2，而thread1和thread2使用了同一个SomeServlet对象，如果JVM在 SomeServlet在执行到method1()后和method2()前时发生线程间的切换，那么就很可能导致出错，比如：thread1.method1()->thread2.method1()->thread1.method2()->thread2.method2()。实例变量和类变量都是在线程间共享的，而方法内的局部变量和参数是不会被共享的，所以要处理这个问题，最简单的方法是通过方法参数来传递 someVariable，而不是通过实例变量。所以这段程序改成这样就可以了：<br/></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeServlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doPost<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletRespons rsp<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SomeType someVariable <span style="color: #339933;">=</span> method1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method2<span style="color: #009900;">&#40;</span>someVariable <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; SomeType someVariablemethod1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> someValue<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> method2<span style="color: #009900;">&#40;</span>SomeType someVariable<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006633;">someVariable</span> <span style="color: #339933;">=</span> someValue<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>通过synchronized等并发访问控制机制也可以解决这个问题，但是我觉得上面的方式是最直观的。也许有人会说后面的代码比前面的看起来要难看，不够面向对象。这里不讨论怎样才是面向对象，或者面向对象好不好。总之后者是正确的，前者是错误的，没有正确性的程序是没有意义的。<br/></p>
<p>被过线程共享而不会出错的对象，被称之为线程安全的。显然Servlet不是线程安全的，而且还有很多组件也不是线程安全的，比如HttpSession。在上个项目中，我们使用JAXB来处理XML，每次使用时都创建一个JAXBContext对象，后来发现创建JAXBContext的代价是相当的高，于是我们想把它缓存起来在整个应用程序范围内使用，幸好我们使用的JAXBContext的实现是线程安全的，可以放心的被多个线程共享。<br/></p>
<h3>有状态与无状态</h3>
<p>有状态组件是这样一种组件，组件调用者的返回结果会依赖于这个组件之前或正在受到的调用。无状态的组件则相反，调用者的返回结果只和本次调用相关，所有调用者的调用过程不会影响到其他调用者。举例来说，someComponent组件的getLatestCaller()返回上一次的调用者，这个行为的返回值总会和上一次的调用者相关，这样的组件就是有状态的。假如有个组件math有个方法为add(x,y)，返回x+y的值，那么无论之前被哪个调用者调用过，math.add()的返回值只和本次调用的参数相关，这样的组件是无状态的。无状态的组件是线程安全的，因为被别的调用者调用并不会影响到本次的调用结果。</p>
<p>除非有必要，否则应当尽量把你的程序组件实现成无状态的。在spring, seam, ejb里都有无状态的组件。不同的框架在如何实现无状态方面各不相同。最简单的实现方法就是为无状态组件做一个包装，每次调用组件的方法时，都由包装类生成一个新的对象来处理，这样实际上就不再任何的调用者之间共享被调用者的实例，实现了无状态。比如：<br/></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> SomeInterface <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> SomeType doMyBusinness<span style="color: #009900;">&#40;</span>SomeArg arg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeStatlessComponent <span style="color: #000000; font-weight: bold;">implements</span> SomeInterface <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> SomeType doMyBusinness<span style="color: #009900;">&#40;</span>SomeArg arg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;...<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeStatlessComponentWrapper <span style="color: #000000; font-weight: bold;">implements</span> SomeInterface <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> SomeType doMyBusinness<span style="color: #009900;">&#40;</span>SomeArg arg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> SomeStatlessComponent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">doMyBusinness</span><span style="color: #009900;">&#40;</span>arg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>如果实例化组件的代价是昂贵的，用一个对象池缓存组件实例，并在每次从池中取对象时清空对象的状态可能是个更好的办法。其实只要遵循简单的原则，就很容易实现无状态，就是不使用实例变量或类变量，或者只使用只读的实例变量或只读类变量。这样的组件用单例就可以实先无状态。<br/></p>
<p>由于实现上的差别，有些框架根本就不会做过多的努力来保证组件的无状态性，相反他们把责任交给了程序员。作为组件的创作者，如果你确定你要的是无状态的组件，那么只使用只读的实例变量或类变量，是个明智的选择。</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/146/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java EE的不足</title>
		<link>http://www.sulong.info/archives/123</link>
		<comments>http://www.sulong.info/archives/123#comments</comments>
		<pubDate>Sun, 07 Dec 2008 15:10:12 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[java EE]]></category>
		<category><![CDATA[思考]]></category>

		<guid isPermaLink="false">http://www.sulong.info/?p=123</guid>
		<description><![CDATA[Java EE的发布，尤其是ejb3，让我高兴了一把。改进是显而易见的，但是还不够，经过一年的多的使用，我今天忍不住要发发牢骚。 混乱的jndi 把对象绑定到树形结构的目录上，易于查找和使用，这本来是一个非常好的想法，但是偏偏被搞的非常复杂。在同一个服务器内，你能看到的jndi的形式可能有 /a/b, java:a/b,java:com/env/a/b, 甚至a/b。为什么JCP那帮人就不能把这个名称统一一下呢？部署在应用服务器上的组件，可以有一个jndi，但是Java EE偏偏没有定义将组件映射到一个什么样的jndi名字上，于是在glassfish下，默认的数据源的jndi名是java:/jdbc/db，在 jboss下则是java:/db，就算是ejb，在不同的应用服务器上被部署后都会被分配给不同的名字。@Resource可以把jndi资源注入到组件中，但是在引用组件时却可以通过两个名字，如：@Resource(name=&#8221;a/b&#8221;) @Resource(mappedName=&#8221;a/b&#8221;)，好讨厌！什么时候jndi能够被统一起来，java EE在易用性上一定会进一大步！ 半吊子的依赖注入 当我听到可以通过@Resource @EJB注入资源时，我非常高兴，那岂不是意味着Java 应用服务器，一下子就变成了个spring bean容器了？但是最终的结果很令人失望，因为你只能注入有jndi名字的东西，而且只能将它注入到指定的几种组件中，这样的限制一下子把好好的依赖注入给废了。通过jndi去找组件，比通过spring bean的名字找bean要难多了，而且我们确实需要将组件注入到某个普通的类里面。这样的限制最终导致了在实际项目中，很少会用到Java EE本身的依赖注入功能。 classloader的噩梦 相同的一个类，被不同的classloader载入，那么在jvm中这两个类就不是同一个类。classloader的这一机制有它的道理，比如提高安全性，但是会产生一些很奇怪的问题。假设有两个classloader, cl1和cl2，这两个classloader都载入了同一个class，比如 A.class，其中cl1的A.class实例化了一个对象a1, 那么，当你试图把a1 cast成 cl2的A.class的类型的时候，就会发生ClassCastException，尽管cl1的A.class和cl2的A.class是同一个 class。J2ee规定了每个ear内的library必须能被ear内的每个成员共享，容器生成的ejb的stub也必须能在ear内被共享。通常要实现这个功能，只要给这个ear分配一个classloader，并由这个classloader载入需要被整个ear内共享的class，而ear内的 war,和ejb-jar则由这个ear的classloader的子classloader载入。有了这一机制，在ear内部，ejb的local接口可以被正常的使用而不会出现问题。但是如果跨越ear调用ejb的local接口，就会出现诸如ClassloaderException的异常。跨越 ear使用jpa也会遇到类似的问题。Java EE没有给出统一的解决方案，不同的服务器给出了不同的方案，但还没有一个解决方案算是真正的优美。 MDB不能消费远程的消息 ejb2的时代，同样作为ejb，message driven bean 远不像会话bean和实体bean那样有那么多负面评论。MDB确实很好用，唯一的不足就是没有统一和简单的方法可以让mdb监听远程的jms消息。我对此感到非常纳闷，既然Java EE的标准已经统一了那么多，为何不多进一步，也统一一下如何让MDB访问远程JMS？ 或许Java EE标准的制定有太多的政治因素了，那些专家们总是再制造一些半成品。]]></description>
			<content:encoded><![CDATA[<p>Java EE的发布，尤其是ejb3，让我高兴了一把。改进是显而易见的，但是还不够，经过一年的多的使用，我今天忍不住要发发牢骚。</p>
<h2>混乱的jndi</h2>
<p>把对象绑定到树形结构的目录上，易于查找和使用，这本来是一个非常好的想法，但是偏偏被搞的非常复杂。在同一个服务器内，你能看到的jndi的形式可能有 /a/b, java:a/b,java:com/env/a/b, 甚至a/b。为什么JCP那帮人就不能把这个名称统一一下呢？部署在应用服务器上的组件，可以有一个jndi，但是Java EE偏偏没有定义将组件映射到一个什么样的jndi名字上，于是在glassfish下，默认的数据源的jndi名是java:/jdbc/db，在 jboss下则是java:/db，就算是ejb，在不同的应用服务器上被部署后都会被分配给不同的名字。@Resource可以把jndi资源注入到组件中，但是在引用组件时却可以通过两个名字，如：@Resource(name=&#8221;a/b&#8221;) @Resource(mappedName=&#8221;a/b&#8221;)，好讨厌！什么时候jndi能够被统一起来，java EE在易用性上一定会进一大步！</p>
<h2>半吊子的依赖注入</h2>
<p>当我听到可以通过@Resource @EJB注入资源时，我非常高兴，那岂不是意味着Java 应用服务器，一下子就变成了个spring bean容器了？但是最终的结果很令人失望，因为你只能注入有jndi名字的东西，而且只能将它注入到指定的几种组件中，这样的限制一下子把好好的依赖注入给废了。通过jndi去找组件，比通过spring bean的名字找bean要难多了，而且我们确实需要将组件注入到某个普通的类里面。这样的限制最终导致了在实际项目中，很少会用到Java EE本身的依赖注入功能。</p>
<h2>classloader的噩梦</h2>
<p>相同的一个类，被不同的classloader载入，那么在jvm中这两个类就不是同一个类。classloader的这一机制有它的道理，比如提高安全性，但是会产生一些很奇怪的问题。假设有两个classloader, cl1和cl2，这两个classloader都载入了同一个class，比如 A.class，其中cl1的A.class实例化了一个对象a1, 那么，当你试图把a1 cast成 cl2的A.class的类型的时候，就会发生ClassCastException，尽管cl1的A.class和cl2的A.class是同一个 class。J2ee规定了每个ear内的library必须能被ear内的每个成员共享，容器生成的ejb的stub也必须能在ear内被共享。通常要实现这个功能，只要给这个ear分配一个classloader，并由这个classloader载入需要被整个ear内共享的class，而ear内的 war,和ejb-jar则由这个ear的classloader的子classloader载入。有了这一机制，在ear内部，ejb的local接口可以被正常的使用而不会出现问题。但是如果跨越ear调用ejb的local接口，就会出现诸如ClassloaderException的异常。跨越 ear使用jpa也会遇到类似的问题。Java EE没有给出统一的解决方案，不同的服务器给出了不同的方案，但还没有一个解决方案算是真正的优美。</p>
<h2>MDB不能消费远程的消息</h2>
<p>ejb2的时代，同样作为ejb，message driven bean 远不像会话bean和实体bean那样有那么多负面评论。MDB确实很好用，唯一的不足就是没有统一和简单的方法可以让mdb监听远程的jms消息。我对此感到非常纳闷，既然Java EE的标准已经统一了那么多，为何不多进一步，也统一一下如何让MDB访问远程JMS？</p>
<p>或许Java EE标准的制定有太多的政治因素了，那些专家们总是再制造一些半成品。</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/123/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>我不想在重启中浪费生命</title>
		<link>http://www.sulong.info/archives/121</link>
		<comments>http://www.sulong.info/archives/121#comments</comments>
		<pubDate>Thu, 04 Dec 2008 13:21:13 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[开发效率]]></category>

		<guid isPermaLink="false">http://www.sulong.info/?p=121</guid>
		<description><![CDATA[php比java的开发效率高几乎成了大家的共识了。为什么php开发起来会比java快很多呢？频繁的服务重启是众多原因之一。php开发者们编辑完了 php代码，只要刷新一下浏览器，就可以看到运行的效果了，从来没有编译和重启apache的问题。尽管现在的IDE能够自动编译部署和打包应用程序，但 是还是没有php便捷。这一年来一直在使用jboss 4.2 做开发，jboss号称有很强的热部署能力，但是更新一个应用后还是要等上一两分钟的时间让jboss重新部署应用，而且这种热部署时间一长就会发生内存 耗尽的错误。如果是用tomcat+spring这种轻量级解决方案，重启一次十几秒钟，还可以接受，但是jboss+seam要一两分钟，而且吃掉百兆 内存，实在让人受不了。有的时候感觉一天之中没有在专心写多少代码，都是在等待jboss重启中度过。我现在对这种工作方式越来越不满，我想要更好性能的 开发机器，使用更轻量的框架，更轻量的服务器。现在使用的seam相对spring+hibernate+struts来说，还是重量的多，而jboss 也没有tomcat那样轻巧。如果能够不用等待就可以看到程序运行的效果，java在开发效率上会离php更近一步。 jboss的热部署，是指不重启jboss的情况下部署应用，比如ear, war等。虽然不重启jboss，但是还是要卸载和装载应用，还是慢。 如何热部署jboss应用呢？如果应用是用打包方式部署的，更新一下包的修改时间就可以了。如果是以目录的方式部署的，对于ear，更新一下META-INF/application.xml的修改时间，对于war，则更新WEB-INF/web.xml的修改时间。更新修改时间，最方便的就是用touch命令了，如果你用的是windows，没有touch命令，你可以装一个cygwin，或者用ant的touch任务来代替。]]></description>
			<content:encoded><![CDATA[<p>php比java的开发效率高几乎成了大家的共识了。为什么php开发起来会比java快很多呢？频繁的服务重启是众多原因之一。php开发者们编辑完了 php代码，只要刷新一下浏览器，就可以看到运行的效果了，从来没有编译和重启apache的问题。尽管现在的IDE能够自动编译部署和打包应用程序，但 是还是没有php便捷。这一年来一直在使用jboss 4.2 做开发，jboss号称有很强的热部署能力，但是更新一个应用后还是要等上一两分钟的时间让jboss重新部署应用，而且这种热部署时间一长就会发生内存 耗尽的错误。如果是用tomcat+spring这种轻量级解决方案，重启一次十几秒钟，还可以接受，但是jboss+seam要一两分钟，而且吃掉百兆 内存，实在让人受不了。有的时候感觉一天之中没有在专心写多少代码，都是在等待jboss重启中度过。我现在对这种工作方式越来越不满，我想要更好性能的 开发机器，使用更轻量的框架，更轻量的服务器。现在使用的seam相对spring+hibernate+struts来说，还是重量的多，而jboss 也没有tomcat那样轻巧。如果能够不用等待就可以看到程序运行的效果，java在开发效率上会离php更近一步。</p>
<p>jboss的热部署，是指不重启jboss的情况下部署应用，比如ear, war等。虽然不重启jboss，但是还是要卸载和装载应用，还是慢。</p>
<p>如何热部署jboss应用呢？如果应用是用打包方式部署的，更新一下包的修改时间就可以了。如果是以目录的方式部署的，对于ear，更新一下META-INF/application.xml的修改时间，对于war，则更新WEB-INF/web.xml的修改时间。更新修改时间，最方便的就是用touch命令了，如果你用的是windows，没有touch命令，你可以装一个cygwin，或者用ant的touch任务来代替。</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/121/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Console的写操作在windows和linux下的性能差异</title>
		<link>http://www.sulong.info/archives/77</link>
		<comments>http://www.sulong.info/archives/77#comments</comments>
		<pubDate>Wed, 09 Apr 2008 04:43:09 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[性能]]></category>

		<guid isPermaLink="false">http://www.sulong.info/archives/77</guid>
		<description><![CDATA[在做项目的时候，为了调试方便，把很多日志信息通过console打印出来。每次进行一个操作，输出的日志非常多，导致程序看起来非常慢。难道向控制台打印信息就这么慢吗？我突然想到以前用linux时好像没有这么慢，于是就做了一个简单的测试。测试程序非常简单，就是用System.out.println()向控制台写一个字符串，写十万次。在我的windows xp和ubuntu 8.04下分别测试了10次，结果发现，在linux下平均用时9.8秒，而windows下要用18.6秒，差不多是linux的两倍。两边用的都是sun的1.6版的虚拟机，没想到会有这么大的差异。 是不是java在linux上就比在windows上要快呢？我想找一些更全面，更权威的测试，但是没有找到。sun的官方论坛上，有人说在两个系统上的表现是差不多的，不知道他是从哪里得到的结论。通过这个简单的测试，至少可以看出，在x86平台上，同是sun 1.6的虚拟机，写控制台的时候，在windows xp上，比在ubunut 8.04上要慢一倍]]></description>
			<content:encoded><![CDATA[<p>在做项目的时候，为了调试方便，把很多日志信息通过console打印出来。每次进行一个操作，输出的日志非常多，导致程序看起来非常慢。难道向控制台打印信息就这么慢吗？我突然想到以前用linux时好像没有这么慢，于是就做了一个简单的测试。测试程序非常简单，就是用System.out.println()向控制台写一个字符串，写十万次。在我的windows xp和ubuntu 8.04下分别测试了10次，结果发现，在linux下平均用时9.8秒，而windows下要用18.6秒，差不多是linux的两倍。两边用的都是sun的1.6版的虚拟机，没想到会有这么大的差异。</p>
<p>是不是java在linux上就比在windows上要快呢？我想找一些更全面，更权威的测试，但是没有找到。sun的官方论坛上，有人说在两个系统上的表现是差不多的，不知道他是从哪里得到的结论。通过这个简单的测试，至少可以看出，在x86平台上，同是sun 1.6的虚拟机，写控制台的时候，在windows xp上，比在ubunut 8.04上要慢一倍</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/77/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>震惊！微软收购SpringSource！</title>
		<link>http://www.sulong.info/archives/75</link>
		<comments>http://www.sulong.info/archives/75#comments</comments>
		<pubDate>Tue, 01 Apr 2008 09:36:20 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[生活]]></category>
		<category><![CDATA[程序]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://www.sulong.info/archives/75</guid>
		<description><![CDATA[突然看到消息，SpringSource宣布被微软收购，非常震惊！ 这是真的吗？ 今天是个西方特殊的日子，目前还不能肯定是不是真的。但是，微软完全有动机这样做。微软提出.net已有数年，但是仍旧不无法动摇java在企业开发界的地位。我对.net没有深入的研究，但是.net界远没有像java界那样百家争鸣，思想活跃是不争的事实。当用java开发有无数的方案可选时，.net世界里基本上只有MS自己的解决方案一家。sping是Java世界里最丰硕的果实，虽然spring也有.net的版本，但是一直没有被足够支持和发扬。微软收购Spring，一下子就把java世界的果实摘了去。而且，微软借此收购，还可以把java界的牛人Rod Johnson收入麾下，这颗脑袋比spring的源代码还要重要。 我很不希望这是真的。我担心johnson被拉入微软后，以后spring在java界的发展变慢，甚至于停滞。但愿这不是真的！]]></description>
			<content:encoded><![CDATA[<p>突然看到消息，<a href="http://www.infoq.com/cn/news/2008/04/microsoft-springsource-purchase">SpringSource宣布被微软收购</a>，非常震惊！ 这是真的吗？<br />
今天是个西方特殊的日子，目前还不能肯定是不是真的。但是，微软完全有动机这样做。微软提出.net已有数年，但是仍旧不无法动摇java在企业开发界的地位。我对.net没有深入的研究，但是.net界远没有像java界那样百家争鸣，思想活跃是不争的事实。当用java开发有无数的方案可选时，.net世界里基本上只有MS自己的解决方案一家。sping是Java世界里最丰硕的果实，虽然spring也有.net的版本，但是一直没有被足够支持和发扬。微软收购Spring，一下子就把java世界的果实摘了去。而且，微软借此收购，还可以把java界的牛人Rod Johnson收入麾下，这颗脑袋比spring的源代码还要重要。<br />
我很不希望这是真的。我担心johnson被拉入微软后，以后spring在java界的发展变慢，甚至于停滞。但愿这不是真的！</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/75/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>更改jboss默认使用的rmi端口，避免冲突</title>
		<link>http://www.sulong.info/archives/71</link>
		<comments>http://www.sulong.info/archives/71#comments</comments>
		<pubDate>Wed, 27 Feb 2008 06:12:41 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[jboss]]></category>

		<guid isPermaLink="false">http://www.sulong.info/archives/71</guid>
		<description><![CDATA[最近使用jboss的时候常常出现1098端口被占，不能正常起动的错误: 10:28:57,000 WARN  [ServiceController] Problem starting service jboss:service=Naming java.rmi.server.ExportException: Port already in use: 1098; nested exception is: java.net.BindException: Address already in use: JVM_Bind at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:310) at sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:218) 也不知道是哪个程序占用了这个端口。可以通过修改jboss的配置文件，让jboss使用另外一个端口，来修正这个问题。方法如下： 1，打开jboss的jboos-service.xml文件，在我机上位于D:\prog\jboss-4.2.2.GA\server\default\conf 目录。 2，搜索1098，找到 &#60;attribute name=&#8221;RmiPort&#8221;&#62;1098&#60;/attribute&#62;，修改这个端口号为一个一般不会被用到的端口号，比如12345。 再重启jboss应该就OK了。]]></description>
			<content:encoded><![CDATA[<p>最近使用jboss的时候常常出现1098端口被占，不能正常起动的错误:</p>
<p>10:28:57,000 WARN  [ServiceController] Problem starting service jboss:service=Naming<br />
java.rmi.server.ExportException: Port already in use: 1098; nested exception is:<br />
java.net.BindException: Address already in use: JVM_Bind<br />
at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:310)<br />
at sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:218)</p>
<p>也不知道是哪个程序占用了这个端口。可以通过修改jboss的配置文件，让jboss使用另外一个端口，来修正这个问题。方法如下：</p>
<p>1，打开jboss的jboos-service.xml文件，在我机上位于D:\prog\jboss-4.2.2.GA\server\default\conf 目录。</p>
<p>2，搜索1098，找到 &lt;attribute name=&#8221;RmiPort&#8221;&gt;1098&lt;/attribute&gt;，修改这个端口号为一个一般不会被用到的端口号，比如12345。</p>
<p>再重启jboss应该就OK了。</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/71/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AOP简介，理解AOP</title>
		<link>http://www.sulong.info/archives/70</link>
		<comments>http://www.sulong.info/archives/70#comments</comments>
		<pubDate>Thu, 21 Feb 2008 02:18:14 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[程序]]></category>
		<category><![CDATA[AOP]]></category>
		<category><![CDATA[思考]]></category>

		<guid isPermaLink="false">http://www.sulong.info/archives/70</guid>
		<description><![CDATA[AOP简介 一． AOP字面意思 AOP = Aspect Oriented Programming 中文意为： 面向切面编程 要掌握AOP就得从aspect，即切面，入手，理解什么是切面，为什么要切面，如何使用切面。 二． 没有AOP的日子 OOP = Object Oriented Programming 面向对象的编程。 兴起于上个世纪九十年代的面向对象编程理念，仍旧是现在编程思想的主流。面向对象的核心思想是抽像问题域的模型成对象，用程序去模拟对象以解决问题。但是在软件系统中还存在着一些问题，它们不易于，或者不适合于用面向对象地方式来解决，比如事务，安全，日志等。看下面： public class Tool1 &#123; &#160; &#160; public void doSomething&#40;&#41; &#123; &#160; &#160; &#160; &#160; System.out.println&#40;&#34;I am doing...&#34;&#41;; &#160; &#160; &#125; &#125; public class Tool2 &#123; &#160; &#160; public void doSomething &#40;&#41; &#123; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>AOP简介</p>
<h3>一． AOP字面意思</h3>
<p>AOP = Aspect Oriented Programming 中文意为： 面向切面编程</p>
<p>要掌握AOP就得从aspect，即切面，入手，理解什么是切面，为什么要切面，如何使用切面。</p>
<h3>二． 没有AOP的日子</h3>
<p>OOP = Object Oriented Programming 面向对象的编程。</p>
<p>兴起于上个世纪九十年代的面向对象编程理念，仍旧是现在编程思想的主流。面向对象的核心思想是抽像问题域的模型成对象，用程序去模拟对象以解决问题。但是在软件系统中还存在着一些问题，它们不易于，或者不适合于用面向对象地方式来解决，比如事务，安全，日志等。看下面：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tool1 <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am doing...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tool2 <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am do some other things...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Tool1 t1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tool1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Tool2 t2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tool2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; t1.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; t2. <span style="color: #006633;">doSomething</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>例1</strong></p>
<p>这个程序很简单，容易看得懂。现在新的要求来了，要求在执行Tool1和Tool2的两个方法前后说一句话。如果用过程式的编程方法，我们只要改一下main方法，让它变成这样，看例2：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Tool1 t1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tool1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Tool2 t2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tool2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Going to do something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; t1.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Did something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Going to do something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; t2. <span style="color: #006633;">doSomething</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Did something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>例2</strong></p>
<p>很好，实现目标了，但这存在以下问题：</p>
<ul>
<li> 如果还有其它不是main的调用者使用Tool1和Tool2，我们需要改动所有调用者代码</li>
<li> 完成同样的事情却将代码散落在多个地方，复制粘贴不是一个编程好习惯</li>
</ul>
<p>下面我们来解决这两个问题。为了解决第一个问题，我们可以把代码改动由调用者转移到被调用者里，如：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tool1 <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Going to do something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am doing...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Did something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tool2 <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Going to do something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am do some other things...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Did something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Tool1 t1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tool1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Tool2 t2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tool2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; t1.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; t2. <span style="color: #006633;">doSomething</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>例3</strong></p>
<p>好，这样就不需要改变第个调用者的代码了。但是为了实现同样的功能，还是用了太多的代码，好，我们利用伟大的面向对向的能力吧！</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> abstractTool <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Going to do something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doSomethingReally<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Did something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000066; font-weight: bold;">void</span> doSomethingReally<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tool1 <span style="color: #000000; font-weight: bold;">extends</span> abstractTool <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomethingReally<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am doing...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tool2 <span style="color: #000000; font-weight: bold;">extends</span> abstractTool <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomethingReally<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am do some other things...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Tool1 t1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tool1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Tool2 t2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tool2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; t1.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; t2.<span style="color: #006633;">doSomething</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>例4</strong></p>
<p>好了，这样实现新功能的代码被集在了一个抽象类的方法里了，没有在散落开来，易于维护了。这样的方案是不是就是完美的了呢？不，还不够，它存在着一个严重问题，</p>
<ul>
<li> 必须得修改被调用者的代码</li>
<li> 强迫被调用者继承于某个类，这样它将不能再继承其它类</li>
</ul>
<p>这样做显然很不灵活，假如还有Tool3 Tool4 Tooln…，它们有一些需要这样的功能，有一些不需要，甚至于有时有一些需要，有时有一些不需要，怎么办？我们只好每次变动时都做这样的修改，那太痛苦了。好吧，还有更好的方案吗？也许一开始设计得就有些问题吧？嗯，是的，我们采用工厂模式吧，面向接口编程吧！于是就有了下面的代码：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Tool <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomeThing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tool1 <span style="color: #000000; font-weight: bold;">implements</span> Tool <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomethingReally<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am doing...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tool2 <span style="color: #000000; font-weight: bold;">implements</span> Tool <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomethingReally<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am do some other things...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ToolProxy <span style="color: #000000; font-weight: bold;">implements</span> Tool<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Tool tool<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> ToolProxy<span style="color: #009900;">&#40;</span>tool<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">tool</span> <span style="color: #339933;">=</span>tool<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Going to do something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">tool</span>.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Did something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ToolFactory <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Tool getTool<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> type<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tool1&quot;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ToolProxy<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Tool1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tool2&quot;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ToolProxy<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Tool2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Tool t1 <span style="color: #339933;">=</span> ToolFactory.<span style="color: #006633;">getTool</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tool1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Tool t2 <span style="color: #339933;">=</span> ToolFactory.<span style="color: #006633;">getTool</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tool2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; t1.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; t2.<span style="color: #006633;">doSomething</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>例5</strong></p>
<p>接口使被调用者不再需要继承于某个类，也使得调用者和被调用者之间解耦。工厂负责组装类，我们可以在工厂里实现通过xml来配置等功能。ToolProxy代理类把新功能性代码集中在一起，便于管理。可惜这个方案还是有些问题的，假如我们不能改动被调用代码，不能强制人家实现某个接口呢？好吧，我们再使用一点花招吧，看下面代码：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Tool <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomeThing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tool1Wrapper <span style="color: #000000; font-weight: bold;">implements</span> Tool <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Tool1 tool1<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Tool1Wrapper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tool1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tool1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tool1.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tool2Wrapper <span style="color: #000000; font-weight: bold;">implements</span> Tool <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Tool2 tool2<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Tool2Wrapper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tool2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tool2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tool2.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tool1<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am doing...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tool2<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am do some other things...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ToolProxy <span style="color: #000000; font-weight: bold;">implements</span> Tool<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Tool tool<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> ToolProxy<span style="color: #009900;">&#40;</span>tool<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">tool</span> <span style="color: #339933;">=</span>tool<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Going to do something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">tool</span>.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Did something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ToolFactory <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Tool getTool<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> type<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tool1&quot;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ToolProxy<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Tool1Wrapper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tool2&quot;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ToolProxy<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Tool2Wrapper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Tool t1 <span style="color: #339933;">=</span> ToolFactory.<span style="color: #006633;">getTool</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tool1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Tool t2 <span style="color: #339933;">=</span> ToolFactory.<span style="color: #006633;">getTool</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tool2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; t1.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; t2.<span style="color: #006633;">doSomething</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>例6</strong></p>
<p>当你看完这段代码后，如果你想要抓狂，那就抓吧，我能够理解。为了实现这点新功能，我们做了如此大的努力！是不是这样就结束了呢？这就是最终的完美方案了吗？显然还不够：</p>
<ul>
<li> 代码太长</li>
<li> 如果有多个类，就需要写多个Wrapper类</li>
<li> 如果有多个接口，就需要写多个Proxy类</li>
</ul>
<p>如果这样的代码就能够让你满意，看来你不是足够懒惰的程序员，你很勤快，愿意为实现这样的功能敲打出一堆代码。但是伟大的懒惰程序员们并不没有停下来。</p>
<h3>三． AOP是对OOP的补充</h3>
<p>当写完上面的代码后，我们该静下来想一想，我们到底想要对程序做什么，为什么会这样麻烦呢？</p>
<p>我们想要什么？我们想要就是能在程序的调用者和被调用者之间切开一个面，加入新的功能！上面的例子，我们就是想在调用Tool1和Tool2的doSomething()方法时，将调用过程切开一个面，分别在进入和出来时放入两句代码而已。而现在的程序语言恰恰缺少这样把调用过程切开的能力，所以我们需要新的语言，新的能力！这个编程的思想就是AOP，这种能力就是AOP的能力，能够这样做的语言就是AOP的语言。</p>
<p>AOP是对OOP的一个补充，正是OOP缺少了这样的能力，而人们又有这样的需要，所以才会有AOP。AOP并不能代替OOP，正如OOP不能代替过程式编程一样。</p>
<h3>四． 理解AOP的语义</h3>
<p>AOP中最关键的就是aspect，即切面。切面是以前的语言没有定义的新的概念。要理解切面，就要理解切面几个要素：</p>
<p>1. 切谁 即，对谁的调用会被切开，我们想要加入的新的功能正是针对于这个被切的对象的。</p>
<p>2. 切什么地方 被调用者会可能有多个被外部访问的出入口，为了实现我们的功能，需要被切的那个出入口。</p>
<p>3. 添加什么样的功能 将程序切开，目的是在被切开的地方加入新的功能。</p>
<p>理想状况下，所有的对象都能被切，不管这个对象是什么类型的，是不是final的，是不是nested的等；对象的所有地方都能切，不管是在构建时，调用方法时，调用属性时，调用前，调用后等；任意功能都能添加，切开之后，做什么都可以。</p>
<p>AOP有各种不同的实现，不同实现的能力之间的区别就可以通过上面的三条来衡量。功能最强的AspectJ，可以对任意对象的创建，方法调用，属性调用时做切面，加入任意功能。但是一般情况下我们不需要那么强的能力，能够实现对象的方法调用的切入，就已经够用了，spring里大量使用了这样的能力。</p>
<p>为了使用的方便，一般对方法的切入，分为以下几种：</p>
<p>1. Before 在方法调用前执行，不能控制返回值</p>
<p>2. After 在方法调用后执行，可以控制返回值</p>
<p>3. Throw 在方法异常退出时执行</p>
<p>4. Around 兼有以上三种的能力</p>
<h3>五． AOP 实现原理举例</h3>
<p>实现AOP有很多种不同方法，大体上可以分为三类：</p>
<h4>1. 修改由源程序生的class文件，加入新功能</h4>
<p>这一类的代表是AspectJ项目。它在java语言的基础上添加的新的语言和语法。AspectJ有自己的编译器，它会在调用java的编译器生成class文件后，修改class文件。这种方法实现的AOP是最高效的，因为功能的添加是在编译器就加入了，也是功能最强的，所有的地方都可以切入，缺点是需要学习新的语法和需要额外的编译步骤。</p>
<h4>2. 动态生成类的子类，通过方法覆盖来添加新的功能</h4>
<p>Spring使用cglib在运行时生成类的子类来实现一部分AOP。比如对于上面例子中的Tool1类，spring可以在运行时动态生成一个新的类，类似于:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tool1$<span style="color: #cc66cc;">4535354</span> <span style="color: #000000; font-weight: bold;">extends</span> Tool1<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Going to do something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Did something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>例7</strong></p>
<p>这种方法需要被调用对象的类是可以被继承的，方法是可以被覆盖的，还好这一条基本上都能满足，因为大部情况下我们面对的都是普通的java bean。</p>
<h4>3. 借助于接口和动态代理类，添加新功能。</h4>
<pre>Spring在处理带有接口的类的时候采用这种方式。JDK里有一个类java.lang.reflect.Proxy 可以动态地生</pre>
<pre>成一个实现了某个接口的代理类。例如，spring实现了一个类似于下面的类：</pre>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ProxyFactory <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> getProxy<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> interfaces,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> proxy <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aproxy+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Proxy</span></a>.<span style="color: #006633;">newProxyInstance</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; target.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getClassLoader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaces,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> MyInvocationHandler<span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> proxy<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> MyInvocationHandler <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> target<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> MyInvocationHandler<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">target</span> <span style="color: #339933;">=</span> target<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> invoke<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> proxy, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amethod+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Method</span></a> method, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Athrowable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Throwable</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Going to do something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> o <span style="color: #339933;">=</span> method.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>target, args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Did something...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> o<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<pre></pre>
<pre>进一步的，我们还可以修改MyInvocationHanlder使其动态化，在invoke方法里执行抽像化的before after</pre>
<pre>around throw 等建议，这里就写了。有了这个动态代理类，我们就可以不用实现很多的Proxy类了。</pre>
<h3>六． AOP的标准</h3>
<p>为了能够统一对AOP使用的术语和API，国际上出现了AOP相关的组织，比如项目<strong>aopalliance</strong>等。Spring在使用的aop时就采用这个项目制定的api。关于标准的内容，可以自行去查看相关资料。</p>
<h3>七． 在spring中使用AOP</h3>
<p>Spring里大量使用AOP，为了能更简单地表达AOP，可以通过xml或annotation对AOP进行描述。详细请看spring的手册。</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tx:advice</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;daoTxAdvice&quot;</span> <span style="color: #000066;">transaction-manager</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tx:attributes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tx:method</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;find*&quot;</span> <span style="color: #000066;">read-only</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">propagation</span>=<span style="color: #ff0000;">&quot;REQUIRED&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tx:method</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;check*&quot;</span> <span style="color: #000066;">read-only</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">propagation</span>=<span style="color: #ff0000;">&quot;REQUIRED&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tx:method</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;*&quot;</span> <span style="color: #000066;">propagation</span>=<span style="color: #ff0000;">&quot;REQUIRED&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tx:method<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tx:method<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;aop:config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;aop:pointcut</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;daoOperation&quot;</span> <span style="color: #000066;">expression</span>=<span style="color: #ff0000;">&quot;execution(public * com.**.dao.*Dao.*(..))&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/aop:pointcut<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;aop:advisor</span> <span style="color: #000066;">advice-ref</span>=<span style="color: #ff0000;">&quot;daoTxAdvice&quot;</span> <span style="color: #000066;">pointcut-ref</span>=<span style="color: #ff0000;">&quot;daoOperation&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/aop:advisor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;aop:aspect</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;permissionCheckBean&quot;</span> <span style="color: #000066;">order</span>=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;aop:pointcut</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;permissionCheckPointCut&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/aop:pointcut<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>expression=&quot;execution(public String com.**.action.*Action.execute*()) &quot; /<span style="color: #ddbb00;">&amp;gt;</span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;aop:around</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;checkPermission&quot;</span> <span style="color: #000066;">arg-names</span>=<span style="color: #ff0000;">&quot;pjp &quot;</span> <span style="color: #000066;">pointcut-ref</span>=<span style="color: #ff0000;">&quot;permissionCheckPointCut&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/aop:around<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/aop:aspect<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/aop:config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tx:method<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/tx:attributes<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/tx:advice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p><strong>例9</strong></p>
<p>上面的配置中&lt;aop:pointcut&gt;标签定义了一个bean,它能够说明切什么对象的什么地方。比如，expression=&#8221;execution(public String com.**.action.*Action.execute*()) and @annotation(requirePermission) 说明在对方公开的反回值为String的，包名符使表达式com.liba.**.action的，类名符合*Action的，方法名以execute开头的，无参数的，这样的方法进行切入。然后&lt;aop:around&gt;标签则表达了，对于上面那样的方法以around方式切入，切入后，在切开的地方执行permissionCheckBean的 checkPermission方法。</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/70/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>当return遇到finally</title>
		<link>http://www.sulong.info/archives/58</link>
		<comments>http://www.sulong.info/archives/58#comments</comments>
		<pubDate>Mon, 24 Dec 2007 02:09:05 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[技巧]]></category>

		<guid isPermaLink="false">http://www.sulong.info/archives/58</guid>
		<description><![CDATA[猜猜下面java程序分别会有什么样的输出？ 程序: public class Test &#123; &#160; &#160; public static void main&#40;String&#91;&#93; args&#41; &#123; &#160; &#160; &#160; &#160; System.out.println&#40;&#34;=============test1==================&#34;&#41;; &#160; &#160; &#160; &#160; System.out.println&#40;test1&#40;&#41;&#41;; &#160; &#160; &#160; &#160; System.out.println&#40;&#34;===============================&#34;&#41;; &#160; &#160; &#160; &#160; System.out.println&#40;&#34;=============test1_1==================&#34;&#41;; &#160; &#160; &#160; &#160; System.out.println&#40;test1_1&#40;&#41;&#41;; &#160; &#160; &#160; &#160; System.out.println&#40;&#34;===============================&#34;&#41;; &#160; &#160; &#160; &#160; System.out.println&#40;&#34;\n============test2===================&#34;&#41;; &#160; &#160; &#160; &#160; System.out.println&#40;test2&#40;&#41;&#41;; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>猜猜下面java程序分别会有什么样的输出？</p>
<p>程序:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;=============test1==================&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;===============================&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;=============test1_1==================&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test1_1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;===============================&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>============test2===================&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;===============================&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>============test2_1===================&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test2_1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;===============================&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>============test3===================&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test3<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;===============================&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>============test3_1===================&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test3_1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;===============================&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> test1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> a <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;in try&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> e <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;in finally&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;do finally&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> test1_1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> a <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;in try&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> e <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;in finally&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;do finally&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> test2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> a <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> e <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;do finally&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> test2_1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> a <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> e <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;do finally&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Helper test3<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Helper a <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Helper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; a.<span style="color: #006633;">a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> e <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.<span style="color: #006633;">a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;do finally&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Helper test3_1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Helper a <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Helper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; a.<span style="color: #006633;">a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> e <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.<span style="color: #006633;">a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;do finally&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> Helper <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> a<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>结果：</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">=============test1==================<br />
do finally<br />
in try<br />
===============================<br />
=============test1_1==================<br />
do finally<br />
in finally<br />
===============================<br />
<br />
============test2===================<br />
do finally<br />
1<br />
===============================<br />
<br />
============test2_1===================<br />
do finally<br />
2<br />
===============================<br />
<br />
============test3===================<br />
do finally<br />
2<br />
===============================<br />
<br />
============test3_1===================<br />
do finally<br />
2<br />
===============================</div></div>
<p>结论：</p>
<ol>
<li>在try catch块里return的时候，finally也会被执行。</li>
<li>return 语句会把后面的值复制到一份用来返回，如果return的是基本类型的，finally里对变量的改动将不起效果，如果return 的是引用类型的，改动将可以起效果。</li>
<li>finally里的return语句会把try catch块里的return语句效果给覆盖掉。</li>
</ol>
<p>看来return语句并不一定都是函数的出口，执行return时，只是把return后面的值复制了一份到返回值变量里去了。看来最佳实践是：</p>
<ol>
<li><strong>最好把return放到方法尾而不要在try cath 里return</strong></li>
<li><strong>不要在try catch块和finally块里都包含return</strong></li>
<li><strong>如果在try catch块里return,  则不要在finally块里操作被return的变量</strong></li>
</ol>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/58/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>对使用annotation的一些反思</title>
		<link>http://www.sulong.info/archives/49</link>
		<comments>http://www.sulong.info/archives/49#comments</comments>
		<pubDate>Wed, 05 Dec 2007 14:50:26 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[annotation]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.sulong.info/archives/49</guid>
		<description><![CDATA[Annotation是java 5加入的新功能，可以加在java代码的类，域，方法和参数上，丰富程序语义。我一度认为引入annotation后，可以轻松地抛弃html，但是现在我发现annotation也有不好的地方： 侵入java代码，被标注代码会对标注产生依赖，比如，如果在类里加了javax.persistence.Entity标注，这意味着编译时得把javax.persistence.Entity所在包加入类路径，即使你并不实际使用这个标注。如果编译期不检测，到运行期才寻找Annotation定义的话，会好很多。xml则不会对代码有任何入侵。 需要重新编译才能升效。用XML时，只要更新一下XML重部署，现在是要把java代码也得重新编译。 要配置的信息很多时，多个annotation加在同一个目标上，格式很难排，不易读。 配置信息散在文档各个地方，不易于查找。 我觉得，使用Annotation时，最好要想清楚，你的代码是不是不会迁移到其它坏境，和annotation的耦合也是没问题可以接受的，否则，不要用annotation了。如果真的要用，最好只用annotation来做标注，但不要来做“配置”。比方说，如果你在用一个标注来配合做权限控制，在调用被标注的方法前，做一些权限的检测。那么，最好只在方法前的标注里说明这个方法需要被做权限检测，但不要说明需要被做怎么样的权限检测，例如下面的这个就很好， @NeedPermission&#40;methodName=&#34;writePost&#34;&#41; 但是下面这个就不好 @NeedPermission&#40;methodName=&#34;writePost&#34;, permission=&#34;read,write,admin&#34; condition=&#34;and&#34;&#41; 上面的写法，暴露了太多的细节（配置信息），而这些信息很可能会随着业务变化而变动。我觉得，把配置的细节统一存放到一个XML中，再用annotation来定义这些配置应该应用到哪里。]]></description>
			<content:encoded><![CDATA[<p>Annotation是java 5加入的新功能，可以加在java代码的类，域，方法和参数上，丰富程序语义。我一度认为引入annotation后，可以轻松地抛弃html，但是现在我发现annotation也有不好的地方：</p>
<ol>
<li>侵入java代码，被标注代码会对标注产生依赖，比如，如果在类里加了javax.persistence.Entity标注，这意味着编译时得把javax.persistence.Entity所在包加入类路径，即使你并不实际使用这个标注。如果编译期不检测，到运行期才寻找Annotation定义的话，会好很多。xml则不会对代码有任何入侵。</li>
<li>需要重新编译才能升效。用XML时，只要更新一下XML重部署，现在是要把java代码也得重新编译。</li>
<li>要配置的信息很多时，多个annotation加在同一个目标上，格式很难排，不易读。</li>
<li>配置信息散在文档各个地方，不易于查找。</li>
</ol>
<p>我觉得，使用Annotation时，最好要想清楚，你的代码是不是不会迁移到其它坏境，和annotation的耦合也是没问题可以接受的，否则，不要用annotation了。如果真的要用，最好只用annotation来做标注，但不要来做“配置”。比方说，如果你在用一个标注来配合做权限控制，在调用被标注的方法前，做一些权限的检测。那么，最好只在方法前的标注里说明这个方法需要被做权限检测，但不要说明需要被做怎么样的权限检测，例如下面的这个就很好，</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@NeedPermission<span style="color: #009900;">&#40;</span>methodName<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;writePost&quot;</span><span style="color: #009900;">&#41;</span></div></div>
<p>但是下面这个就不好</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@NeedPermission<span style="color: #009900;">&#40;</span>methodName<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;writePost&quot;</span>, permission<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;read,write,admin&quot;</span> condition<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;and&quot;</span><span style="color: #009900;">&#41;</span></div></div>
<p>上面的写法，暴露了太多的细节（配置信息），而这些信息很可能会随着业务变化而变动。我觉得，把配置的细节统一存放到一个XML中，再用annotation来定义这些配置应该应用到哪里。 </p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/49/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
