Kwadz
Kwadz

Reputation: 2232

How to use all AspectJ pointcut designators on a Spring based application?

I have a Spring project managed by Maven. Instead of using execution pointcut designator like the following:

@Before("execution(* my.app.TestUtil.myStaticClass(..))")
public void logBeforeTestUtil(JoinPoint joinPoint) {
    System.out.println("*** hijacked : " + joinPoint.getSignature().getName());
}

I want to use call pointcut designator like:

@Before("call(* my.app.TestUtil.myStaticClass(..))")
public void logBeforeTestUtil(JoinPoint joinPoint) {
    System.out.println("*** hijacked : " + joinPoint.getSignature().getName());
}

However, my IDE (IntelliJ) tells me call pointcut designator isn't supported by Spring, even after I removed spring-aop in the pom.xml.

I'd like to mention the example above with execution pointcut designator is working and Spring AOP does not supports all AspectJ pointcut designators.

This answer shows how to use Load Time Weaving without using Spring. I am wondering if we can use Spring + AspectJ without Spring AOP. If so, how to do it?

EDIT: add pom.xml and context configuration files

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>my.app</groupId>
    <artifactId>myapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <spring.version>4.3.4.RELEASE</spring.version>
        <jackson.version>2.7.3</jackson.version>
        <aspectj.version>1.8.4</aspectj.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <aspectj.version>1.8.4</aspectj.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.springframework</groupId>-->
            <!--<artifactId>spring-aop</artifactId>-->
            <!--<version>${spring.version}</version>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>
</project>

application-context.xml

package my.app;

import org.springframework.context.annotation.*;

@Configuration
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
public class AppConfig {
}

META-INF/aop.xml

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver>
        <include within="my.app.utils.*"/>
        <include within="my.app.aspect.*"/>
    </weaver>
    <aspects>
        <aspect name="my.app.aspect.LoggingAspect" />
    </aspects>
</aspectj>

Upvotes: 2

Views: 3983

Answers (1)

sheltem
sheltem

Reputation: 3825

For execution the called code must be advised. For call the calling code must be advised. Is the calling code within the scope of my.app.utils.* or my.app.aspect.*? Those are the only packages you configured your weaver to include. If the caller is from org.springframework... or other parts of your application, it will not be advised.

Converted to answer from previous comment.

Upvotes: 2

Related Questions