1。AOPとは何ですか
AOPは、アスペクト指向プログラミングの略語であり、アスペクト指向プログラミングを意味します。 AOPは、実際にはGOF設計モデルの継続です。
2。スプリングAOPに関するいくつかの用語:
A.アスペクト:スプリングAOPでは、一般クラスを使用してファセットを実装できます。
B. JoinPoint:Spring AOPでは、接続ポイントがメソッドの実行を表します。
C.アドバイス:セクションの特定のジョイントポイントで実行されたアクション。 「Aurver」、「Before」、「After」など、さまざまな種類の通知があります。 Springを含む多くのAOPフレームワークは、インターセプターを通知モデルとして使用し、リンクポイント中心のインターセプターチェーンを維持します。
D. PointCut:これらのメソッドを実行するときに通知を生成できるメソッドの1つまたはグループを定義します。 Springは、デフォルトでAspectJポイントカット構文を使用します。
3。通知タイプ
A.プレノット化(@before):結合ポイントの前に実行される通知ですが、この通知は接続ポイントの前に実行を防ぐことはできません(例外をスローしない限り)
B.返された後の通知(@AfterTurning):接続ポイント(結合ポイント)が正常に完了した後に実行される通知:たとえば、メソッドは例外をスローせず、正常に戻る
C.例外がスローされた後の通知(@AfterThrowing):メソッドが例外をスローして終了したときに通知が実行されます
D.投稿通知(@After):接続ポイントが終了したときに通知が実行されます(通常のリターンであるか異常な出口であるか)
E.サラウンド通知(@Around):メソッド呼び出しなどの結合ポイントを囲む通知。これは最も強力な通知タイプであり、サラウンド通知はメソッド呼び出しの前後にカスタム動作を完了することができます。また、接続ポイントの実行を継続し続けるか、自分の返品値を直接返すか、例外をスローするかを選択して実行を終了します
4。@AspectJスタイルAOP構成
Spring AOP構成には2つのスタイルがあります。
A. XMLスタイル=宣言的な形でスプリングAOPを実装する
B. aspectj style =注釈フォームを使用してスプリングAOPを実装する
5。例
tesspect
パッケージcom.spring.aop; / ***セクション*/ public class testaspect {public void doafter(joinpoint jp){system.out.println( "log endingメソッド:" + jp.getTarget()。getname()。getName() + "。" + jp.getsignature()。 } public Object Doaround(ProceedingJoinPoint PJP)スロー可能{long time = system.currenttimemillis(); Object retval = pjp.proceed(); time = system.currenttimemillis() - time; System.out.println( "Process Time:" + time + "ms");返品retval; } public void dobefore(joinpoint jp){system.out.println( "log biringing method:" + jp.getTarget()。getClass()。 } public void dothrowing(joinpoint jp、throwable ex){system.out.println( "method" + jp.getTarget()。getClass()。getName() + "。" + jp.getSignature()。 System.out.println(ex.getMessage()); } private void sendex(string ex){// todo smsまたはemail reminder}}}パッケージcom.spring.service; / ** *インターフェイスa */ public interface aservice {public void fooa(string _msg); public void bara(); }パッケージcom.spring.service; / ***インターフェイスの実装クラスA*/ public class aserviceImplを実装しますaservice {public void bara(){system.out.println( "aserviceimpl.bara()"); } public void fooa(string _msg){system.out.println( "aserviceimpl.fooa(msg:"+_ msg+")"); }}パッケージcom.spring.service; / ** *サービスクラスB */パブリッククラスBServiceImpl {public void barb(string _msg、int _type){system.out.println( "bserviceimpl.barb(msg:"+_ msg+"type:"+_ type+")"); if(_type == 1)新しいIllegalargumentException( "Test Exception"); } public void foob(){system.out.println( "bserviceimpl.foob()"); }}ApplicationContext
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns:aop = "http://www.springframework.org/schema/aop" xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/ http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd com.spring.serviceパッケージに基づくすべてのクラスまたはインターフェイス - > <aop:pointcut id = "businessservice" expression = "execution(*com.spring.service。 <aop:pointcut-ref = "businessservice" method = "doaround"/> <aop:shoppoint-cut-ref = "businessservice" method = "dothrowing"/> </aop> </aop:config> <bean id = "aspeatbean"/> <bean "aservice"> </bean> </bean
クラスAオプテストをテストします
パブリッククラスAOPTESTは、AbstractDependencyInjectionSpringContextTests {private aservice aservice;プライベートbserviceimpl bservice; protected string [] getConfiglocations(){string [] configs = new String [] {"/applicationContext.xml"}; configsを返します。 } / ***テスト通常のコール* / public void testcall(){system.out.println( "SpringTest Junit Test"); aservice.fooa( "junit test fooa"); aservice.bara(); bservice.foob(); bservice.barb( "junit test barb"、0); } / ** * test after-howing * / public void testthrow(){try {bservice.barb( "junit call barb"、1); } catch(IllegalargumentException e){}} public void setAservice(aservice service){aservice = service; } public void setbservice(bserviceimpl service){bservice = service; }}
操作結果は次のとおりです。
ログの開始方法:com.spring.service.aserviceimpl.fooa aserviceimpl.fooa(msg:junit test fooa)ログエンディング方法:com.spring.service.aserviceimpl.fooaプロセス時間:0 msログ開始方法:com.spring.service.aserviceimpl.bara aservicempl.bara aservicempl.bara aservicempl.bara aserviceimpl.bara com.spring.service.aserviceimpl.baraプロセス時間:0 msログ開始方法:com.spring.service.bserviceimpl.foob bserviceimpl.foob()log ending method:com.spring.service.bserviceimpl.foobプロセス時間:0 MSログの開始方法bserviceimpl.barb(msg:junitテストバーブタイプ:0)ログ終了方法:com.spring.service.bserviceimpl.barbプロセス時間:0 msログ開始方法:com.spring.service.bserviceimpl.barb bserviceimpl.barb(msg:junit call barb:1)log ending method: com.spring.service.bserviceimpl.barbメソッドcom.spring.service.bserviceimpl.barbスロー例外テスト例外