用另一个方面包裹一个弹簧方面 [英] Wrap a spring aspects with another aspect

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

问题描述

我已经将两个方面声明为 foo &bar 在函数 runFunc 上,我想捕获运行函数 runcFunc 所花费的时间 &BarFoo 中,但它只为 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?

推荐答案

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

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆