开发者

Newbie working with AspectJ: Can't get aspect called

开发者 https://www.devze.com 2023-01-25 14:24 出处:网络
So, I\'ve started looking into using AspectJ to handle processing events when the state of an object in my domain changes.

So, I've started looking into using AspectJ to handle processing events when the state of an object in my domain changes.

Effectively, I'd like to write advice that wraps all of the setter methods in my domain. When the advice is called, it'll check the initial value of the field being set, run the setter, then check the value after the setter executes. If the value changes, it'll fire an event to an event listener, informing of the change.

I used the tutorial found here: http://www.andrewewhite.net/wordpress/2010/03/17/aspectj-annotation-tutorial/, but I'm unable to get any advice calls to be made. Notice that I'm only using the LTW method for weaving my advice, I'm not writing advice usin开发者_运维百科g the AspectJ language and precompiling it.

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

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

The Aspect class that I've 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 a method (domain.Error.setTask ()) gets called. It is my belief that this should trigger my advice, but I never get into the advice method.

Any pointers to what I'm doing wrong here?

Thanks


You must also include which classes you want to weave.

Try to replace your aop.xml file with this:

<!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 have configured your runtime server or Java agent correctly, you can also see the weaving process in your log.

I hope it helps!

0

精彩评论

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