2012-08-17 73 views
2

我用spring框架3.1.2開始學習AOP。但我有麻煩。在Beans.xml上下文文件我寫道:AOP在彈簧方面的警告

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

+0

你的classpath中是否有所需的Jars? – 2012-08-17 18:08:10

+0

當然,我在classpath中都有彈簧罐。 – user902691 2012-08-17 18:09:03

+1

這個類不是Spring的一部分。你需要aopalliance.jar,http://sourceforge.net/projects/aopalliance/files/ – 2012-08-17 18:10:33

回答

1

我想你正在學習春天使用春季實例中的例子。事實上,你錯過了一個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因爲你不包括罐子到類路徑中。