Search code examples
javaspringspring-mvcaopspring-aop

Spring AOP before and after method not working with XML configuration


I have an aspect, which at this moment doesn't do anything special:

@Named
public final class PreventNullReturn {
    public void replaceNullWithSpecialCase() {
        System.out.print("ASPECT CALLED!");
        throw new AssertionError();
    }
}

It simply should throw an error to prove that it was called. I have spring-aspects.xml file where I declare aspects:

<?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"
>
    <aop:aspectj-autoproxy/>

    <bean class="aspects.PreventNullReturn"
          id="preventNullReturn"
    ></bean>

    <aop:config>
        <aop:aspect
                ref="preventNullReturn"
        >
            <aop:pointcut
                    id="preventNull"
                    expression="execution(* *.getById(..))"
            ></aop:pointcut>
            <aop:before
                    pointcut-ref="preventNull"
                    method="replaceNullWithSpecialCase"
            ></aop:before>
            <aop:after
                    pointcut-ref="preventNull"
                    method="replaceNullWithSpecialCase"
            ></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

And in configuration file for Spring unit tests I use it in this way:

<import resource="classpath*:spring-aspects.xml" />

But aspect is not called. What is funny, even IDE shows Navigate to advised methods tooltip and it navigates properly. I try to follow "Spring in Action, Fourth Edition book in chapter 1.1.3. Applying aspects (and that's why I use XML instead of classes), but I am doing something wrong and I can't figure it out. Please write if I can provide more useful details. I would be grateful if you decide to help me, thank you in advance.


Solution

  • I found an answer on my own, instead importing it as a resource in configuration file for Spring unit tests:

    <import resource="classpath*:spring-aspects.xml" />
    

    I have to have its content directly in configuration file for Spring unit tests:

     <!--TODO: Move it to separated file-->
    <aop:aspectj-autoproxy/>
    
    <bean class="aspects.PreventNullReturn"
          id="preventNullReturn"
    ></bean>
    
    <aop:config>
        <aop:aspect
                ref="preventNullReturn"
        >
            <aop:pointcut
                    id="preventNull"
                    expression="execution(* *.getById(..))"
            ></aop:pointcut>
            <aop:before
                    pointcut-ref="preventNull"
                    method="replaceNullWithSpecialCase"
            ></aop:before>
            <aop:after
                    pointcut-ref="preventNull"
                    method="replaceNullWithSpecialCase"
            ></aop:after>
        </aop:aspect>
    </aop:config>
    

    Which of course is worse solution than having it in separated file, but it's only way I can make it work.