<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Five 's Weblog &#187; proxy</title>
	<atom:link href="http://powerdream5.wordpress.com/tag/proxy/feed/" rel="self" type="application/rss+xml" />
	<link>http://powerdream5.wordpress.com</link>
	<description>It is not just another blog! 伍行天下！</description>
	<lastBuildDate>Wed, 28 Nov 2007 21:21:29 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='powerdream5.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/b83f3cb0955eab114902a009525db137?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Five 's Weblog &#187; proxy</title>
		<link>http://powerdream5.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://powerdream5.wordpress.com/osd.xml" title="Five &#8217;s Weblog" />
		<item>
		<title>Applying Java reflection to Ioc and AOP (2)</title>
		<link>http://powerdream5.wordpress.com/2007/10/08/applying-java-reflection-to-ioc-and-aop-2/</link>
		<comments>http://powerdream5.wordpress.com/2007/10/08/applying-java-reflection-to-ioc-and-aop-2/#comments</comments>
		<pubDate>Mon, 08 Oct 2007 03:30:55 +0000</pubDate>
		<dc:creator>powerdream5</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[Ioc]]></category>
		<category><![CDATA[java reflection]]></category>
		<category><![CDATA[proxy]]></category>

		<guid isPermaLink="false">http://powerdream5.wordpress.com/2007/10/08/applying-java-reflection-to-ioc-and-aop-2/</guid>
		<description><![CDATA[        Today, let&#8217;s continue the example of applying java reflection to AOP(Aspect-oriented programming). Because the core of AOP is proxy, I want to make a brief description of it firstly.
        The two important tasks of proxy are interface implementation and delegation. Interface implementation means the proxy class need to implement all the interfaces the target [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=powerdream5.wordpress.com&blog=1840509&post=22&subd=powerdream5&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://powerdream5.files.wordpress.com/2007/10/blog.JPG" title="blog.JPG"></a><a href="http://powerdream5.files.wordpress.com/2007/10/blogcmd.JPG" title="blogcmd.JPG"></a><a href="http://powerdream5.files.wordpress.com/2007/10/xiaoran.jpg" title="xiaoran.jpg"></a>        Today, let&#8217;s continue the example of applying java reflection to AOP(Aspect-oriented programming). Because the core of AOP is proxy, I want to make a brief description of it firstly.<br />
        The two important tasks of proxy are interface implementation and delegation. Interface implementation means the proxy class need to implement all the interfaces the target class implements, and delegation means the proxy will catch all the calls to the target class, so that it is easy to add functions before or after the target methods.</p>
<p><a href="http://powerdream5.files.wordpress.com/2007/10/blog.JPG" title="blog.JPG"></a><a href="http://powerdream5.files.wordpress.com/2007/10/blog.JPG" title="blog.JPG"></a><a href="http://powerdream5.files.wordpress.com/2007/10/blog.JPG" title="blog.JPG"></a></p>
<p style="text-align:center;"><img src="http://powerdream5.files.wordpress.com/2007/10/blog.JPG" alt="blog.JPG" /></p>
<p>           <strong><em>java.lang.reflect.Proxy</em></strong> helps us create a proxy of the target class dynamically, and <strong><em>java.lang.reflect.InvocationHandler</em></strong> interface will allow the proxy class delegate the calls to the target class.<br />
          The following example shows how to use proxy to realize the function of logging. By extracting the function of logging from the method, we can concentrate our method in business logic.</p>
<p><font color="#ff0000">//TargetInterface.java<br />
//Define the interface the target class implements</font><br />
public interface TargetInterface{<br />
     public void print();<br />
}</p>
<p><font color="#ff0000">//TargetImplementation.java<br />
//The class implements the interface</font><br />
public class TargetImplementation implements TargetInterface{<br />
    public void print(){<br />
          System.out.println(&#8220;print method&#8221;);<br />
    }<br />
}</p>
<p><font color="#ff0000">//ProxyTarget.java<br />
//Create the proxy class and delegate the calls to the target class</font></p>
<p>import java.lang.reflect.*;<br />
public class ProxyTarget implements InvocationHandler{<br />
      public static Object createProxy(Object obj){<br />
            <font color="#ff0000">//creat the proxy class according the instance of the target class</font>                 <br />
            return Proxy.newProxyInstance(obj.getClass().getClassLoader(),<br />
                                                                           obj.getClass().getInterfaces(),<br />
                                                                           new ProxyTarget(obj));<br />
       }</p>
<p>        private Object target;<br />
        private ProxyTarget(Object target)<br />
        {<br />
                this.target = target;<br />
         }<br />
 <br />
        <font color="#ff0000">//Implements the method of InvocationHandler interface<br />
        //This method will catch all the calls to the target class<br />
        //and redispatch them       </font><br />
        public Object invoke(Object proxy, Method method, Object[] args)<br />
        {<br />
                 Object result = null;<br />
                 try{<br />
                        <font color="#ff0000">//add function before redispatching</font><br />
                        System.out.println(&#8220;Before &#8220;+method.getName());<br />
                        <font color="#ff0000">//redispatch the call</font><br />
                        result = method.invoke(target,args);<br />
                        <font color="#ff0000">//add function after redispatching<br />
</font>                        System.out.println(&#8220;After &#8220;+method.getName());<br />
                 }catch(Exception e){<br />
                        System.out.println(e.getMessage());<br />
                 }<br />
                 return result;<br />
          }<br />
}</p>
<p><font color="#ff0000">//TestProxyTarget.java<br />
//the test class</font><br />
public class TestProxyTarget{<br />
        public static void main(String[] args){<br />
                 TargetInterface t = (TargetInterface)ProxyTarget.createProxy(<br />
                                                                                   new TargetImplementation());<br />
               <font color="#ff0000">  //this will call the invoke method of the ProxyTarget class automatically and transparently</font> <br />
                 t.print();<br />
        }<br />
}</p>
<p>The running result of the TestProxyTarget is:</p>
<p style="text-align:center;"><img src="http://powerdream5.files.wordpress.com/2007/10/blogcmd.JPG" alt="blogcmd.JPG" /></p>
<p>         After reading the examples, I hope you can have some basic ideas of Ioc and Aop, and figure out the relationship between java reflection and Ioc and AOP. Of course, if you want to learn more, please search more materials about them.</p>
<p><a href="http://powerdream5.files.wordpress.com/2007/10/xiaoran.jpg" title="xiaoran.jpg"></a><a href="http://powerdream5.files.wordpress.com/2007/10/xiaoran.jpg" title="xiaoran.jpg"></p>
<p style="text-align:center;"><img src="http://powerdream5.files.wordpress.com/2007/10/xiaoran.jpg" alt="xiaoran.jpg" /></p>
<p></a><a href="http://powerdream5.files.wordpress.com/2007/10/blogcmd.JPG" title="blogcmd.JPG"></a><a href="http://powerdream5.files.wordpress.com/2007/10/blogcmd.JPG" title="blogcmd.JPG"></a><a href="http://powerdream5.files.wordpress.com/2007/10/blogcmd.JPG" title="blogcmd.JPG"></p>
<p style="text-align:center;"> </p>
<p></a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/powerdream5.wordpress.com/22/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/powerdream5.wordpress.com/22/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/powerdream5.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/powerdream5.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/powerdream5.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/powerdream5.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/powerdream5.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/powerdream5.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/powerdream5.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/powerdream5.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/powerdream5.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/powerdream5.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=powerdream5.wordpress.com&blog=1840509&post=22&subd=powerdream5&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://powerdream5.wordpress.com/2007/10/08/applying-java-reflection-to-ioc-and-aop-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a0819a06b6ff4afc4f3e0a1977e6a5a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">powerdream5</media:title>
		</media:content>

		<media:content url="http://powerdream5.files.wordpress.com/2007/10/blog.JPG" medium="image">
			<media:title type="html">blog.JPG</media:title>
		</media:content>

		<media:content url="http://powerdream5.files.wordpress.com/2007/10/blogcmd.JPG" medium="image">
			<media:title type="html">blogcmd.JPG</media:title>
		</media:content>

		<media:content url="http://powerdream5.files.wordpress.com/2007/10/xiaoran.jpg" medium="image">
			<media:title type="html">xiaoran.jpg</media:title>
		</media:content>
	</item>
	</channel>
</rss>