2011-09-27 164 views
1

我正在首次试验Spring AOP并陷入XML配置中。我试图获取基于AOP的“日志记录”的模拟版本,并使用MethodInterceptor来包装特定的方法调用,并在这些方法调用之前和之后执行一些简单的System.out.println语句。简单的东西,对吧?Spring AOP配置(XML)

所以我的项目有很多课,其中两个是FizzBuzz。 Fizz有一个名为foo()的方法,Buzz有一个名为wapap()的方法。每个这些方法在运行时调用的时候,我希望我的LoggingInterceptor身边人执行它的invoke()方法:

public class LoggingInterceptor implements MethodInterceptor 
{ 
    public Object invoke(MethodInvocation methodInvocation) 
    { 
     try 
     { 
      System.out.println("About to call a special method."); 
      Object result = methodInvocation.proceed(); 
      return result; 
     } 
     finally 
     { 
      System.out.println("Finished executing the special method."); 
     } 
    } 
} 

所以我理解的建议(我拦截implement执行)的概念,切入点(这将有方法围绕他们执行的建议)和切入点顾问(建议和切入点之间的绑定)。

我只是努力在一个简单的XML配置完全绑定它。

这是我到目前为止,但我知道它缺少切入点和切入点顾问定义,可能更多。

<beans default-autowire="no" > 
    <bean name="loggingInterceptor" class="org.me.myproject.aop.LoggingInterceptor"/> 
</beans> 

缺少什么我在这里,使这个特定的嘶嘶声:: foo的()和巴兹:: wapap()调用?

任何在正确的方向微调都非常感谢!

回答

5

补充一点:

<aop:config> 
    <aop:advisor advice-ref="loggingInterceptor" pointcut="execution(public * Fizz.foo(..))"/> 
    <aop:advisor advice-ref="loggingInterceptor" pointcut="execution(public * Buzz.wapap(..))"/> 
</aop:config> 

您还需要添加适合于您的框架版本AOP命名空间声明:

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

还要考虑使用@AspectJ方面看这个问题:Spring: Standard Logging aspect (interceptor)

+0

感谢Tomasz,但它看起来像那些将调用任何Fizz或Buzz方法的日志拦截器建议。我需要它来指定foo()和wapap()。我也需要添加一个xmlns的东西来处理新的aop标签? – IAmYourFaja

+0

我编辑了我的答案,看看。 –

+0

所有这些记录在哪里?我一直在寻找一些Spring AOP框架的XML模式指南,并找不到任何!另外,如果我有一个ExceptionInterceptor(实现ThrowsAdvice)来处理异常,那该怎么办?我如何修改这些切入点字符串以指定任何异常? – IAmYourFaja