AspectJ & Maven warning: "Advice defined in ... has not been applied?" - java

I'm trying to weave some aspects at compile time into a project that becomes a WAR. The aspects advise classes that are within the same project (though in different packages).
I get the warning:
Advice not applied
My aspect is not being executed. Here's my setup:
annotation FooAnnotation.java:
package a;
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
public #interface FooAnnotation {}
aspect FooAdvice.aj:
package a;
public aspect FooAdvice {
private static final Log log = LogFactory.getLog(FooAdvice.class);
Object around() : call( #FooAnnotation * *(..)) {
log.info(String.format("Testing around"));
return proceed();
}
}
I also tried the annotation:
#Around("call( #FooAnnotation * *(..))")
public Object checkFoo( ProceedingJoinPoint joinPoint) throws Throwable {
As far as I can tell, my pointcut specification is correct, but for some reason the ajc compiler isn't playing ball.
Class FooClass.java:
package b;
#ApplicationPath("/service")
#Path("/{param}")
#Produces("application/json")
#Provider
public class FooClass {
#POST
#PUT
#Path("/{context}/{resource}")
#FooAnnotation
public String updateResource(...) {}
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>zzz.group</groupId>
<artifactId>yyy.artifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>yyy.name</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javaVersion>1.6</javaVersion>
<org.aspectj-version>1.7.2</org.aspectj-version>
</properties>
<dependencies>
<dependency>
... stuff ...
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${javaVersion}</source>
<target>${javaVersion}</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<!--
Have to use version 1.2 since version 1.3 does not appear to work
with ITDs
-->
<version>1.4</version>
<dependencies>
<!--
You must use Maven 2.0.9 or above or these are ignored (see
MNG-2972)
-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
<!-- <goal>test-compile</goal> -->
</goals>
</execution>
</executions>
<configuration>
<outxml>true</outxml>
<source>${javaVersion}</source>
<target>${javaVersion}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Aha! Found the answer here:
aspectj: why advice cannot be applied?
It has nothing to do with maven.
The reason is that in my example, the method is being called from within the jax-rs framework by indirect loading. The call() advice wants to weave the caller, but the ajc compiler can't know where the caller is. The fix is to change the advice to execution() thusly:
Object around() : execution(#FooAnnotation * *(..)) {...}
This differs because it weaves around the execution (which ajc can find), rather than the caller.
Done.

Related

Groovy Mocked methods return null when called inside code from a Library

I'm developing a Java project that has several modules. I decided to create a Module just for testing, where the "java" folder is empty and the "test" folder has all the project's unit and integration tests.
I'm having trouble with a test where I mock MyClass1's getText() method. It works fine if I call getText() somewhere within the Groovy test files, but it's returning null when it gets called from within MyClass2's methods. I suspect it has to do with how the Groovy files get compiled to allow method mocking, whereas MyClass1 and MyClass2 are already compiled since they're from a library. Here's somewhat what my code looks like:
Classes from Module1
public final class MyClass1 {
private String text;
public MyClass1() {
text = "Hello there!";
}
public String getText() {
return text;
}
}
public class MyClass2 {
private MyClass1 myObject1;
public MyClass2(MyClass1 myObject1) {
this.myObject1 = myObject1;
}
public String toLowerCase() {
return myObject1.getText().toLowerCase();
}
}
Tests:
def assertTextNotNull(MyClass1 myObject1) {
assert myObject1.getText() != null;
}
def "successful test"() {
given:
MyClass1 myMock1 = GroovyMock(MyClass1);
myMock1.getText() >> "ABC";
assertTextNotNull(myMock1);
}
def "unsuccessful test"() {
given:
MyClass1 myMock1 = GroovyMock(MyClass1); // I need to use GroovyMock here because the actual class I'm mocking is final
MyClass2 myObject2 = new MyClass2(myMock1);
myMock1.getText() >> "ABC";
when:
assertTextNotNull(myMock1);
then:
myObject2.toLowerCase() == "abc"; // Fails here because myObject1.getText() returns an empty String ""
}
My test module's pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>myGroupId</groupId>
<artifactId>myParentArtifactId</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>myArtifactId-tests</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>17</release>
</configuration>
<dependencies>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<excludes>
<exclude>**/*IT.*</exclude>
<exclude>**/*IntegrationTest.*</exclude>
</excludes>
<includes>
<include>**/*UT.*</include>
<include>**/*UnitTest.*</include>
<include>**/*UT.groovy</include>
</includes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M7</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*UT.*</exclude>
<exclude>**/*UnitTest.*</exclude>
</excludes>
<includes>
<include>**/*IT.*</include>
<include>**/*IntegrationTest.*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- The gmavenplus plugin is used to compile Groovy code. To learn
more about this plugin, visit https://github.com/groovy/GMavenPlus/wiki -->
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.13.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
<goal>addSources</goal>
<goal>addTestSources</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>papermc</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<!-- Paper/Waterfall -->
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.18.2-R0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.waterfallmc</groupId>
<artifactId>waterfall-api</artifactId>
<version>1.18-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- All other modules -->
<dependency>
<groupId>myGroupId</groupId>
<artifactId>myArtifactId-module1</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<!-- Testing related -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<!-- Enables mocking of classes without default constructor (together with CGLIB) -->
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>2.2-M1-groovy-4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
I've searched far and wide on StackOverflow. It seems like people only have issues with mocked methods returning null when they're using their own code, and not library code. Hence, I found no solution.
What can I do to make sure getText() returns the mocked reply independently from where it's called?
EDIT:
There has been a development. I basically copy-pasted the code I pasted here into a new module and made MyClass1 final just like it is in the library. It didn't work, as expected. By removing the final on the class declaration, the tests suddenly worked. So, the conclusion I can take from this is that Mocking final class's methods only works inside groovy code. Those methods can be called from within library/other module's code but will return a default value (in my case, null. In the posted code's case, an empty String).
You cannot mock final classes for Java with Spock. And GroovyMock only works for code written in (dynamic) Groovy as you suspected.
However, there is spock-mockable, it is an extension, that will use a java agent to transparently remove the final modifier from the configured classes, methods, and fields. Using this extension, you can use the standard Mock method from Spock to mock the now non-final class.
#Mockable(MyClass1)
class ASpec extends Specification {
def "unsuccessful test"() {
given:
MyClass1 myMock1 = Mock() // I need to use GroovyMock here because the actual class I'm mocking is final
MyClass2 myObject2 = new MyClass2(myMock1)
myMock1.getText() >> "ABC"
when:
assertTextNotNull(myMock1)
then:
myObject2.toLowerCase() == "abc" // Fails here because myObject1.getText() returns an empty String ""
}
}

Can I use PowerMockito in Quarkus with JUnit5?

Im am currently building a Quarkus application and for unit testing I am using JUnit5. Currently I have no other option then using PowerMockito to mock my static functions but I cant seem to find the dependencies for it in my Quarkus project.
Does anyone know what the best set of dependecies should be used in a Quarkus app for PowerMockito with JUnit5?
I guess you don't need PowerMockito, just the capability to mock static methods. Since 2.7.x (released in 2017) Mockito community made an experimental library called mockito-inline for mocking static methods or final classes. Some features (e.g. static method mocking) have already merged into mockito-core.
Additionally Quarkus has extension for Mockito.
Here is a working example
pom.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.zforgo</groupId>
<artifactId>quarkus-mockito-static</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<maven.compiler.release>11</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>2.6.1.Final</quarkus.platform.version>
<surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-mockito</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
Some utility class with static method
package io.github.zforgo;
public class SomeStaticClass {
public static String getSome() {
return "foo";
}
}
Test class
package io.github.zforgo;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.junit.jupiter.api.Assertions.assertEquals;
#QuarkusTest
public class SomeStaticClassTest {
#Test
void nonMocked() {
assertEquals("foo", SomeStaticClass.getSome(), "Something went wrong");
}
#Test
void mocked() {
// try-with-resources is recommended in case of scoped (temporary) mocking
try (var mocked = Mockito.mockStatic(SomeStaticClass.class)) {
mocked.when(SomeStaticClass::getSome).thenReturn("bar");
assertEquals("bar", SomeStaticClass.getSome(), "Something went wrong");
}
}
}

Using AspectJ annotations without Spring

I am fairly new to AOP. I am trying to create annotations in a maven project without Spring using AspectJ. However my the method I am trying to call using the #Aspect is not being called.
This is what my pom looks like :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>tanvi</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/aspectj/aspectjrt -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aspectj/aspectjweaver -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.5.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
The annotation looks like this :
#Target({ElementType.METHOD})
#Retention(RetentionPolicy.RUNTIME)
public #interface HasAccess {
Access[] accesses();
String message() default "You are not allowed to perform this operation";
}
I created an annotation processor for my annotation :
#Aspect
public class HasAccessAdvice {
// #Before("execution(* *.*(..)) && #annotation(testAnnotation) ")
#Before("execution(* *.*(..)) && #annotation(hasAccess)")
public void myBeforeLogger(JoinPoint joinPoint, HasAccess hasAccess) {
System.out.println("Okay - we're in the before handler...");
System.out.println("The test annotation value is: " + hasAccess.accesses().toString());
Signature signature = joinPoint.getSignature();
String methodName = signature.getName();
String stuff = signature.toString();
String arguments = Arrays.toString(joinPoint.getArgs());
System.out.println("Write something in the log... We are just about to call method: "
+ methodName + " with arguments " + arguments + "\nand the full toString: "
+ stuff);
}
}
I am calling it in this call :
public class TestMe {
#HasAccess(accesses = {Access.CREATE_PURCHASE})
public void createPurchase(BigDecimal bigDecimal) {
System.out.println("create Purchase called");
}
}
I created an aop.xml file and placed it in the same folder as pom.xml.
<aspectj>
<aspects>
<aspect name="HasAccessAdvice"/>
</aspects>
</aspectj>
When I call the method createPurchase, it runs without the #Before method being called first. Please help me with what I am missing. Most of the documentation/answers I found were Spring aligned. Any pointers to any tutorial or even another way of creating simple annotations without Spring would be greatly appreciated.
First, since you are using aop.xml, I assume you want to do load time weaving. See Load Time Weaving docs and docs on different weaving types.
Second, in your aop.xml file, you define which <aspect> to use, but you also need to define which class files / packages you want to weave:
<aspectj>
<aspects>
<aspect name="HasAccessAdvice"/>
</aspects>
<weaver options="-verbose">
<!-- weave anything -->
<include within="*" />
<!-- weave specific packages only -->
<include within="my.package..*" />
</weaver>
</aspectj>
Either use "*" to run your aspect on any classes or replace my.package with the package of TestMe. Note that double-dot .. includes sub-packages too.
Also note that <aspect name="..."> asks for fully-qualified Aspect-name with package. Did you create HasAccessibleAdvice in default-package? Add your package otherwise.
Third, aop.xml must be readable in META-INF/aop.xml on your classpath.
If you are running your test via CLI (using java -javaagent...), check your classpath settings (-cp).
If you are writing JUnit tests, you can put META-INF/aop.xml in src/test/resources and adjust the <build><plugins>-section of your pom.xml to include the load time weaver like this:
<properties>
<aspectj.version>1.8.9</aspectj.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<argLine>
-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

what is the purpose of #Component in OSGI factory implementations?

I am trying to implement servicefactory using apache felix scr annotations.
#Component
#Service(serviceFactory = true)
#Properties(value = { #Property(name = "className", value = "interface1") })
public class Tinterfaceimpl1 implements Tinterface {
#Override
public void consumeService() {
System.out.println("tinterfaceimpl1");
}
}
Above code is working fine. But what is the purpose of #Component? Because i am trying to expose it as a service instead of both Component & Service. If i remove the #Component state is unsatisfied. IS it really mandatory for a factory to use both component and service?
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TinterfaceImpl</groupId>
<artifactId>TinterfaceImpl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<version>2.3.5</version>
<!-- <configuration>
<instructions>
<Import-Package>com.java.serviceeg.tinterface.Tinterface</Import-Package>
</instructions>
</configuration> -->
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<version>1.14.0</version>
<executions>
<execution>
<id>generate-scr-scrdescriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Felix SCR annotations -->
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>Tinterface</groupId>
<artifactId>Tinterface</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>bundle</type>
</dependency>
</dependencies>
<packaging>bundle</packaging>
</project>
Yes this is mandatory. It declares your class as a Component according to the Declarative Services specifcation. Without the #Component annotation, it is merely some class hanging around in your bundle.
Components may also be published as services, as in this example. But components do not have to be services -- instead they might expose some kind of external interface like a server socket or a GUI.

Spring AOP + AspectJ maven plugin - Internal method call doesn't work

Java + Spring + Maven application.
Unable to make Internal call from annotation based public method.
Prerequisite
Java-Version: 1.7.
Project: AspectProject > Post build it will create jar file.
Client: AspectClient : which has dependency of "AspectProject".
AspectProject
pom.xml
<properties>
<java.version>1.7</java.version>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springframework.version>4.1.2.RELEASE</springframework.version>
<org.aspectj-version>1.7.0</org.aspectj-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- AspectJ dependencies -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<!-- compile for Java 1.7 -->
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
<configuration>
<complianceLevel>${maven.compiler.source}</complianceLevel>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
AspectProvider
#Aspect
public class AspectProvider {
/**
* This is the point cut for all get Method with #TestAnnotation annotation
*/
#Pointcut("execution(* get*()) && #annotation(aTestAnnotation)")
public void getMethodPointcut(TestAnnotation aTestAnnotation) {}
#Around("getMethodPointcut(aTestAnnotation)")
public Object getConfiguration(ProceedingJoinPoint iJoinPoint, TestAnnotation aTestAnnotation) throws Throwable {
return getValueFromISTCompositeConfiguration(iJoinPoint, aTestAnnotation);
}
private Object getValueFromISTCompositeConfiguration(final ProceedingJoinPoint iProceedingJoinPoint, TestAnnotation aTestAnnotation) throws Throwable {
Object aReturnValue = null;
if (aTestAnnotation.value() != null) {
System.out.println("ASPECT: Returning annotation value.");
aReturnValue = aTestAnnotation.value();
} else {
System.out.println("MISSING_GETTER_PROPERTY");
}
if(aReturnValue == null){
aReturnValue = iProceedingJoinPoint.proceed();
}
return aReturnValue;
}
}
Annotation "TestAnnotation"
#Component
#Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
#Retention(RetentionPolicy.RUNTIME)
public #interface TestAnnotation {
String value();
}
AspectClient
pom.xml
<properties>
<java.version>1.7</java.version>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<aspectProject.version>0.0.1-SNAPSHOT</aspectProject.version>
<spring-framework.version>4.1.2.RELEASE</spring-framework.version>
<org.aspectj-version>1.7.0</org.aspectj-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- AspectProject dependencies -->
<dependency>
<groupId>com.example.aop</groupId>
<artifactId>AspectProject</artifactId>
<version>${aspectProject.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java/</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<!-- compile for Java 1.7 -->
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<aspectLibraries>
<aspectLibrary>
<groupId>com.example.aop</groupId>
<artifactId>AspectProject</artifactId>
</aspectLibrary>
</aspectLibraries>
<complianceLevel>${maven.compiler.source}</complianceLevel>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Service Class
#Component
public class TestService {
private String value;
public void internalCall() {
System.out.println("INTERNAL_CALL :"+ getValue());
}
#TestAnnotation("RETURNED_FROM_ASPECT_CALL")
public String getValue() {
return value;
}
public void setValue(String iValue) {
this.value = iValue;
}
}
Spring context.xml
<?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/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- Enable AspectJ style of Spring AOP -->
<context:component-scan base-package="com.example.aop.client" />
<aop:aspectj-autoproxy />
<bean name="TestService" class="com.example.aop.client.service.TestService" />
<!-- Configure Aspect Beans, without this Aspects advice wont execute -->
<bean name="aspectProvider" class="com.example.aop.aspect.AspectProvider"/>
</beans>
Main class
public class SpringMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
TestService aTestService = ctx.getBean("TestService", TestService.class);
System.out.println("EXTERNAL_CALL: "+aTestService.getValue());
aTestService.internalCall();
ctx.close();
}
}
OUTPUT:
ASPECT: Returning annotation value.
EXTERNAL_CALL:RETURNED_FROM_ASPECT_CALL
INTERNAL_CALL: **null**
Expected:
ASPECT: Returning annotation value.
EXTERNAL_CALL: RETURNED_FROM_ASPECT_CALL
INTERNAL_CALL: **RETURNED_FROM_ASPECT_CALL**
Please advice in case if I am missing any entry or required to change configuration.Thanks in advance for your valuable time.
What you do is a bit strange because on the one hand you configure Spring to use (auto) proxy-based Spring AOP, on the other hand you use AspectJ Maven Plugin to use native AspectJ and do compile-time weaving. Please decide which way you want to go:
For Spring AOP you do not need the AspectJ compiler, but then you are stuck with the proxy-based "AOP lite" approach which comes at the cost of internal calls not being intercepted by aspects because they do not go through proxies but through this (the original object).
For full-blown AspectJ you can configure Spring to use LTW (load-time weaving) as described in manual chapter Using AspectJ with Spring applications. Alternatively, you can also use compile-time weaving, but this is not necessary unless you have performance problems during application start-up.

Categories