📜 ⬆️ ⬇️

AOP in action. AspectJ (CTW) + Spring + LTW

I decided to implement AOP logging on the project and did not implement it. How and why, in fact, I want to share.

I will not describe the essence and principles of AOP, but I will describe only those problems that I encountered and whose solutions took a lot of time.
I had Spring, WebLogic, google.com and a project where I wanted to implement AOP logging. I have to say right before that I have never worked with AOP.

Problem number 1


Spring AOP - uses proxy-based approach.
')
If we have a class (ClassA) with methods (methodA, methodB), then methodB () calls methodA () and an aspect (say after) that must be executed when calling 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"); } } 

And a certain class which, within the framework of some logic, makes the call of these methods:
 public void execute() { // ..... classA.methodA(); classA.methodB(); // ..... } 

The result of such a call (using the standard Spring AOP) will be:
 methodA Aspect on method A methodB methodA 

And yet, the second time aspect does not work. The documentation is well described the principle of operation of Spring-AOP, after reading it, everything falls into place. This is the starting point.

Problem number 2


Methods must be public. There are no comments here.

So, reading the documentation and other informative literature, I found the following solution:

Since I found good LTW documentation, I decided to use it. Price issue:
  1. Now we don’t have a single .xml file where we nicely fold our pointcuts and aspects.
  2. We need to add a new aop.xml, where we have to specify our weavers (classes that are directly involved in the process), aspects.
  3. Pointcuts now appear directly above aspect.
     @Before( "execution(* com.solutions. web.test.WebTestClass.testA())") public void testALog() {} 
  4. Above the aspect classes, the @Aspect annotation @Aspect .
  5. You need to add an argument when running JM / WebLogic:
     -javaagent:${PATH_TO_LIB }/aspectjweaver.jar 

Note

If you look at the example given in the documentation (aop.context):
  <weaver> <include within="foo.*"/> </weaver> <aspects> <aspect name="foo.ProfilingAspect"/> </aspects> 

Yes, everything works, but one BUT - we will rarely store our executable code and the aspect code directly in one class / package. This little detail they missed in the description. So, if we have a class (ClassA) and an aspect (AspectA) that are in different packages, then the following aop.xml will be a valid configuration:
  <weaver> <include within="com.example.ClassA"/> <!--     --> <include within="com.log.* "/> <!—    > </weaver> <aspects> <aspect name="com.log.AspectA"/> </aspects> 

In tag + .

â„– 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;
    + .

    â„– 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
    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;
    + .

    â„– 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;

The problem disappears immediately after turning off LTW.

TOTAL


What I made for myself and would like to add:
  1. For all public top-level methods (EAR / APP, WEB level), you can use Spring AOP.
  2. For the entire level of non-public WEB and non-top-level methods, you can use LTW (if W is not used).
  3. For the entire APP level is not public and non-top level methods can be used CTW (if LTW is not used).
  4. In the tag of app.context “weav” aspect-.
    CTW and LTW are not compatible technologies.

Source: https://habr.com/ru/post/186940/


All Articles