Spring AOP: получение исключения - PullRequest
0 голосов
/ 05 января 2019

Я пытаюсь запустить пример демонстрационной программы Spring AOP из книги Spring in Action, чтобы изучить концепции 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
^

1 Ответ

0 голосов
/ 05 января 2019

Добавление () после выполнения внутри аннотаций @Before, @AfterReturning и @AfterThrowing устранило проблему.

@Before("performance()")

@Before("performance()")

@AfterReturning("performance()")

@AfterThrowing("performance()")
...