<?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>素心如何天上月 &#187; Java</title> <atom:link href="http://yongsun.me/category/java/feed/" rel="self" type="application/rss+xml" /><link>http://yongsun.me</link> <description>Yong Sun&#039;s Blog</description> <lastBuildDate>Mon, 19 Mar 2012 02:29:22 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>Stand alone的Spring示例 (IoC)</title><link>http://yongsun.me/2005/05/stand-alone%e7%9a%84spring%e7%a4%ba%e4%be%8b-ioc/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=stand-alone%25e7%259a%2584spring%25e7%25a4%25ba%25e4%25be%258b-ioc</link> <comments>http://yongsun.me/2005/05/stand-alone%e7%9a%84spring%e7%a4%ba%e4%be%8b-ioc/#comments</comments> <pubDate>Mon, 16 May 2005 03:58:52 +0000</pubDate> <dc:creator>yongsun</dc:creator> <category><![CDATA[Java]]></category> <category><![CDATA[IoC]]></category> <category><![CDATA[spring]]></category> <guid
isPermaLink="false">http://yongsun.wordpress.com/2005/05/15/stand-alone%e7%9a%84spring%e7%a4%ba%e4%be%8b-ioc/</guid> <description><![CDATA[Reference:1. 60秒入门教程: http://gabriel.jarchitect.org/spring/index.html必需的jar文件包括: spring-core.jar, common-logging.jar, 在IDEA中,运行Java虚拟机的目录并不是classes目录, 因此它在当前目录中无法找到"bean.xml"文件, 使用下面的代码:&#160; import org.springframework.core.io.*; &#160; /* Read the configuration file from classpath */&#160;&#160; Resource res = new ClassPathResource("bean.xml");&#160;&#160; XmlBeanFactory factory = new XmlBeanFactory(res);]]></description> <content:encoded><![CDATA[<p><span
style="font-family:Arial,Helvetica,sans-serif;">Reference:</span><br
style="font-family:Arial,Helvetica,sans-serif;"><br
style="font-family:Arial,Helvetica,sans-serif;"><span
style="font-family:Arial,Helvetica,sans-serif;">1. 60秒入门教程: </span><a
style="font-family:Arial,Helvetica,sans-serif;" href="http://gabriel.jarchitect.org/spring/index.html">http://gabriel.jarchitect.org/spring/index.html</a><br
style="font-family:Arial,Helvetica,sans-serif;"><br
style="font-family:Arial,Helvetica,sans-serif;"><span
style="font-family:Arial,Helvetica,sans-serif;">必需的jar文件包括: spring-core.jar, common-logging.jar, 在IDEA中,</span><br
style="font-family:Arial,Helvetica,sans-serif;"><span
style="font-family:Arial,Helvetica,sans-serif;">运行Java虚拟机的目录并不是classes目录, 因此它在当前目录中无法找到</span><br
style="font-family:Arial,Helvetica,sans-serif;"><span
style="font-family:Arial,Helvetica,sans-serif;">"bean.xml"文件, 使用下面的代码:</span><br
style="font-family:Arial,Helvetica,sans-serif;"><br
style="font-family:Arial,Helvetica,sans-serif;"><span
style="font-family:Arial,Helvetica,sans-serif;">&nbsp; import org.springframework.core.io.*; </span><br
style="font-family:Arial,Helvetica,sans-serif;"><span
style="font-family:Arial,Helvetica,sans-serif;">&nbsp; /* Read the configuration file from classpath */</span><br
style="font-family:Arial,Helvetica,sans-serif;"><span
style="font-family:Arial,Helvetica,sans-serif;">&nbsp;&nbsp; Resource res = new ClassPathResource("bean.xml");</span><br
style="font-family:Arial,Helvetica,sans-serif;"><span
style="font-family:Arial,Helvetica,sans-serif;">&nbsp;&nbsp; XmlBeanFactory factory = new XmlBeanFactory(res);</span><br
style="font-family:Arial,Helvetica,sans-serif;"><br
style="font-family:Arial,Helvetica,sans-serif;"><br
style="font-family:Arial,Helvetica,sans-serif;"></p> ]]></content:encoded> <wfw:commentRss>http://yongsun.me/2005/05/stand-alone%e7%9a%84spring%e7%a4%ba%e4%be%8b-ioc/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Stand alone的Spring示例 (AOP)</title><link>http://yongsun.me/2005/05/stand-alone%e7%9a%84spring%e7%a4%ba%e4%be%8b-aop/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=stand-alone%25e7%259a%2584spring%25e7%25a4%25ba%25e4%25be%258b-aop</link> <comments>http://yongsun.me/2005/05/stand-alone%e7%9a%84spring%e7%a4%ba%e4%be%8b-aop/#comments</comments> <pubDate>Mon, 16 May 2005 03:57:28 +0000</pubDate> <dc:creator>yongsun</dc:creator> <category><![CDATA[Java]]></category> <category><![CDATA[AOP]]></category> <category><![CDATA[spring]]></category> <guid
isPermaLink="false">http://yongsun.wordpress.com/2005/05/15/stand-alone%e7%9a%84spring%e7%a4%ba%e4%be%8b-aop/</guid> <description><![CDATA[Referneces: 1. An Introduction to Aspect-Oriented Programming with the Spring Framework, Part 1: http://www.onjava.com/lpt/a/4994 2. http://www.springframework.org/docs/wiki/Spring_AOP_Framework.html 3. SpringFrameWork中AOP的简单使用: http://blog.csdn.net/ylong/archive/2004/07/10/38482.aspx I just copy the code from the onjava one, and implement a demo with IDEA 4.5. But a minor change for &#8230; <a
href="http://yongsun.me/2005/05/stand-alone%e7%9a%84spring%e7%a4%ba%e4%be%8b-aop/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>Referneces:</p><p>1. An Introduction to Aspect-Oriented Programming with the Spring Framework, Part 1: http://www.onjava.com/lpt/a/4994</p><p>2. http://www.springframework.org/docs/wiki/Spring_AOP_Framework.html</p><p>3. SpringFrameWork中AOP的简单使用: http://blog.csdn.net/ylong/archive/2004/07/10/38482.aspx</p><p>I just copy the code from the onjava one, and implement a demo with<br
/> IDEA 4.5. But a minor change for the same issue in IoC example:</p><p>import org.springframework.context.support.ClassPathXmlApplicationContext;</p><p>&nbsp;&nbsp;&nbsp; // Read the configuration file from classpath<br
/> &nbsp;&nbsp;&nbsp; ApplicationContext ctx = new ClassPathXmlApplicationContext( "springconfig.xml");</p><p>必须的jar文件包括:<br
/> aopalliance.jar, commons-logging.jar, jakarta-oro-xxx.jar, spring-aop.jar, spring-context.jar, spring-core.jar.</p> ]]></content:encoded> <wfw:commentRss>http://yongsun.me/2005/05/stand-alone%e7%9a%84spring%e7%a4%ba%e4%be%8b-aop/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Java动态代理的示例</title><link>http://yongsun.me/2005/05/java%e5%8a%a8%e6%80%81%e4%bb%a3%e7%90%86%e7%9a%84%e7%a4%ba%e4%be%8b/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=java%25e5%258a%25a8%25e6%2580%2581%25e4%25bb%25a3%25e7%2590%2586%25e7%259a%2584%25e7%25a4%25ba%25e4%25be%258b</link> <comments>http://yongsun.me/2005/05/java%e5%8a%a8%e6%80%81%e4%bb%a3%e7%90%86%e7%9a%84%e7%a4%ba%e4%be%8b/#comments</comments> <pubDate>Mon, 09 May 2005 03:16:09 +0000</pubDate> <dc:creator>yongsun</dc:creator> <category><![CDATA[Java]]></category> <guid
isPermaLink="false">http://yongsun.wordpress.com/2005/05/08/java%e5%8a%a8%e6%80%81%e4%bb%a3%e7%90%86%e7%9a%84%e7%a4%ba%e4%be%8b/</guid> <description><![CDATA[public interface SomeClass { public abstract void someMethod(); public abstract void someOtherMethod(final String text); } public class SomeClassImpl implements SomeClass{ private String userName; public SomeClassImpl(final String userName) { this.userName = userName; } public void someMethod( ) { System.out.println(this.userName); } public &#8230; <a
href="http://yongsun.me/2005/05/java%e5%8a%a8%e6%80%81%e4%bb%a3%e7%90%86%e7%9a%84%e7%a4%ba%e4%be%8b/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<pre name="code" class="java">
public interface SomeClass {
    public abstract void someMethod();
    public abstract void someOtherMethod(final String text);
}
public class SomeClassImpl implements SomeClass{
    private String userName;
    public SomeClassImpl(final String userName) {
        this.userName = userName;
    }
    public void someMethod( ) {
        System.out.println(this.userName);
    }
    public void someOtherMethod(final String text) {
        System.out.println(text);
    }
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MethodCountingHandler implements InvocationHandler {
    /* whatever object, you can pass one in */
    private final Object impl;
    private int invocationCount = 0;
    /* constructor */
    public MethodCountingHandler(final Object impl) {
        this.impl = impl;
    }
    /* export the invocation Count */
    public int getInvocationCount( ) {
        return invocationCount;
    }
    /* implements the interface function of InvocationHandler */
    public Object invoke(Object proxy, Method meth, Object[] args)  throws Throwable {
        try {
            this.invocationCount++;
            Object result = meth.invoke(impl, args);
            return result;
        } catch (final InvocationTargetException ex) {
            throw ex.getTargetException( );
        }
    }
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class SomeClassFactory {
    public static final SomeClass getDynamicSomeClassProxy( ) {
        /* get a implement instance of SomeClass */
        SomeClassImpl impl = new SomeClassImpl(System.getProperty("user.name"));
        if ( !(impl instanceof SomeClass) )
            return null;
        /* construct a invocation handler with the impl instance */
        InvocationHandler handler = new MethodCountingHandler(impl);
        /* get the class info, and the class loader used by this factory */
        Class[] interfaces = new Class[] { SomeClass.class };
        ClassLoader loader = SomeClassFactory.class.getClassLoader( );
        /*
        * install the handler for all implementations of this interface in this class loader
        * and return the proxy instance which accords to SomeClass interface.
        */
        SomeClass proxy = (SomeClass)Proxy.newProxyInstance(loader,
                interfaces,
                handler);
        return proxy;
    }
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class DemoDynamicProxy {
    public static final void main(final String[] args) {
        SomeClass proxy = SomeClassFactory.getDynamicSomeClassProxy( );
        proxy.someMethod( );
        proxy.someOtherMethod("Our Proxy works!");
        /* get the handler associated with this proxy instance */
        InvocationHandler handler = Proxy.getInvocationHandler(proxy);
        if (handler instanceof MethodCountingHandler) {
            System.out.println(((MethodCountingHandler)handler).getInvocationCount( ));
        }
     }
}
</pre><p>Java的动态代理只支持基于Interface的Method Interception. 本例是从”Hardcore Java”一书中摘取出来的.</span><br
style="font-family:Arial,Helvetica,sans-serif;"><br
style="font-family:Arial,Helvetica,sans-serif;"></p> ]]></content:encoded> <wfw:commentRss>http://yongsun.me/2005/05/java%e5%8a%a8%e6%80%81%e4%bb%a3%e7%90%86%e7%9a%84%e7%a4%ba%e4%be%8b/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
