2012-03-19 46 views
2

在某些方法(或截至目前所有方法)之前,我必須調用某個方面的方法來記錄某些消息。我的應用程序運行正常,否則Aspect類的方法都不會被調用。使用ZK配置AOP來攔截方法

我已經在我的本地應用程序中嘗試過相同的文件夾結構中的相同的cutpoint,但是當我嘗試將它與ZK包含在一起時,我遇到了問題。我也修改了我的application-context.xml以支持AOP。

這是我的切面類:

package com.mypckg.services.impl; 

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

@Aspect 
public class MyIntercpeter { 

    @Pointcut("execution(* com.mypckg.services.impl.MyService.getStudents(..))") 
    public void performance() { 
    } 

    @Before("performance()") 
    public void doSomethingBeforeExecution() { 
     System.out.println("Before execution method called..."); 


    } 

    @AfterReturning("performance()") 
    public void doSomethingAfterExecution() { 
     System.out.println("After execution method called..."); 

    } 
} 

的修改,我在做出application-context.xml

<beans ......... 

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


..... 

<aop:aspectj-autoproxy /> 
<context:annotation-config /> 

難道我失去了一些東西?提前致謝。

回答

0

看起來像你錯過了一件顯而易見的事情:你忘了在spring配置中聲明bean?

從春天文檔:

的Spring AOP只支持方法執行連接點Spring bean的,所以你可以把切入點的方法在Spring bean執行匹配。

http://static.springsource.org/spring/docs/2.0.x/reference/aop.html

你可以用註釋或配置您的聲明豆。

還將更好地把你使用的春天版本(我認爲它是2.0.x)。

+0

我已經在spring配置文件中聲明瞭所有的bean。我只給出了spring應用程序上下文文件的修改部分。我們正在使用Spring 2.5 – 2012-03-20 04:41:13

+0

只需要提一下:您可以使用已經有註釋和註釋處理器的perf4j來測量性能。 [http://perf4j.codehaus.org/devguide.html#Unobtrusive_Logging_with_Profiled_and_AOP](http://perf4j.codehaus.org/devguide.html#Unobtrusive_Logging_with_Profiled_and_AOP)。 – 2012-03-20 08:05:48

+0

您是否在zk.xml配置中配置了spring bean解析器? – 2012-03-20 19:10:24