【问题标题】:Wrap a spring aspects with another aspect用另一个方面包裹一个弹簧方面
【发布时间】:2017-05-19 12:45:52
【问题描述】:

我在函数runFunc 上声明了foobar 的两个方面,我想捕获在Foo 中运行函数runcFuncBar 所花费的时间,但它正在捕获仅适用于runFunc 的时间。 Bar 独立运行。

我希望如果我在一个函数上放置两个注释,第一个注释应该包装第二个注释,第二个应该包装函数runfunc。我怎样才能做到这一点?

【问题讨论】:

  • 嗨,Pankaj。您让我回答这个问题,但我必须假设您这样做的原因是在非平凡代码中发现性能问题。如果那不是你的目标,那我帮不上什么忙。如果这是你的目标,我认为更有效的方法不是测量(如这段代码),而是stackshots

标签: spring annotations aop wrapper aspectj


【解决方案1】:

事实证明切面可以像包装函数一样容易地包装其他切面。

以下代码有效。

@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多