繁体   English   中英

AspectJ与aop.xml

[英]AspectJ with aop.xml

我试图通过LTW aop.xml文件定义一个继承的具体方面,但似乎没有发生任何事情。

这是我的代码:

我的主要方法:

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));
}
}

Fox calss:

package example;

public class Fox extends Animal{

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

我的抽象方面:

package example;

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

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

我的aop.xml文件:

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

最后,我的输出:

MEW
WOOF
MOO
DING DING!!!

在“丁丁!!!”之前没有打印过“之前”

任何想法我做错了什么? 我已经下载了一个工作示例,一切似乎都在我的计算机上正常配置...

不得不在运行配置中更改我的VM参数以指向aspectj-weaver.jar。 不管怎么说,还是要谢谢你。 希望这会对任何人有所帮助。

对于没有Spring的AOP运行,您需要从IDE配置VM参数-javaagent

使用maven,你可以像这样配置一个简单的junit o:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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