﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Sun River-随笔分类-AOP</title><link>http://www.blogjava.net/SunRiver/category/17334.html</link><description>Topics about J2EE XML AJAX SOA DB etc.</description><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 03:10:57 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 03:10:57 GMT</pubDate><ttl>60</ttl><item><title>A simple use of Spring AOP</title><link>http://www.blogjava.net/SunRiver/archive/2006/11/16/81508.html</link><dc:creator>Sun River</dc:creator><author>Sun River</author><pubDate>Thu, 16 Nov 2006 05:29:00 GMT</pubDate><guid>http://www.blogjava.net/SunRiver/archive/2006/11/16/81508.html</guid><wfw:comment>http://www.blogjava.net/SunRiver/comments/81508.html</wfw:comment><comments>http://www.blogjava.net/SunRiver/archive/2006/11/16/81508.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/SunRiver/comments/commentRss/81508.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/SunRiver/services/trackbacks/81508.html</trackback:ping><description><![CDATA[
		<h4 style="MARGIN: 3pt 0in 0pt; LINE-HEIGHT: 145%">
				<b>
						<span style="COLOR: black">Spring</span>
				</b>
				<b>
						<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的</span>
						<span style="COLOR: black">AOP</span>
				</b>
				<b>
						<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">简单使用</span>
						<span style="COLOR: black">
								<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?>
								<o:p>
								</o:p>
						</span>
				</b>
		</h4>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">    <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">    AOP</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">作为</span>
				<span style="COLOR: black">Spring</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这个轻量级的容器中很重要的一部分，得到越来越多的关注，</span>
				<span style="COLOR: black">Spring</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</span>
				<span style="COLOR: black">Transaction</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">就是用</span>
				<span style="COLOR: black">AOP</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来管理的，今天就通过简单的例子来看看</span>
				<span style="COLOR: black">Spring</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的</span>
				<span style="COLOR: black">AOP</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的基本使用方法。</span>
				<span style="COLOR: black">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">  </span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">首先确定将要</span>
				<span style="COLOR: black">Proxy</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的目标，在</span>
				<span style="COLOR: black">Spring</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中默认采用</span>
				<span style="COLOR: black">JDK</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的</span>
				<span style="COLOR: black">dynamic proxy</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，它只能够实现接口的代理，如果想对类进行代理的话，需要采用</span>
				<span style="COLOR: black">CGLIB</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</span>
				<span style="COLOR: black">proxy</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。显然，选择</span>
				<span style="COLOR: black">“</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">编程到接口</span>
				<span style="COLOR: black">”</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是更明智的做法。</span>
				<span style="COLOR: black">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">下面是将要代理的接口：</span>
				<span style="COLOR: black">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">public interface FooInterface {<br />    public void printFoo();<br />    public void dummyFoo();<br />}<br /><br /></span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以及其一个简单的实现：</span>
				<span style="COLOR: black">
						<br />public class FooImpl implements FooInterface {<br />    public void printFoo() {<br />        System.out.println("In FooImpl.printFoo");<o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">    }<br /><br />    public void dummyFoo() {<br />        System.out.println("In FooImpl.dummyFoo");<br />    }<br />}<br /> <br /></span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">接下来创建一个</span>
				<span style="COLOR: black">Advice</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，在</span>
				<span style="COLOR: black">Spring</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中支持</span>
				<span style="COLOR: black">Around,Before,After returning</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
				<span style="COLOR: black">Throws</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">四种</span>
				<span style="COLOR: black">Advice</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，这里就以简单的</span>
				<span style="COLOR: black">Before Advice</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">举例：</span>
				<span style="COLOR: black">
						<br /> <br />public class PrintBeforeAdvice implements MethodBeforeAdvice {<br />    public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {<br />        System.out.println("In PrintBeforeAdvice");<br />    }<o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">}<br /> <br /></span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">有了自己的</span>
				<span style="COLOR: black">business interface</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
				<span style="COLOR: black">advice</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，剩下的就是如何去装配它们了，首先利用</span>
				<span style="COLOR: black">ProxyFactory</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以编程方式实现，如下：</span>
				<span style="COLOR: black">
						<br /> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">public class AopTestMain {<br />    public static void main(String[] args) {<br />        FooImpl fooImpl = new FooImpl();<br />        PrintBeforeAdvice myAdvice = new PrintBeforeAdvice();<br />     <br />        ProxyFactory factory = new ProxyFactory(fooImpl);<br />        factory.addBeforeAdvice(myAdvice);<br />        FooInterface myInterface = (FooInterface)factory.getProxy();<o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">        myInterface.printFoo();<br />        myInterface.dummyFoo();<br />    }<o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">}<br /> <br />  </span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">现在执行程序，神奇的结果就出现了：</span>
				<span style="COLOR: black">
						<br /> <br />  In PrintBeforeAdvice<br />  In FooImpl.printFoo<br />  In PrintBeforeAdvice<br />  In FooImpl.dummyFoo<br /> <br />   </span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">虽然这样能体会到</span>
				<span style="COLOR: black">Spring</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中</span>
				<span style="COLOR: black">AOP</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的用法，但这决不是值得推荐的方法，既然使用了</span>
				<span style="COLOR: black">Spring</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，在</span>
				<span style="COLOR: black">ApplicationContext</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中装配所需要</span>
				<span lang="ZH-CN" style="COLOR: black">
				</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</span>
				<span style="COLOR: black">bean</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">才是最佳策略，实现上面的功能只需要写个简单的</span>
				<span style="COLOR: black">applicationContext</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">就可以了，如下：</span>
				<span style="COLOR: black">
						<br /> <br />  &lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />  &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"<br />    "</span>http://www.springframework.org/dtd/spring-beans.dtd<span style="COLOR: black">"&gt;<o:p></o:p></span></p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">&lt;beans&gt;<br />    &lt;description&gt;The aop application context&lt;/description&gt;<br />    &lt;bean id="fooTarget" class="FooImpl"/&gt;<br />    &lt;bean id="myAdvice" class="PrintBeforeAdvice"/&gt;<br />    &lt;bean id="foo" class="org.springframework.aop.framework.ProxyFactoryBean"&gt;<br />     &lt;property name="proxyInterfaces"&gt;<br />       &lt;value&gt;FooInterface&lt;/value&gt;<br />     &lt;/property&gt;<br />     &lt;property name="target"&gt;<br />       &lt;ref local="fooTarget"/&gt;<br />     &lt;/property&gt;<br />     &lt;property name="interceptorNames"&gt;<br />       &lt;list&gt;<br />         &lt;value&gt;myAdvice&lt;/value&gt;<br />       &lt;/list&gt;<br />     &lt;/property&gt;<br />    &lt;/bean&gt;<o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">&lt;/beans&gt;<o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">  </span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">当然，</span>
				<span style="COLOR: black">main</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的代码也要进行相应的修改：</span>
				<span style="COLOR: black">
						<br />    <br />public static void main(String[] args) {<br />    ClassPathXmlApplicationContext context = new <br />             ClassPathXmlApplicationContext("applicationContext.xml");<br />    FooInterface foo = (FooInterface)context.getBean("foo");<br />    foo.printFoo();<br />    foo.dummyFoo();<br />}<br /> <br />   </span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">现在运行一下，结果将和上面的运行结果完全一样，这样是不是更优雅？当需要更改实现时，只需要修改配置文件就可以了，程序中的代码不需任何改动。</span>
				<span style="COLOR: black">
						<br /> <br />   </span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">但是，这时候会发现被</span>
				<span style="COLOR: black">proxy</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</span>
				<span style="COLOR: black">object</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的所有方法调用时都将运行</span>
				<span style="COLOR: black">advice</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的</span>
				<span style="COLOR: black">before</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，这显然不能满足绝大多数情况下的需要，此时，只</span>
				<span lang="ZH-CN" style="COLOR: black">
				</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">需借用</span>
				<span style="COLOR: black">Advisor</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">就可以了，当然要在</span>
				<span style="COLOR: black">Advisor</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中利用</span>
				<span style="COLOR: black">pattern</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设置好哪些方法需要</span>
				<span style="COLOR: black">advice</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，更改</span>
				<span style="COLOR: black">applicationContext </span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如下：</span>
				<span style="COLOR: black">
						<br /> <br />  &lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />  &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"<br />    "</span>http://www.springframework.org/dtd/spring-beans.dtd<span style="COLOR: black">"&gt;<o:p></o:p></span></p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">&lt;beans&gt;<br />    &lt;description&gt;The springeva application context&lt;/description&gt;<br />    &lt;bean id="fooTarget" class="FooImpl"/&gt;<br />    &lt;bean id="printBeforeAdvice" class="PrintBeforeAdvice"/&gt;<br />    &lt;bean id="myAdvisor"<br />          class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"&gt;<br />      &lt;property name="advice"&gt;<br />        &lt;ref local="printBeforeAdvice"/&gt;<br />      &lt;/property&gt;<br />      &lt;property name="pattern"&gt;<br />        &lt;value&gt;.*print.*&lt;/value&gt;<br />      &lt;/property&gt;<br />    &lt;/bean&gt;<br />    &lt;bean id="foo" class="org.springframework.aop.framework.ProxyFactoryBean"&gt;<br />      &lt;property name="proxyInterfaces"&gt;<br />        &lt;value&gt;FooInterface&lt;/value&gt;<br />      &lt;/property&gt;<br />      &lt;property name="target"&gt;<br />        &lt;ref local="fooTarget"/&gt;<br />      &lt;/property&gt;<br />      &lt;property name="interceptorNames"&gt;<br />        &lt;list&gt;<br />          &lt;value&gt;myAdvisor&lt;/value&gt;<br />        &lt;/list&gt;<br />      &lt;/property&gt;<br />    &lt;/bean&gt;<br />&lt;/beans&gt;<o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">   </span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">主程序不需进行任何修改，运行结果已经变样了</span>
				<span style="COLOR: black">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">   In PrintBeforeAdvice<br />    In FooImpl.printFoo<br />    In FooImpl.dummyFoo<br /> <br />   </span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">至此，应该已经理解了</span>
				<span style="COLOR: black">Spring</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中</span>
				<span style="COLOR: black">AOP</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的使用方法，当然</span>
				<span style="COLOR: black">Spring</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中</span>
				<span style="COLOR: black">AOP</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">最重要的应用是</span>
				<span style="COLOR: black">Transaction Manager</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，举个这方面的</span>
				<span style="COLOR: black">applicationContext</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">例子看看：</span>
				<span style="COLOR: black">
						<br /> <br />  &lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />  &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd"&gt;<o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 5pt 0in">
				<span style="COLOR: black">&lt;beans&gt;<br />    &lt;bean id="propertyConfigurer"   <br />         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;<br />      &lt;property name="location"&gt;<br />        &lt;value&gt;/WEB-INF/jdbc.properties&lt;/value&gt;<br />      &lt;/property&gt;<br />    &lt;/bean&gt;<br />    &lt;bean id="dataSource"<br />          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;<br />      &lt;property name="driverClassName"&gt;<br />        &lt;value&gt;${jdbc.driverClassName}&lt;/value&gt;<br />      &lt;/property&gt;<br />      &lt;property name="url"&gt;<br />        &lt;value&gt;${jdbc.url}&lt;/value&gt;<br />      &lt;/property&gt;<br />      &lt;property name="username"&gt;<br />        &lt;value&gt;${jdbc.username}&lt;/value&gt;<br />      &lt;/property&gt;<br />      &lt;property name="password"&gt;<br />        &lt;value&gt;${jdbc.password}&lt;/value&gt;<br />      &lt;/property&gt;<br />    &lt;/bean&gt;<br />    &lt;bean id="sessionFactory"<br />          class="org.springframework.orm.hibernate.LocalSessionFactoryBean"&gt;<br />      &lt;property name="dataSource"&gt;<br />        &lt;ref local="dataSource"/&gt;<br />      &lt;/property&gt;<br />      &lt;property name="mappingResources"&gt;<br />        &lt;value&gt;smartmenu.hbm.xml&lt;/value&gt;<br />      &lt;/property&gt;<br />      &lt;property name="hibernateProperties"&gt;<br />        &lt;props&gt;<br />          &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;<br />        &lt;/props&gt;<br />      &lt;/property&gt;<br />    &lt;/bean&gt;<br /> <br />    &lt;bean id="transactionManager"       <br />          class="org.springframework.orm.hibernate.HibernateTransactionManager"&gt;<br />      &lt;property name="sessionFactory"&gt;<br />        &lt;ref local="sessionFactory"/&gt;<br />      &lt;/property&gt;<br />    &lt;/bean&gt;<br />    &lt;bean id="smartmenuTarget" class="SmartMenuHibernate"&gt;<br />      &lt;property name="sessionFactory"&gt;<br />        &lt;ref local="sessionFactory"/&gt;<br />      &lt;/property&gt;<br />    &lt;/bean&gt;<br />    &lt;bean id="smartMenu"<br />        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"&gt;<br />      &lt;property name="transactionManager"&gt;<br />        &lt;ref local="transactionManager"/&gt;<br />      &lt;/property&gt;<br />      &lt;property name="target"&gt;<br />        &lt;ref local="smartmenuTarget"/&gt;<br />      &lt;/property&gt;<br />      &lt;property name="transactionAttributes"&gt;<br />        &lt;props&gt;<br />          &lt;prop key="get*"&gt;PROPAGATION_REQUIRED,readOnly&lt;/prop&gt;<br />          &lt;prop key="find*"&gt;PROPAGATION_REQUIRED,readOnly&lt;/prop&gt;<br />        &lt;/props&gt;<br />      &lt;/property&gt;<br />    &lt;/bean&gt;<br />  &lt;/beans&gt;<br /> <br />  </span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">要想彻底理解</span>
				<span style="COLOR: black">Spring</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</span>
				<span style="COLOR: black">AOP</span>
				<span lang="ZH-CN" style="COLOR: black; FONT-FAMILY: SimSun; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，最好还是多看看源码，开源就是好啊！</span>
				<span style="COLOR: black">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<span style="COLOR: black">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
				<o:p> </o:p>
		</p>
<img src ="http://www.blogjava.net/SunRiver/aggbug/81508.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/SunRiver/" target="_blank">Sun River</a> 2006-11-16 13:29 <a href="http://www.blogjava.net/SunRiver/archive/2006/11/16/81508.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>