動作中のAOP。 AspectJ(CTW)+スプリング+ LTW

プロジェクトにAOPロギングを実装することにしましたが、実装しませんでした。 実際、どのように、そしてなぜ、共有したいのか。



AOPの本質と原理については説明しませんが、私が遭遇し、解決に多くの時間を要した問題のみを説明します。

Spring、WebLogic、google.com、およびAOPロギングを実装したいプロジェクトがありました。 その前に、AOPで働いたことは一度もなかったとすぐに言います。



問題番号1



Spring AOP-プロキシベースのアプローチを使用します。



メソッド(methodA、methodB)を持つクラス(lassA)がある場合、methodB()はmethodA()を呼び出し、methodA()が呼び出されたときに実行する必要があるアスペクト(後)を呼び出します。



public class ClassA { public void methodA() { System.out.println("methodA"); } public void methodB() { System.out.println("methodB"); methodA(); } } public class AspectClass { public void aspectMethodA() { System.out.println("Aspect on method A"); } }
      
      





そして、ある種のロジックのフレームワーク内で、これらのメソッドを呼び出す特定のクラス:

 public void execute() { // ..... classA.methodA(); classA.methodB(); // ..... }
      
      





このような呼び出しの結果(標準のSpring AOPを使用)は次のようになります。

 methodA Aspect on method A methodB methodA
      
      





そして、これでアスペクトは2度目は機能しなくなります。 ドキュメントはSpring-AOPの原理をよく説明しており、それを読んだ後、すべてが適切に配置されます。 これが出発点です。



問題番号2



メソッドはパブリックでなければなりません。 コメントはありません。



そのため、ドキュメントやその他の参考文献を読んだ後、次の解決策を見つけました。



LTWに関する優れたドキュメントを見つけたので、使用することにしました。 発行価格:

  1. これで、ポイントカット、アスペクトを美しく配置した1つの.xmlファイルはありません。
  2. 新しいaop.xmlを追加する必要があります。ここで、ウィーバー(プロセスに直接関与するクラス)の側面を指定する必要があります。
  3. ポイントカットポイントは、アスペクトポイントのすぐ上に表示されるようになりました。
     @Before( "execution(* com.solutions. web.test.WebTestClass.testA())") public void testALog() {}
          
          



  4. @Aspect



    アノテーションがアスペクトクラスの上に表示されます。
  5. JM / WebLogicの起動時に引数を追加する必要があります。
     -javaagent:${PATH_TO_LIB }/aspectjweaver.jar
          
          





ご注意


ドキュメント (aop.context)に記載されている例を見ると:

  <weaver> <include within="foo.*"/> </weaver> <aspects> <aspect name="foo.ProfilingAspect"/> </aspects>
      
      





はい、すべて機能しますが、1つですが、実行コードとアスペクトコードを1つのクラス/パッケージに直接保存することはほとんどありません。 彼らは説明のこの小さな詳細を見逃した。 したがって、異なるパッケージにあるクラス(ClassA)とアスペクト(AspectA)がある場合、次のaop.xmlは有効な構成になります。

  <weaver> <include within="com.example.ClassA"/> <!--     --> <include within="com.log.* "/> <!—    > </weaver> <aspects> <aspect name="com.log.AspectA"/> </aspects>
      
      





タグ内 + .







№ 3

LTW EAR/APP .

"As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

.



, CTW. :

- , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

№ 4

CTW+LTW .



, LTW classpath TW :

java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;




           +    . 
      



№ 3

LTW EAR/APP .

"As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

.



, CTW. :

- , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

№ 4

CTW+LTW .



, LTW classpath TW :

java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;








+ .







№ 3

LTW EAR/APP .

"As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

.



, CTW. :

- , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

№ 4

CTW+LTW .



, LTW classpath TW :

java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;




+ .







№ 3

LTW EAR/APP .

"As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

.



, CTW. :

- , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

№ 4

CTW+LTW .



, LTW classpath TW :

java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;




+ .







№ 3

LTW EAR/APP .

"As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

.



, CTW. :

- , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

№ 4

CTW+LTW .



, LTW classpath TW :

java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;




+ .







№ 3

LTW EAR/APP .

"As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

.



, CTW. :

- , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

№ 4

CTW+LTW .



, LTW classpath TW :

java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;




    + .







    № 3

    LTW EAR/APP .

    "As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

    .



    , CTW. :

    - , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

    № 4

    CTW+LTW .



    , LTW classpath TW :

    java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;




  1. + .







    № 3

    LTW EAR/APP .

    "As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

    .



    , CTW. :

    - , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

    № 4

    CTW+LTW .



    , LTW classpath TW :

    java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;




  2. + .







    № 3

    LTW EAR/APP .

    "As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

    .



    , CTW. :

    - , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

    № 4

    CTW+LTW .



    , LTW classpath TW :

    java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;




  3. + .







    № 3

    LTW EAR/APP .

    "As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

    .



    , CTW. :

    - , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

    № 4

    CTW+LTW .



    , LTW classpath TW :

    java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;




  4. + .







    № 3

    LTW EAR/APP .

    "As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

    .



    , CTW. :

    - , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

    № 4

    CTW+LTW .



    , LTW classpath TW :

    java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;




  5. + .







    № 3

    LTW EAR/APP .

    "As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

    .



    , CTW. :

    - , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

    № 4

    CTW+LTW .



    , LTW classpath TW :

    java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;




  6. + .







    № 3

    LTW EAR/APP .

    "As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

    .



    , CTW. :

    - , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

    № 4

    CTW+LTW .



    , LTW classpath TW :

    java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;




+ .







№ 3

LTW EAR/APP .

"As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

.



, CTW. :

- , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

№ 4

CTW+LTW .



, LTW classpath TW :

java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;




+ .







№ 3

LTW EAR/APP .

"As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

.



, CTW. :

- , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

№ 4

CTW+LTW .



, LTW classpath TW :

java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;




+ .







№ 3

LTW EAR/APP .

"As Costin said, there is unfortunately nothing we can do about this. Load-time weaving only works for specific deployment units such as WARs, and even there it is considered an advanced feature that won't work in all runtime environments.”

.



, CTW. :

- , aspect- pointcut-. C pointcut-, . ajc-, (ant, maven, gradle…).

№ 4

CTW+LTW .



, LTW classpath TW :

java.lang.Exception: java.lang.NoSuchMethodError: com.aop.example.log.AspectA.aspectOf()Lcom/aop/example/log/AspectA;






LTWを無効にすると、問題はすぐに消えます。



合計



私が自分自身のために耐えたものと追加したいもの:

  1. すべてのパブリックトップレベルメソッド(EAR / APP、WEBレベル)で、Spring AOPを使用できます。
  2. WEBレベル全体では、パブリックではなく、トップレベル以外のメソッドでLTWを使用できます(CTWを使用しない場合)。
  3. APPレベル全体では、パブリックではなく、非トップレベルのメソッドでCTWを使用できます(LTWを使用しない場合)。
  4. app.context “weav” aspect-.



    タグで app.context “weav” aspect-.





    CTWとLTWは互換性のある技術ではありません。




All Articles