Spring 4 AOP:在切入点中获取异常java.lang.IlLegalArgumentException:Error at::0 Formal Unbinded in PointCut [英] Spring 4 AOP : Getting exception java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut

查看:0
本文介绍了Spring 4 AOP:在切入点中获取异常java.lang.IlLegalArgumentException:Error at::0 Formal Unbinded in PointCut的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring建议参数运行一个Spring AOP演示程序。在执行下面的代码时,我收到异常"java.lang.IlLegalArgumentException:Error at::0 Formal Unbinded in PointCut"。请帮助我了解以下代码的错误之处。

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>

异常:

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 

推荐答案

如果在方面的建议中不需要方法参数值,则不应该使用args(),只需在切入点中指定方法签名,如下所示:

@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) {}

只有当您确实需要访问参数时,如您的代码所示,您应该使用args(),但然后您还需要将参数添加到切入点,而不仅仅是通知。如果您直接在通知中定义切入点,则只需定义一次。单独定义切入点仅在您希望在同一方面的多个建议中重用相同切入点时,IMO才有帮助。无论如何,您想要做的是修复您的代码(我没有运行它,只是编写了这个"免提"):

@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) {}
顺便说一句,错误消息"Formal Unbinded in PointCut"表示您没有在Viaargs()this()target()@target()中正确绑定通知方法签名中的形参。或者反过来,切入点语法是正确的,但方法签名是错误的。

这篇关于Spring 4 AOP:在切入点中获取异常java.lang.IlLegalArgumentException:Error at::0 Formal Unbinded in PointCut的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆