本文介绍了用另一个方面来包装春季方面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将两个方面声明为foo& bar在功能runFunc上,我想捕获运行功能runcFunc&所花费的时间. Foo中的Bar,但它仅捕获runFunc的时间. Bar独立运行.

I've declared two aspects as foo & bar over a function runFunc and I want to capture time taken to run the function runcFunc & Bar in Foo, but it is capturing the time only for runFunc. Bar is running independently.

我希望如果在函数上放置两个注释,则第一个注释应包装第二个注释,第二个注释应包装函数runfunc.我该如何实现?

I want that If I put two annotation over a function, the 1st annotation should wrap the 2nd annotation and the 2nd one should wrap the function runfunc. How can I achieve that?

推荐答案

事实证明,方面可以像包装函数一样容易地包装其他方面.

It turns out aspect can wrap other aspects just as easily as they can wrap a function.

以下代码有效.

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Foo {}


@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Bar {}


@Aspect
@Component
public class A {

    @Around("@annotation( foo )")
    public void func1(final ProceedingJoinPoint pjp, Foo foo) throws Throwable {
        System.out.println("foo start");
        pjp.proceed();
        System.out.println("foo end");
    }
}


@Aspect
@Component
public class B {

    @Around("@annotation( bar )")
    public void func2(final ProceedingJoinPoint pjp, Bar bar) throws Throwable {
        System.out.println("bar start");
        pjp.proceed();
        System.out.println("bar end");
    }
}

以下代码:

@Foo
@Bar
public void runFunc(){
    System.out.println("Inside Run.runFunc");
}

输出以下内容:

foo start
bar start
Inside Run.runFunc
bar end
foo end

这篇关于用另一个方面来包装春季方面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 21:05