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?
@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.
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.
@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.
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.
getSignOfLife seems has a return value, which you write a void return type in expression, which will fails the match;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;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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With