简体   繁体   中英

AspectJ with aop.xml

I'm trying to define an inheriting concrete-aspect via an LTW aop.xml file, but nothing seems to be happening.

here is my code:

My main method:

package example;    

public class Farm {

static Cat cat = new Cat();
static Cow cow = new Cow();
static Dog dog = new Dog();
static Fox fox = new Fox();

public static void main(String args[]){
    System.out.println(cat.talk(1));
    System.out.println(dog.talk(1));
    System.out.println(cow.talk(1));
    System.out.println(fox.talk(1));
}
}

The Fox calss:

package example;

public class Fox extends Animal{

public String talk(int i) {
    return "DING DING!!!";
}
}

My abstract aspect:

package example;

public abstract aspect MyAspect {
protected abstract pointcut scope();

before() : scope() {
    System.out.println("Before");
}
}

My aop.xml file:

<aspectj>
    <aspects>
    <aspect name="example.MyAspect"/>
        <concrete-aspect
            name="example.MyConcreteAspect"
            extends="example.MyAspect"
        >
            <pointcut
                name="scope"
                expression="execution(public String example.Fox.talk(int))"
            />
        </concrete-aspect>
    </aspects>
</aspectj>

And finally, my output:

MEW
WOOF
MOO
DING DING!!!

With no "Before" printed before the "DING DING!!!"

Any Idea what am I doing wrong? I've already downloaded a working example, and everything seems to be configured ok on my computer...

Had to change my VM arguments at the run configurations to point the aspectj-weaver.jar. Thanks anyway. Hope this would help anybody.

For run AOP without Spring you need to configure the VM Parameter -javaagent from your IDE.

With maven you can configure a simple junit o run like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <argLine>
                    -javaagent:${settings.localRepository}\org\aspectj\aspectjweaver\1.8.10\aspectjweaver-1.8.10.jar
                </argLine>
            </configuration>
        </plugin>
    </plugins>
</build>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM