2

我开始使用 spring 框架 3.1.2 学习 AOP。但我有麻烦。在Beans.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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <bean id = "audience" class="com.MySpring.Audience"></bean>

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

            <aop:before pointcut-ref="performance" method="seatDown"/>
            <aop:before pointcut-ref="performance" method="turnOffPhone"/>
            <aop:after-returning pointcut-ref="performance" method="applauz"/>
            <aop:after-throwing pointcut-ref="performance" method="demandRefund"/>

        </aop:aspect>
    </aop:config>

</beans>

和Java类

public class AOPTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "Beans.xml");

    }

}

// ///
class Audience {

    public void seatDown() {
        out.println("Seat down");
    }

    public void turnOffPhone() {
        out.println("Turn Off Phone");

    }

    public void applauz() {

        out.println("Aplauz");
    }

    public void demandRefund() {
        out.println("We have money back");
    }

}
////
interface Performer{
    void perform();
}

但在错误控制台中我看到:

Description Resource Path Location Type Error occured processing XML 'class path resource [org/aopalliance/aop/Advice.class] cannot be opened because it does not exist'. See Error Log for more details Beans.xml /AnnotationsWiring/src line 11 Spring Beans Problem 第二个错误:

Description Resource Path Location Type Error occured processing XML 'org/springframework/aop/aspectj/AspectJMethodBeforeAdvice'. See Error Log for more details Beans.xml /AOP/src line 12 Spring Beans Problem

4

1 回答 1

1

我猜你正在使用 spring in action 中的示例来学习 spring。实际上,您缺少一个com.springsource.org.apoalliance.jar文件,该文件未包含在其中org.springframework.aop.jar(尽管我不知道为什么)。

请在以下网址下载:http: //sourceforge.net/projects/aopalliance/files/aopalliance/

你有,Caused by: java.lang.ClassNotFoundException: org.aopalliance.aop.Advice因为你没有将 jar 包含到你的类路径中。

于 2012-12-06T08:10:54.163 回答