Spring AOP Configuration (XML)

17,700

Solution 1

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).

Solution 2

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.

Share:
17,700
IAmYourFaja
Author by

IAmYourFaja

my father is a principal at burgoyne intnl and got me this job programming lisp and development. I aspire to unittesting with a concentration in mobile platforms.

Updated on June 04, 2022

Comments

  • IAmYourFaja
    IAmYourFaja almost 2 years

    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.println("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!