0

Java + Spring + Maven application:

Can somebody provide me with the link or advise me on a pure AspectJ implementation without proxy-based Spring AOP?

My application is purely Spring + Maven based. I have currently implemented aspects with Spring AOP which is not solving my problems.

If I try to access a private method2() from public method1() within the same class A this is not supported.

I would like to know : 1) How to write an aspectj with pointcut that supports intraclass method calls? 2) How to configure that into my current Spring,maven project with AspectJ load-time weaving? 3) how to configure AspectJ Maven Plugin for compile-time weaving in Tomcat server + eclipse.

@Controller
class A {
    public void method1() {
        method2("foo");
    }

    private String method2(String text) {
        return text;
    }
}

Expected output:

log.entering(method1)
log.entering(method2)
print abc
log.exiting(method2)
log.exiting(method1)
Chuck
  • 1
  • 1
  • It is not clear what is not working. Add more details to your question (aspects for example). – Aleksandr M Oct 28 '14 at 18:45
  • 2
    I have edited the question to make it somewhat comprehensible. It is clear that Spring AOP does not work with intra-class method calls, no matter if the target method is public or private, because they are not routed through the dynamic proxy. So far, so true. It is also true that native AspectJ will solve your problem. But what do you want to know? How to write an aspect with pointcut and advice? Or how to configure Spring to use AspectJ load-time weaving? Or how to configure AspectJ Maven Plugin for compile-time weaving? Please update the question. – kriegaex Oct 28 '14 at 23:01
  • I would like to know : 1) How to write an aspectj with pointcut that supports intraclass method calls? 2) How to configure that into my current Spring,maven project with AspectJ load-time weaving? 3) how to configure AspectJ Maven Plugin for compile-time weaving in Tomcat server + eclipse. – Chuck Oct 29 '14 at 14:18

2 Answers2

0

My recommendation for you is to make a dummy project with Spring Roo (so you can see how the Maven pom.xml and Spring applicationContext.xml file looks) and download Spring STS version of Eclipse which has AspectJ setup correctly.

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
0

You can use Maven and AspectJ project together by converting Maven project into AspectJ project by Right Click - Configuration - Convert to AspectJ project. From there you can create Aspect class without annotation or Java class of Aspect using annotation.

As for the result that you want, you can use Around method like below:

    @Around("execution ( * A.method1(..))")
    public void captureMethodOne(ProceedingJoinPoint joinPoint) throws Throwable{
    System.out.println("log.entering(method1)");
    joinPoint.proceed();
    System.out.println("log.exiting(method1)");
    }

Also do not forget to input aspectj and aspectj maven plugin in the pom.xml, such as

<properties>
    <maven.compiler.plugin.version>3.5.1</maven.compiler.plugin.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.7</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.8</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>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

    </plugins>
</build>

Hope it helps. PS Check AspectJ tutorial first, plenty around in the internet.

Ihsan Haikal
  • 1,085
  • 4
  • 16
  • 42