带有局部变量注释的 AOP

发布于 2024-09-11 01:46:39 字数 430 浏览 0 评论 0原文

我想使用局部变量注解来做更好的AOP。一个想法是实现Future使用注释的代理的概念。

@NonBlocking ExpensiveObject exp = new ExpensiveObject(); 
//returns immediately, but has threaded out instantiation of the ExpensiveObject.

exp.doStuff(); 
//okay, now it blocks until it's finished instantiating and then executes #doStuff

我能否以某种方式让 AspectJ 对此感到厌烦并通过局部变量注释完成我想要的工作?我知道其他线程已经表明 Java 并不真正支持它们,但这将是神奇的。我真的不想传递 Future 并破坏封装。

I want to use local variable annotations to do better AOP. One idea is to implement the Future<T> concept with a proxy using an annotation.

@NonBlocking ExpensiveObject exp = new ExpensiveObject(); 
//returns immediately, but has threaded out instantiation of the ExpensiveObject.

exp.doStuff(); 
//okay, now it blocks until it's finished instantiating and then executes #doStuff

Can I sick AspectJ on this somehow and get what I want done with local variable annotations? I know other threads have indicated that Java doesn't really support them but it would be magical. I really don't want to pass around a Future and break encapsulation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

a√萤火虫的光℡ 2024-09-18 01:46:39

您无法使用代理来执行此操作,但如果您注释类型而不是局部变量,则真正的aspectj字节码编织将帮助您实现这一点。 (我不认为局部变量访问被支持作为切入点)。无论如何,这是一些代码。

注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Later {}

标有此注释的类:

package com.dummy.aspectj;
@Later
public class HeavyObject{

    public HeavyObject(){
        System.out.println("Boy, I am heavy");
    }
}

主类:

package com.dummy.aspectj;
public class HeavyLifter{

    public static void main(final String[] args){
        final HeavyObject fatman = new HeavyObject();
        System.out.println("Finished with main");
    }

}

和方面:

package com.dummy.aspectj;
public aspect LaterAspect{

    pointcut laterInstantiation() :
        execution(@Later *.new(..))    ;

    void around() : laterInstantiation() {
        new Thread(new Runnable(){
            @Override
            public void run(){
                System.out.println("Wait... this is too heavy");

                try{
                    Thread.sleep(2000);
                } catch(final InterruptedException e){
                    throw new IllegalStateException(e);
                }
                System.out.println("OK, now I am up to the task");
                proceed();
            }
        }).start();
    }

}

以下是当您从 eclipse 中将 HeavyLifter 作为 AspectJ/Java 应用程序运行时的输出:

Finished with main
Wait... this is too heavy
OK, now I am up to the task
Boy, I am heavy

You can not do this with a proxy, but real aspectj bytecode weaving will get you there if you annotate the type instead of the local variable. (I don't think local variable access is supported as a pointcut). Anyway, here's some code.

An annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Later {}

A class marked with this annotation:

package com.dummy.aspectj;
@Later
public class HeavyObject{

    public HeavyObject(){
        System.out.println("Boy, I am heavy");
    }
}

A main class:

package com.dummy.aspectj;
public class HeavyLifter{

    public static void main(final String[] args){
        final HeavyObject fatman = new HeavyObject();
        System.out.println("Finished with main");
    }

}

and an aspect:

package com.dummy.aspectj;
public aspect LaterAspect{

    pointcut laterInstantiation() :
        execution(@Later *.new(..))    ;

    void around() : laterInstantiation() {
        new Thread(new Runnable(){
            @Override
            public void run(){
                System.out.println("Wait... this is too heavy");

                try{
                    Thread.sleep(2000);
                } catch(final InterruptedException e){
                    throw new IllegalStateException(e);
                }
                System.out.println("OK, now I am up to the task");
                proceed();
            }
        }).start();
    }

}

Here is the output from HeavyLifter when you run it as an AspectJ/Java Application from eclipse:

Finished with main
Wait... this is too heavy
OK, now I am up to the task
Boy, I am heavy
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文