最初のタイプ:注釈構成AOP
注釈構成AOP(AspectJクラスライブラリを使用して実装)、ほぼ3つのステップに分割します。
1. Annotation @Aspectを使用してセクションを定義し、エントリポイント(@pointCut)と通知タイプ(@before、@afterreturning、after、@afthrowing、@arourd)を定義します。
2。傍受する必要があるクラスを開発します。
3。もちろん、セクションをXMLに設定します。この場合、Spring AOPコンテナによって管理されます。
さらに、AspectjのJARパッケージを参照する必要があります:aspectjweaver.jar aspectjrt.jar
例:
user.javapackage com.bjsxt.model {private string getusername() (){パスワードを返す} setPassword {password =}}} {public void save(user user);
インターフェイスを実装します。
com.bjsxt.dao.impl void save(user user){system.out.println( "user save11d!");
オペレーションクラス:
com.bjsxt.service.annotation.beans.Annotation.Autowired.Beans.Annotation.Spring.SpringFramework.StringFrameWork.Spring.Annotation; com.bjsxt.dao.userdao; ;} public void add(userdao.save(user); userdao;} public void destroy()
AOPに参加してください
パッケージcom.bjsxt.aspectj.lang.Annotation.aspectj.Annotation.aspectj.lang.Annotation; aspectj.lang.ang.lang.annotation.pointcut.springframework.stereepone.component; . bjsxt.service..*.add(..))") public void myMethod(){}; /*@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjs xt .model .user)) ")*/ @before(" mymethod() ")public void before(){system.out.println(" method staet ");} @after(" mymethod() ")public void (){system.out.println( "method after"); @afterturning( "execution(public*com.bjsxt.dao ..*。 .println( "メソッド後も") ");}}
構成ファイル
<?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:context =" http://www.springframework.org/schema/context "xmlns:aop =" http://www.springframework.org/sche ma/aop "xsi:schemalotion = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context htp: /www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/sc hema/aop/spring-aop -3.1 .xsd "> <! - 最後の2行を追加するには - > <context:annotation-config /> <context:component-scanベースパッケージ=" com.bjsxt " /> <! - 自動スキャン - > <aop:aspectj-autoproxy/> <! - この行を追加する - > </beans>
テストクラス:
com.bjsxt.junit.test.context.applicationcontext;コントロールのパブリッククラスuserservicetest {@test public void testadd()throws {classpathxmlapplicationcontext = new classpathxmlappliccontext( "applicationcontext.xml"); println(service.getclass.add(new system.out.println);
結果:
クラスcom.bjsxt.service.userservice $$ enthancerbycglib $ 7b201784method統計save11d!メソッド後のメソッド###
知らせ:
@Aspect:それはこのクラスがファセットクラスであることを意味します
@componet:スプリングは接線クラスとして管理する必要があるため、このクラスの初期化は、初期化中にスプリング管理に追加する必要があります。
@befoe:エントリポイントの論理(アドバイス)
実行…:ポイントカット構文
2番目のタイプ:XML構成AOP
上記と同じ:構成ファイルだけが異なる
<?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:context =" http://www.springframework.org/schema/context "xmlns:aop =" http://www.springframework.org/sche ma/aop "xsi:schemalotion = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context htp: /www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/sc hema/aop/spring-aop -3.1 .xsd "> <! - 最後の2行を追加するには - > <context:annotation-config /> <context:component-scans base-package =" com.bjsxt " /> <bean id =" loginterceptor "> < /bean> <aop:config> <aop:pointcut expression =" execution(public * com.bjsxt.service .. *。add(..)) "id =" servicepointcut " /> <aop:aspe = "lo gaspect" ref = "loginterceptor"> <aop:method = "before" pointcut-ref = "servicepointcut"/> </aop:aspect> </aop:config> </beans>
次の<beans>は、スプリング構成タグであり、豆のいくつかの重要な特性です。
xmlns:
これは、デフォルトのXMLドキュメント解析形式、つまりスプリングビーンズです。アドレスはhttp://www.springframework.org/schema/beansです。
このプロパティを設定することにより、豆で宣言されたすべての属性は、<bean>など、<>を通して直接使用できます。
XMLNS:XSI:
これは、XMLがURLを介して順守する必要があることです。
xmlns:aop:
これが重要なポイントであり、ここで使用する必要があるセマンティック仕様であり、接線指向のAOPに関連しています。
XMLNS:TX:
春のトランザクション関連の構成コンテンツ。
XMLファイルは、デフォルトのセマンティック解析仕様のみを宣言できます。
たとえば、上記のXMLでは、1つの豆のみがデフォルトであり、他の豆はAOPなどの特定のタグを使用して使用する必要があります。 。たとえば、AOP:上記の設定。
同様に、デフォルトのXMLNSがAOP関連のセマンティック解析仕様で構成されている場合、XMLでconfigのようなタグを直接書き込むことができます。
上記は、スプリングAOPの2つの構成方法です。 Spring AOP構成方法に関する記事は、後で注意を払う必要があります。