<?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>Neeke &#187; Spring AOP</title>
	<atom:link href="http://www.ineeke.com/tag/spring-aop/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ineeke.com</link>
	<description>悄悄记录点点滴滴</description>
	<lastBuildDate>Sat, 07 Jan 2012 13:04:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>配置applicationContext.xml实现AOP</title>
		<link>http://www.ineeke.com/archives/849/</link>
		<comments>http://www.ineeke.com/archives/849/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 11:22:57 +0000</pubDate>
		<dc:creator>Neeke</dc:creator>
				<category><![CDATA[J2EE技术]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring AOP]]></category>

		<guid isPermaLink="false">http://ineeke.com/archives/%e9%85%8d%e7%bd%aeapplicationcontextxml%e5%ae%9e%e7%8e%b0aop/</guid>
		<description><![CDATA[<p>昨天是使用Annotation注解的方式实现的AOP，今天再将其改为使用配置文件来实现。使用这种方法的话，SecurityHandler类就又少了一大截的代码，只需要提供一个用于标识切点的方法即可。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Spring AOP JDK动态代理" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F847%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F849%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring AOP JDK动态代理</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Annotation方式实现AOP" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F848%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F849%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Annotation方式实现AOP</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring公共属性注入" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F845%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F849%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring公共属性注入</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring中Bean的作用域" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F846%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F849%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring中Bean的作用域</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring MVC" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F1117%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F849%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/site_images/2011/05/12/8522888.png" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring MVC</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>昨天是使用Annotation注解的方式实现的AOP，今天再将其改为使用配置文件来实现。使用这种方法的话，SecurityHandler类就又少了一大截的代码，只需要提供一个用于标识切点的方法即可。</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">cn.ineeke.spring</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SecurityHandler <span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> printSomthing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;--------Security----------&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>虽说类中是不需要使用注解了，但是这个东西肯定是得有的，只不过是将其转移到applicationContext.xml文件中了而已。使用<bean>标签配置好DAO类和SecurityHandler类。</p>
<p>使用<aop:config>标签开始配置AOP。<aop:aspect>标签用于指定哪个bean是切面。<aop:pointcut>标签用于指定切点。<aop:before>标签在这里用于定义Advice，同前面所说的一样，这里还可以是<aop:after>等等。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span></span>
<span style="color: #009900;"><span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">xmlns:aop</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/aop&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans</span>
<span style="color: #009900;">http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</span>
<span style="color: #009900;">http://www.springframework.org/schema/aop</span>
<span style="color: #009900;">http://www.springframework.org/schema/aop/spring-aop-2.5.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;securityHandler&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;cn.ineeke.spring.SecurityHandler&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;userDAO&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;cn.ineeke.spring.UserDAOImpl&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<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>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;aop:aspect</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;securityAspect&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;securityHandler&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<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;addMethod&quot;</span> <span style="color: #000066;">expression</span>=<span style="color: #ff0000;">&quot;execution(* *(..))&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;aop:before</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;printSomthing&quot;</span> <span style="color: #000066;">pointcut-ref</span>=<span style="color: #ff0000;">&quot;addMethod&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<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>
<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>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>调用方法同之前的一样，这样看起来应该比昨天那个清晰的多了，看标签也比较好容易理解。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Spring AOP JDK动态代理" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F847%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F849%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring AOP JDK动态代理</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Annotation方式实现AOP" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F848%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F849%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Annotation方式实现AOP</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring公共属性注入" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F845%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F849%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring公共属性注入</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring中Bean的作用域" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F846%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F849%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring中Bean的作用域</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring MVC" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F1117%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F849%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/site_images/2011/05/12/8522888.png" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring MVC</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://www.ineeke.com/archives/849/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Annotation方式实现AOP</title>
		<link>http://www.ineeke.com/archives/848/</link>
		<comments>http://www.ineeke.com/archives/848/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 14:10:16 +0000</pubDate>
		<dc:creator>Neeke</dc:creator>
				<category><![CDATA[J2EE技术]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring AOP]]></category>

		<guid isPermaLink="false">http://ineeke.com/archives/annotation%e6%96%b9%e5%bc%8f%e5%ae%9e%e7%8e%b0aop/</guid>
		<description><![CDATA[<p>使用Annotation方式重新实现昨天的例子。重新修改SecurityHandler类，使用@Aspect声明此类为一个使用了AOP技术的切面。提供一个方法allMethod()，该方法无参且无返回类型也没有具体的代码实现，用于定义Pointcut（切入点）。Pointcut的内容是一个表达式，用于描述对哪些方法进行切入（类似于拦截的作用）。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Spring AOP JDK动态代理" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F847%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F848%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring AOP JDK动态代理</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="配置applicationContext.xml实现AOP" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F849%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F848%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">配置applicationContext.xml实现AOP</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Why Spring" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F844%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F848%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Why Spring</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring MVC" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F1117%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F848%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/site_images/2011/05/12/8522888.png" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring MVC</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring MVC+Hibernate自动注入sessionFactory" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F1262%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F848%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring MVC+Hibernate自动注入sessionFactory</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>使用Annotation方式重新实现昨天的例子。重新修改SecurityHandler类，使用@Aspect声明此类为一个使用了AOP技术的切面。提供一个方法allMethod()，该方法无参且无返回类型也没有具体的代码实现，用于定义Pointcut（切入点）。Pointcut的内容是一个表达式，用于描述对哪些方法进行切入（类似于拦截的作用）。</p>
<p>定义Advice，字面上是“建议”的意思，这里可以理解为所要向切入点中插入的其他操作。@Before表示该Advice是在切入点中方法执行之前被执行，其参数是指在哪个Pointcut中织入该Advice。除了Before当然还有其它的了，可以百度一下。</p>
<p>该SecurityHandler类的完整代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">cn.ineeke.spring</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.annotation.Aspect</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.annotation.Before</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.annotation.Pointcut</span><span style="color: #339933;">;</span>
@Aspect
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SecurityHandler <span style="color: #009900;">&#123;</span>
@Pointcut<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;execution(* *(..))&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> allMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
@Before<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;allMethod()&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> printSomthing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;--------Security----------&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>在execution(* *(..))中，其格式为execution(返回类型 方法名(参数))，“*”和“..”均表示所有。</p>
<p>接下来需要启用Aspectj对Annotation的支持，并将Aspect类和目标对象配置到IOC容器中。修改applicationContext.xml文件，使用<aop:aspectj-autoproxy>标签启用支持。使用<bean>标签进行相关配置。</p>
<p>完整配置如下：</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span></span>
<span style="color: #009900;"><span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">xmlns:aop</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/aop&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans</span>
<span style="color: #009900;">http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</span>
<span style="color: #009900;">http://www.springframework.org/schema/aop</span>
<span style="color: #009900;">http://www.springframework.org/schema/aop/spring-aop-2.5.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;aop:aspectj-autoproxy</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;securityHandler&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;cn.ineeke.spring.SecurityHandler&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;userDAO&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;cn.ineeke.spring.UserDAOImpl&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>最后再写个测试类测试一下。</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">cn.ineeke.spring</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.beans.factory.BeanFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.context.support.ClassPathXmlApplicationContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #009900;">&#123;</span>
<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><span style="color: #003399;">String</span><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>
BeanFactory factory <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ClassPathXmlApplicationContext<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;applicationContext.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
IUserDAO userDAO <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>IUserDAO<span style="color: #009900;">&#41;</span> factory.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;userDAO&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
userDAO.<span style="color: #006633;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>最后需要注意的是，这里用于定义Pointcut的allMethod()方法并不会被执行，它仅仅只是起到一个标识的作用。不信的话可以在allMethod()方法中写点什么，运行一下看看是否会被执行。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Spring AOP JDK动态代理" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F847%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F848%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring AOP JDK动态代理</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="配置applicationContext.xml实现AOP" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F849%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F848%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">配置applicationContext.xml实现AOP</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Why Spring" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F844%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F848%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Why Spring</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring MVC" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F1117%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F848%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/site_images/2011/05/12/8522888.png" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring MVC</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring MVC+Hibernate自动注入sessionFactory" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F1262%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F848%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring MVC+Hibernate自动注入sessionFactory</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://www.ineeke.com/archives/848/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring AOP JDK动态代理</title>
		<link>http://www.ineeke.com/archives/847/</link>
		<comments>http://www.ineeke.com/archives/847/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 10:52:59 +0000</pubDate>
		<dc:creator>Neeke</dc:creator>
				<category><![CDATA[J2EE技术]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring AOP]]></category>

		<guid isPermaLink="false">http://ineeke.com/archives/spring-aop-jdk%e5%8a%a8%e6%80%81%e4%bb%a3%e7%90%86/</guid>
		<description><![CDATA[<p>在没有使用Spring之前，如果要对DAO层进行修改，比如说做数据安全性验证。那么，就需要对DAO层中的每一个方法都进行修改，项目小了好说，大了的话呢？不久后又要删掉这一功能呢？那岂不又是费神之事。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Annotation方式实现AOP" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F848%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F847%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Annotation方式实现AOP</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="配置applicationContext.xml实现AOP" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F849%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F847%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">配置applicationContext.xml实现AOP</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring MVC 文件上传" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F1124%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F847%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/site_images/2011/05/12/8522888.png" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring MVC 文件上传</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring MVC" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F1117%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F847%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/site_images/2011/05/12/8522888.png" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring MVC</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Why Spring" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F844%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F847%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Why Spring</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>在没有使用Spring之前，如果要对DAO层进行修改，比如说做数据安全性验证。那么，就需要对DAO层中的每一个方法都进行修改，项目小了好说，大了的话呢？不久后又要删掉这一功能呢？那岂不又是费神之事。</p>
<p>这一问题使用AOP（Aspect Oriented Programming 面向切面编程）就好解决的多了。面向切面编程，就像使用FilterServlet（过滤器）对字符编码进行转换时一样。当BIZ层调用DAO层中的方法时，可以对其实施拦截，在前之前或之后插入所要执行的操作。</p>
<p>下面看看具体如何对这一问题进行应用。首先创建一个安全性检测类SecurityHandler并让它实现InvocationHandler接口。声明一个Object类型的变量用于存储目标对象（我们要对谁进行拦截）。提供一个方法getProxy()，该方法接收目标对象然后返回该对象的代理对象。在invoke()方法中先进行数据安全性检测，然后再调用目标对象的方法。因为无法确定所调用的方法是或否有返回值，所以默认的令其返回Object类型。完整代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">cn.ineeke.spring</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.InvocationHandler</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.Method</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.Proxy</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SecurityHandler <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">InvocationHandler</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//用于存储目标对象</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Object</span> obj<span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//获得目标对象的代理</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> getProxy<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">obj</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Proxy</span>.<span style="color: #006633;">newProxyInstance</span><span style="color: #009900;">&#40;</span>obj.<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>,
obj.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getInterfaces</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
<span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//方法调用</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> invoke<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> proxy, <span style="color: #003399;">Method</span> method, <span style="color: #003399;">Object</span><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> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
printSomthing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Object</span> ret <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
ret <span style="color: #339933;">=</span> method.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">obj</span>, args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">return</span> ret<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> printSomthing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;--------Security----------&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>接下来编写一个测试类。</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">cn.ineeke.spring</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #009900;">&#123;</span>
<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><span style="color: #003399;">String</span><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>
SecurityHandler sryHandler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SecurityHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
IUserDAO userDAO <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>IUserDAO<span style="color: #009900;">&#41;</span> sryHandler.<span style="color: #006633;">getProxy</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> UserDAOImpl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
userDAO.<span style="color: #006633;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>上面的代码中，首先创建了一个SecurityHandler的实例sryHandler，然后调用getProxy()方法根据具体的目标对象创建具体的代理对象，最后通过代理来调用相关的方法。这么一来，无论是开发阶段还是后期的维护中，如果需要改动，则只需要改动SecurityHandler类即可，不再需要对DAO中每个方法逐一改动了。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Annotation方式实现AOP" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F848%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F847%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Annotation方式实现AOP</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="配置applicationContext.xml实现AOP" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F849%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F847%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">配置applicationContext.xml实现AOP</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring MVC 文件上传" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F1124%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F847%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/site_images/2011/05/12/8522888.png" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring MVC 文件上传</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring MVC" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F1117%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F847%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/site_images/2011/05/12/8522888.png" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring MVC</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Why Spring" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F844%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F847%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Why Spring</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://www.ineeke.com/archives/847/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

