开发者

Spring AOP Configuration (XML)

I am experimenting with Spring AOP for the first time and get stuck in the XML configuration. I'm trying to get a mock version of AOP-based "logging" up and running, using a MethodInterceptor to wrap specific method calls and do some simple System.out.println statements before and after those method invocations. Simple stuff, right?

So my project has many classes, two of them are Fizz and Buzz. Fizz has a method named foo() and Buzz has a method named wapap(). Every time these methods are invoked at runtime, I want my LoggingInterceptor to execute its invoke() method around them:

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.p开发者_运维百科rintln("Finished executing the special method.");
        }
    }
}

So I understand the concepts of advice (my interceptor impl), pointcuts (the methods that will have advice executed around them), and pointcut advisors (bindings between advice and pointcuts).

I'm just struggling tying it altogether in a simple XML config.

Here's what I have so far, but I know it's missing pointcut and pointcut advisor definitions, and possibly more.

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

What am I missing here to make this specific to Fizz::foo() and Buzz::wapap() calls?

Any nudges in the right direction are enormously appreciated!


Add this:

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

You also need to add AOP namespace declaration in version appropriate to your framework:

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

Also consider using @AspectJ aspects and see this question: Spring: Standard Logging aspect (interceptor).


If you are using Spring 2.5+ you can use annotation to and create your advice and Pointcuts.

Create class with @Aspect annotation.

Create @PointCut for specific class and specific method and then create @Around advice.

You can read short tutorial how to do it here:

http://veerasundar.com/blog/2010/01/spring-aop-example-profiling-method-execution-time-tutorial/

It' very easy to implement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜