Spring 3.x公司專屬應用程式開發實戰(9)----AOP前置增強

來源:互聯網
上載者:User

AOP聯盟為增強定義了org.aopalliance.aop.Advice介面,Spring中增強型別有五種,按增強位置分為以下5類。

1、前置增強:org.springframework.aop.BeforeAdvice代表前置增強,因為Spring只支援方法級增強,所以MethodBeforeAdvice是目前可用的前置增強,表示在目標方法執行前實施增強,而BeforeAdvice是為了將來版本擴充需要而定義的。

下面是前置增強執行個體:

首先Waiter介面:

package com.smart.advice;public interface Waiter {void greetTo(String name);void serveTo(String name);}

NaiveWaiter.java

package com.smart.advice;public class NaiveWaiter implements Waiter {@Overridepublic void greetTo(String name) {System.out.println("Greet to "+name+"...");}@Overridepublic void serveTo(String name) {System.out.println("Serving to "+name+"...");}}
GreetingBeforeAdvice.java

package com.smart.advice;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class GreetingBeforeAdvice implements MethodBeforeAdvice{@Overridepublic void before(Method arg0, Object[] arg1, Object arg2)//在目標類方法調用前執行throws Throwable {String clientName=(String)arg1[0];System.out.println("How are you! Mr."+clientName+".");}}

BeforeAdviceTest.java

package com.smart.advice;import org.junit.Before;import org.junit.Test;import org.springframework.aop.BeforeAdvice;import org.springframework.aop.framework.ProxyFactory;public class BeforeAdviceTest{private Waiter target;private BeforeAdvice advice;private ProxyFactory pf;@Beforepublic void init(){target=new NativeWaiter();advice=new GreatingBeforeAdvice();pf=new ProxyFactory();//Spring提供的代理工廠pf.setTarget(target);//設置代理目標pf.addAdvice(advice);//為代理目標添加增強}@Testpublic void beforeAdvice(){Waiter proxy=(Waiter)pf.getProxy();proxy.greetTo("John");proxy.serveTo("Tom");}}

正如我們所預計的結果:

How are you! Mr.John.
Greet to John...
How are you! Mr.Tom.
Serving to Tom...

2、通過在Spring中的配置實現增強:

<bean id="greetingAdvice" class="com.smart.advice.GreatingBeforeAdvice"/><bean id="target" class="com.smart.advice.NativeWaiter"/><bean id="waiter" class="org.springframework.aop.framework.ProxyFactoryBean"p:proxyInterfaces="com.smart.advice.Waiter"p:interceptorNames="greetingAdvice"p:target-ref="target"/>


SpringAdviceTest.java

waiter.serveTo("Tom");package com.smart.advice;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringAdviceTest {@Testpublic void testAdvice(){String configPath="com/smart/advice/beans.xml";ApplicationContext ctx=new ClassPathXmlApplicationContext(configPath);Waiter waiter=(Waiter)ctx.getBean("waiter");waiter.greetTo("John");waiter.serveTo("Tom");}}

實現同等效果。


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.