AspectJ newbie: can't get aspect called

So, I began to study the use of AspectJ to handle processing events when the state of an object in my domain changes.

In fact, I would like to write a tip that wraps all the setter methods in my domain. When the advice is called, it checks the initial value of the field being set, starts the setter, and then checks the value after the setter completes. If the value changes, it will trigger an event for the event listener, reporting this.

I used the tutorial found here: http://www.andrewewhite.net/wordpress/2010/03/17/aspectj-annotation-tutorial/ , but I can not get any advice that needs to be done. Please note that I only use the LTW method to weave my board, I do not write advice using AspectJ and precompile it.

My aop.xml (in META-INF for my test suite) looks like this:

<aspectj>
    <aspects>
        <aspect name = "domain.aop.TestAspect" />
    </aspects>
</aspectj>

The Aspect class I created looks like this:

package domain.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class TestAspect
{
    @Around ("call (* domain. *. Set * (..))")
    public void aroundSetMethods (JoinPoint jp)
    {
        System.out.println ("aroundSetMethod called");
    }
}

When I run my test case, I can see (by setting a breakpoint) that the method (domain.Error.setTask ()) is being called. I am convinced that this should serve as my advice, but I never delve into the method of consultation.

Any guidance on what I'm doing wrong here?

thank

+3
source share
1 answer

You must also specify which classes you want to bind.

Try replacing the aop.xml file as follows:

<!DOCTYPE aspectj PUBLIC
        "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver>
        <!-- only weave classes in this package -->
        <include within="domain.*" />
    </weaver>
    <aspects>
        <!-- use only this aspect for weaving -->
        <aspect name="domain.aop.TestAspect"/>
    </aspects>
</aspectj>

If you configured the runtime server or Java agent correctly, you can also see the weaving process in your log.

Hope this helps!

+3
source

Source: https://habr.com/ru/post/1775672/


All Articles