1

i am trying to run a Spring AOP demo program using Spring Advice parameters. I am getting exception "java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut" while executing the below code. Please help me to understand what is wrong with the below code.

Performance.java

package com.aop.annotations.example4;

public interface Performance {

    public void perform(int performanceNum, String performanceName) throws Exception;

    public void buyTicket(int price) throws Exception;
}

CircusPerformance.java

package com.aop.annotations.example4;

public class CircusPerformance implements Performance {

    @Override
    public void perform(int performanceNum, String performanceName) throws Exception {
        System.out.println("Circus Performance number:"+performanceNum+" and Performance name :"+performanceName+" in progress..");
    }

    @Override
    public void buyTicket(int price){
        System.out.println("Buy Ticket for Circus performance");
    }
}

DancePerformance.java

package com.aop.annotations.example4;

public class DancePerformance implements Performance{

    @Override
    public void perform(int performanceNum, String performanceName) throws Exception {
        System.out.println("Dance Performance number:"+performanceNum+" and Performance name :"+performanceName+" in progress..");
    }

    @Override
    public void buyTicket(int price) throws Exception {
        System.out.println("Buy Ticket for Dance performance");
    }
}

Audience.java

package com.aop.annotations.example4;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class Audience {
    @Pointcut("execution(* com.aop.annotations.example4.Performance.*(..)) && args(int,String)")
    public void performance() {
    }

    @Pointcut("execution(* com.aop.annotations.example4.Performance.*(..)) && args(int)")
    public void buyTicket() {
    }

    @After("buyTicket()")
    public void afterTicket(JoinPoint jp, int price) {
        System.out.println("Start of method: " + jp.getSignature().getName() + " of Class: "
                + jp.getTarget().getClass().getSimpleName());
        System.out.println("Buying Ticket of Price :" + price);
        System.out.println("Silencing cell phones");
        System.out.println("Taking seats");
    }

    @Before("performance()")
    public void beforePerformance(JoinPoint jp, int performanceNum, String performanceName) {
        System.out.println("Start of method: " + jp.getSignature().getName() + " of Class: "
                + jp.getTarget().getClass().getSimpleName());
        System.out.println("Performance Number :" + performanceNum + "+ is :" + performanceName);
    }

    @After("performance()")
    public void afterPerformance(JoinPoint jp,int performanceNum, String performanceName) {
        System.out.println("End of method: " + jp.getSignature().getName() + " of Class: "
                + jp.getTarget().getClass().getSimpleName());
        System.out.println("End of PerformanceName :" + performanceName);
        System.out.println("CLAP CLAP CLAP!!!");
    }

}

TestAOPMain.java

package com.aop.annotations.example4;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAOPMain {

    public static void main(String args[]) {
        try {
            ApplicationContext context = new ClassPathXmlApplicationContext("appContext4.xml");
            Performance dancePerformance = context.getBean("dance", DancePerformance.class);
            dancePerformance.buyTicket(100);
            dancePerformance.perform(1,"Bhangra Dance");
            dancePerformance.perform(2,"Garba Dance");
            dancePerformance.perform(3,"Bharatnatyam Dance");
            dancePerformance.perform(4,"Folk Dance");

            Performance circusPerformance = (CircusPerformance) context.getBean("circus");
            circusPerformance.buyTicket(200);
            circusPerformance.perform(1,"Ball Juggling");
            circusPerformance.perform(2,"Animal act");
            circusPerformance.perform(3,"Rope Jump");
            circusPerformance.perform(4,"Magic Show");

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

}

aopContext4.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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.aop.annotations.example4" />
    <aop:aspectj-autoproxy proxy-target-class="true" />
    <bean id = "dance" class="com.aop.annotations.example4.DancePerformance" />
    <bean id = "circus" class="com.aop.annotations.example4.CircusPerformance" />
</beans>

Exception:

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.event.internalEventListenerProcessor': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 
Error creating bean with name 'org.springframework.context.event.internalEventListenerProcessor': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 

1 Answer 1

1

If you do not need the method argument values in your aspect's advice, you should not use args(), but just specify the method signature in your pointcut like this:

@Pointcut("execution(* com.aop.annotations.example4.Performance.*(int, String))")
public void performance() {}

@Pointcut("execution(* com.aop.annotations.example4.Performance.*(int))")
public void buyTicket(int price) {}

Only if you do need access to the arguments, as your code implies, you should use args(), but then you also need to add the parameters to the pointcut, not just to the advice. If you define the pointcut directly in the advice, you only need to do it once. Defining pointcuts separately IMO is only helpful if you want to re-use the same pointcut in multiple advices in the same aspect. Anyway, what you want to do is this in order to fix your code (I did not run it, just writing this "hands-free"):

@Pointcut("execution(* com.aop.annotations.example4.Performance.*(..)) && args(performanceNum, performanceName)")
public void performance(int performanceNum, String performanceName) {}

@Pointcut("execution(* com.aop.annotations.example4.Performance.*(..)) && args(price)")
public void buyTicket(int price) {}

BTW, the error message "formal unbound in pointcut" means that you did not bind the formal parameters in your advice method's signature correctly in via args(), this(), target() or @target(). Or the other way around, the pointcut syntax is correct but the method signature is wrong.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.