AOP : java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut
Asked Answered
S

14

14

I am new to AOP. I got some problem like this.

package org.suman.Aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class LoginAspect {
    //@Before("execution(public String getName())")
    //@Before("execution(public String org.suman.Model.Triangle.getName())")
    //@Before("execution(* get*())")
    //@Before("execution(* get*(..))")
    //@Before("execution(* org.suman.Model.*.get*())")

    //@Before("execution(* get*())&& within(org.suman.Model.Circle)")
    @Before("execution(* get*())&& allCircle()")
    //@Before("allGetters() && allCircle()")
    public void LoginAdvice()
    {
        System.out.println("Advice run.. getMethod is called");
    }

    @Before("execution(* get*())")
    //@Before("allGetters()")
    public void SecondAdvice()
    {
        System.out.println("this is a second Advice");
    }
    @Pointcut("execution(* get*())")
    public void allGetters(){}

    //@Pointcut("execution (* * org.suman.Model.Circle.*(..))")
    @Pointcut("within(org.suman.Model.Circle)")
    public void allCircle(){}

} 

when using pointcut, the method allGetters() to LoginAdvice method, if I use @Before("execution(* get*())") then no error but if I use @Before("allGetters()") then gives error

java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut allGetters

if I use @Before("execution(* get*())&& within(org.suman.Model.Circle)") instead of method name it works.

My xml like this:

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <!-- <context:annotation-config /> -->
    <aop:aspectj-autoproxy />

    <bean name="triangle" class="org.suman.Model.Triangle">
        <property name="name" value="Triangle Name"></property>
    </bean>
    <bean name="circle" class="org.suman.Model.Circle">
        <property name="name" value="Circle name"></property>
    </bean>
    <bean name="shapeService" class="org.suman.Services.ShapeService"
        autowire="byName"></bean>
    <bean name="loginAspect" class="org.suman.Aspect.LoginAspect"></bean>

</beans>

Please solve the problem with pointcut by which it takes method

Safari answered 7/5, 2012 at 13:0 Comment(1)
For those who came to this thread and none of the proposed solutions on this page worked, try taking a look here: #21280216Ambo
B
29

I had this problem - using @Pointcut on a 'placeholder' method was giving me the "can't find referenced pointcut" error.

Solved simply by updating the AspectJ libraries with the maven dependencies from this:

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.5.4</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.5.4</version>
    </dependency>

to this

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.7.0</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.7.0</version>
    </dependency>
Behoove answered 27/9, 2012 at 20:46 Comment(3)
For curious people why it's failing with 1.5.4 and working with 1.7.0: https://mcmap.net/q/562376/-error-when-using-aspectj-aop-with-java-7Samhita
Pretty cool...its unusual but yeah changing to latest worked for me...thanks @BehooveTearjerker
i am using java8 , tried with aspectj 1.7.0 and 1.8.10 but still facing the issueInn
N
3

I ran thjrough the same problem. Once i replace the aspectjweaver with the aspectjweaver-1.6.11.M2.jar version. Everything started working smoothly.

Neper answered 29/1, 2013 at 0:44 Comment(0)
I
2

The problem can also be caused by running an early access version of JDK9.

Maven might be preferring a newer version of Java over the JVM in PATH.

In my case I'm running Spring with Swagger2 via Maven on Ubuntu 15.04, and I have installed java-8-oracle and java-9-oracle (and a few more versions). My java -version, derived from PATH, says 1.8.0_72, yet when I run Maven and change /usr/bin/mvn to echo JAVA_HOME, it shows to have selected /usr/lib/jvm/java-9-oracle.

Exporting JAVA_HOME to /usr/lib/jvm/java-8-oracle made Maven use the desired version of Java, and that made the problems with AOP weaving of pointcuts for Spring's dependency injection wiring go away.

Interpenetrate answered 27/1, 2016 at 17:41 Comment(0)
G
2

Use aspectjweaver and aspectjrt versions 1.8.10

Gwenny answered 15/4, 2018 at 16:4 Comment(0)
A
1

I also got similar issue with @Before advice not accepting @Pointcut method name as argument. I got this fixed by changing the dependencies version to:

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.6.11</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.6.11</version>
</dependency>

Its working fine for me now.

Astonishment answered 24/1, 2018 at 9:31 Comment(0)
R
1

Encountered the same error in the different use case.

enter image description here

UseCase : Tried to call a pointcut which is present in different aspect class.

logServicePointCut1() pointcut present in LogService

package com.opensource.kms;
@Aspect
@Component
public class LogService {
    
    @Pointcut("execution(* com.opensource.kms.AccountService.my*(..))")
    public void logServicePointCut1() {
    }
}

Try to call in SecurityService

@Aspect
@Component
public class SecurityService {

    @Before("logServicePointCut1()")  /*Need to give fully Qualified name*/
    public void verifyUserBeforeSecurityService() {
        System.out.println("verifyUserBeforeSecurityService");
    }
}

Solution: Need to pass fully qualifed name com.opensource.kms.LogService.logServicePointCut1()

Hope this would helpful for someone!!

Renin answered 1/5, 2020 at 9:6 Comment(0)
M
0

I believe you need to put another wildcard in front of:

@Pointcut("execution(* get*())")

change it to:

@Pointcut("execution(* *get*())")
Modernism answered 7/5, 2012 at 13:7 Comment(0)
O
0

Change the aspectjeaver version to latest and deploy it...

Ovate answered 3/8, 2012 at 8:43 Comment(1)
Please improve your post by explaining why your post solves the problem.Therontheropod
A
0

You should change the aspectJWeaver version to be 1.6.x

Adjutant answered 4/8, 2012 at 19:26 Comment(0)
A
0

I had this issue because the project was compiled in Java 1.6, but tomcat server started in Java 8.

Arcadia answered 22/5, 2017 at 17:55 Comment(0)
A
0

It is the problem of dependencies. Following jars or dependencies of the exactly same version should be used to avoid such problems:

<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.2</version>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>

<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>

<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>

<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
Audwin answered 13/3, 2019 at 14:22 Comment(1)
They are compatible with each other for AOP.Audwin
M
0

Add this maven dependencies. This will resolve your issue.

Change the version to 1.6.12

March answered 23/8, 2019 at 6:52 Comment(1)
What is "this maven dependencies"? Change version of what?Airburst
C
0

Please use below maven dependencies - I hope it will work

  1. aspectjrt 1.8.14
  2. aspectjweaver 1.8.14
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.8.14</version>
</dependency>

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.14</version>
    <scope>runtime</scope>
</dependency>
Chifforobe answered 8/1, 2021 at 19:9 Comment(0)
A
0

In my case, it was an error in the name of the method pointed out in pointcut in @AfterThrowing

enter image description here

All you had to do was adjust the method name to fit the note in AfterThrowing, and that's it!

enter image description here

Amends answered 22/11, 2023 at 1:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.