【问题标题】:Spring AOP : Getting exception java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting '(' at character position 0Spring AOP:获取异常java.lang.IllegalArgumentException:切入点格式不正确:期望'('在字符位置0
【发布时间】:2019-01-05 15:59:52
【问题描述】:

我正在尝试运行 Spring in Action 书中的示例 Spring AOP 演示程序来学习 AOP 概念。在程序下面运行时,我收到了 IllegalArgumentException。有人可以帮我理解这里出了什么问题。

Performance.java

package com.aop.annotations.example2;

public interface Performance {

    public void perform() throws Exception;
}

DancePerformance.java

package com.aop.annotations.example2;

public class DancePerformance implements Performance{

    @Override
    public void perform() throws Exception{
        System.out.println("DancePerformance started...");      
    }
}

CircusPerformance.java

package com.aop.annotations.example2;

public class CircusPerformance implements Performance {

    @Override
    public void perform() throws Exception {
        System.out.println("Circus Performance started... ");
        throw new Exception("CircusException occurred");
    }
}

Audience.java

package com.aop.annotations.example2;

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

@Aspect
public class Audience {

    @Pointcut("execution(* com.aop.annotations.example2.Performance.perform(..))")
    public void performance() {
    }

    @Before("performance")
    public void silenceCellPhones() {
        System.out.println("Silence cell phones");
    }

    @Before("performance")
    public void takeSeats() {
        System.out.println("Taking seats");
    }

    @AfterReturning("performance")
    public void applause() {
        System.out.println("CLAP CLAP CLAP!!!");
    }

    @AfterThrowing("performance")
    public void demandRefund() {
        System.out.println("Demanding a refund");
    }
}

TestAOPMain.java

package com.aop.annotations.example2;

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

public class TestAOPMain {

    public static void main(String args[]) {
        try {
            ApplicationContext context = new ClassPathXmlApplicationContext("appContext2.xml");
            Performance dancePerformance = context.getBean("dance", DancePerformance.class);
            dancePerformance.perform();

            Performance circusPerformance = (CircusPerformance) context.getBean("circus");
            circusPerformance.perform();

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

    }

}

appContext2.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.example2" />
    <aop:aspectj-autoproxy proxy-target-class="true" />
    <bean id = "dance" class="com.aop.annotations.example2.DancePerformance" />
    <bean id = "circus" class="com.aop.annotations.example2.CircusPerformance" />
    <bean class="com.aop.annotations.example2.Audience" />
</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: Pointcut is not well-formed: expecting '(' at character position 0
performance
^

Error creating bean with name 'org.springframework.context.event.internalEventListenerProcessor': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting '(' at character position 0
performance
^

【问题讨论】:

  • 更改为 @Before("performance()") 并对所有建议执行相同操作
  • 非常感谢...现在工作正常。

标签: spring jakarta-ee aop spring-aop


【解决方案1】:

在@Before、@AfterReturning 和@AfterThrowing 注释中的性能后添加 () 修复了该问题。

@Before("performance()")

@Before("performance()")

@AfterReturning("performance()")

@AfterThrowing("performance()")

【讨论】:

    猜你喜欢
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多