Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add AspectJ in Webservice

i have a Java EE Webservice (REST) and would now like to use AspectJ, to create a rule that will print-out every incoming service call and their params.

I just read this tutorial and implemented the following code:

POM.XML

        <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.10</version>
    </dependency>
    
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.10</version>
    </dependency>
    
</dependencies>
<build>
  <plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.7</version>
        <configuration>
            <complianceLevel>1.8</complianceLevel>
            <source>1.8</source>
            <target>1.8</target>
            <showWeaveInfo>true</showWeaveInfo>
            <verbose>true</verbose>
            <Xlint>ignore</Xlint>
            <encoding>UTF-8 </encoding>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <!-- use this goal to weave all your main classes -->
                    <goal>compile</goal>
                    <!-- use this goal to weave all your test classes -->
                    <goal>test-compile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

...and created a Test.aj File, with a Pointcut which should print-out a teststring after calling the getSignOfLife():

import de.jack.businesspartner.service.BusinessPartnerServiceImpl;
public aspect TestAspectJ {

    pointcut getSignOfLife() : 
      call(void BusinessPartnerServiceImpl.getSignOfLife());

    before() : getSignOfLife() {
        System.err.println("ADKL TEST ASPECT");
    }

}

--> But nothing happens if i call the getSignOfLife() Method. Can you help me?

like image 750
goblingift Avatar asked May 10 '17 14:05

goblingift


People also ask

How do I enable AspectJ?

@AspectJ is a style to declare aspects in a java class using annotations. To enable @AspectJ, spring AOP provides @EnableAspectJAutoProxy annotation which will be annotated in java configuration. To work with spring AOP and @AspectJ support, we need to create a class annotated with @Aspect annotation.

What is AspectJ used for?

AspectJ Provides a Standard Mechanism to Handle a Crosscutting Concern. In the example shown above, we are doing a null validation and throwing an IllegalArgumentException when the request is null. This way we make sure that whenever an argument is null, we get the same uniform behavior.

What is the use of AspectJ in Spring?

@AspectJ refers to a style of declaring aspects as regular Java classes annotated with annotations. The @AspectJ style was introduced by the AspectJ project as part of the AspectJ 5 release. Spring interprets the same annotations as AspectJ 5, using a library supplied by AspectJ for pointcut parsing and matching.


1 Answers

Your point cut expression may be wrong:

pointcut getSignOfLife() : 
  call(void BusinessPartnerServiceImpl.getSignOfLife());

The expression in call should match the signature of your real method.

CheckList

  • return type: getSignOfLife seems has a return value, which you write a void return type in expression, which will fails the match;
  • parameter type: you can use f(..) to represent a method f with any parameter, but if you left it empty, it means no parameter. If your actual method has parameter, it will fails the match;
  • notice that int and Integer is different in expression, because primitives and boxing type is different;
  • you need original compiler plugin of maven in your pom;

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    

More

  • You may refer to here if you want to dive into AspectJ
  • In the most common cases, Spring AOP is enough to handle jobs like logging, auth etc, which can be used with annotation and I think, is more convenient.
  • Runnable code.
like image 139
Tony Avatar answered Sep 28 '22 08:09

Tony