Search code examples
springgradleaspectj

AOP Advice not getting executed


I am using AspectJ AOP with spring. I am NOT USING spring AOP but AspectJ AOP with spring.

I am using load time weaving. My advices are not getting executed. Any leads would greatly help.

@Aspect
public class ProfilingAspect {

    @Around("methodsToBeProfiled()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("Hello #################");
        return pjp.proceed();
    }

    @Pointcut("execution(public * com.core.search.domain.AIESearchDomainService.doSearch())")
    public void methodsToBeProfiled() {
    }
}

Following is the method on which I am trying to run the advice.

package com.core.search.domain;


public class AIESearchDomainService {

    public SearchResponse doSearch() {
        return null;
    }
}

I have enabled load time weaving using the the below annotation.

@EnableLoadTimeWeaving

I have also specified the java agent for enabling the load time weaver as below.

-javaagent:"pathTo/spring-instrument-5.3.25.jar"

I have also created aop.xml and kept it in /resources/META-INF folder.

<aspectj>

    <weaver>
        <!-- only weave classes in our application-specific packages -->
        <include within="com.core.search.domain.*"/>
    </weaver>

    <aspects>
        <!-- weave in just this aspect -->
        <aspect name="package.for.ProfilingAspect"/>
    </aspects>

</aspectj>

There are no errors in the code but my advice is not getting executed. Please help. I believe weaving is happening because I am able to see the following log.

[AppClassLoader@18b4aac2] warning javax.* types are not being woven because the weaver option '-Xset:weaveJavaxPackages=true' has not been specified

Solution

  • Somehow, AOP works with the following aop.xml file.

    <aspectj>
    
        <weaver>
            <!-- only weave classes in our application-specific packages -->
            <include name="package.for.ProfilingAspect"/>
            <include within="com.core.search.domain.*"/>
        </weaver>
    
        <aspects>
            <!-- weave in just this aspect -->
            <aspect name="package.for.ProfilingAspect"/>
        </aspects>
    
    </aspectj>
    

    Just including the Aspect class in weaver tag made it work, I am yet to understand how.

    Hope this helps !!