Search code examples
javamavenaspectjspring-aopaspectj-maven-plugin

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

  1. Java-Version: 1.7.
  2. Project: AspectProject > Post build it will create jar file.
  3. Client: AspectClient : which has dependency of "AspectProject".

AspectProject

  1. 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>
  1. 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;
    }
}
  1. Annotation "TestAnnotation"
@Component
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {  
    String value();
}

AspectClient

  1. 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>
  1. 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;
    }

}
  1. 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>
  1. 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.


Solution

  • 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.