使用XML配置的Spring AOP不起作用

时间:2017-09-18 23:53:41

标签: xml spring spring-aop

我正在尝试创建一个配置了XML的Spring AOP程序。

但我收到了一个错误:

在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.BeanCreationException:创建名称为' performanceImpl'的bean时出错在类路径资源[concert / concertConfig.xml]中定义:bean实例化之前的BeanPostProcessor失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:使用名称' org.springframework.aop.aspectj.AspectJPointcutAdvisor创建bean时出错#0':无法创建内部bean'(内部bean)# 7cbd213e'设置构造函数参数时类型为[org.springframework.aop.aspectj.AspectJMethodBeforeAdvice];嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为'(内部bean)的bean时出错#7cbd213e':无法解析对bean' performance'的引用设置构造函数参数时;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为' performance':bean实例化失败的bean时出错;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.aop.aspectj.AspectJExpressionPointcut]:找不到默认构造函数;嵌套异常是java.lang.NoClassDefFoundError:org / aspectj / weaver / reflect / ReflectionWorld $ ReflectionWorldException

看起来它无法创建Bean' performanceImpl',我已经多次检查过我的代码,但仍然不知道它。希望有人能找到问题。

这是我的代码:

    package concert;
    public interface Performance {
        public void perform();
    }

    package concert;
    public class PerformanceImpl implements Performance {
        @Override
        public void perform() {
            System.out.println("Performing now!");
        }
    }

方面

package concert;
public class Audience{
public void silencePhone() {
    System.out.println("Silencing phone please!");
}

public void takeSeats() {
    System.out.println("Taking seats please!");
}

public void clap() {
    System.out.println("CLAP CLAP CLAP!");
}

public void demandRefund() {
    System.out.println("You can demand the refund.");
}
}

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">

<bean id="performanceImpl" class="concert.PerformanceImpl"></bean>
<bean id="audience" class="concert.Audience"></bean>

<aop:config>
    <aop:aspect ref="audience">
        <aop:pointcut id="performance"
            expression="execution(** concert.Performance.perform(..))"/>

        <aop:before pointcut-ref="performance"
            method="silencePhone"/>

        <aop:before pointcut-ref="performance"
            method="takeSeats"/>

        <aop:after pointcut-ref="performance"
            method="clap"/>

        <aop:after-throwing pointcut-ref="performance"
            method="demandRefund"/>
    </aop:aspect>
</aop:config>

</beans>

JUnitTest

package concert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConcertTest {
@Test
public void test() {
    ApplicationContext cpx = new 
    ClassPathXmlApplicationContext("concert/concertConfig.xml");
    Performance performance = cpx.getBean("performanceImpl",
    PerformanceImpl.class);
    performance.perform();
}
}

0 个答案:

没有答案