【问题标题】:Spring AOP proxies does not work with JavaFXSpring AOP 代理不适用于 JavaFX
【发布时间】:2015-04-23 03:50:40
【问题描述】:

美好的一天,我正在开发一个在 JavaFX 上使用 Spring AOP 的项目,不幸的是,当我尝试包装在 JavaFX 场景中使用的接口时,我收到一个空指针。这是堆栈跟踪。

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda$48/756185697.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at javafx.scene.Node.getScene(Node.java:907)
    at javafx.scene.Scene$9.invalidated(Scene.java:1074)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:145)
    at javafx.scene.Scene.setRoot(Scene.java:1038)
    at javafx.scene.Scene.<init>(Scene.java:325)
    at javafx.scene.Scene.<init>(Scene.java:181)
    at com.hccs.sample.aspectj.Main.start(Main.java:42)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/50630420.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$44/1051754451.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1570685826.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/1775282465.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
    ... 1 more
Exception running application com.hccs.sample.aspectj.Main

这里是 Main 方法,我在这里创建 MyScene 接口的实例并用代理包装它。

public class Main extends Application {

    private static ApplicationContext context;

    public static void main(String[] args) {
        context = new AnnotationConfigApplicationContext(AppConfig.class);
        Application.launch(Main.class);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        MyScene scene = context.getBean(MyScene.class);
        if (scene == null) {
            System.out.println("Scene is null on creation");
            System.exit(0);
        } else {
            System.out.println("Scene is not null on creation.");
        }

        // these code are the source of the problem, when these code are
        // commented, it works fine.
        scene = ProxyWrapper.wrap(scene);
        if (scene == null) {
            System.out.println("Scene is null on wrapping");
            System.exit(0);
        } else {
            System.out.println("Scene is not null on wrapping");
        }
        // comment it up to here

        // No problem with manual invocation of methods
        scene.eventOne();
        scene.eventTwo();
        scene.eventThree();

        // Where null pointer occurs
        primaryStage.setScene(new Scene((Parent) scene));
        primaryStage.show();
    }
}

这是应用程序配置。

@Configuration
@ComponentScan(basePackages = { "com.hccs.sample.aspectj" })
public class AppConfig {

}

这里是需要封装的场景接口和实现。

public interface MyScene {
    public void initialize();

    public void eventOne();

    public void eventTwo();

    public void eventThree();

}

@Lazy
@Component
public class MySceneImpl extends BorderPane implements MyScene {

    @FXML
    private Button cmdOne;
    @FXML
    private Button cmdTwo;
    @FXML
    private Button cmdThree;

    public MySceneImpl() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource(
                    "/com/hccs/sample/aspectj/resources/MyScene.fxml"));
            loader.setRoot(this);
            loader.setController(this);
            loader.load();
            System.out.println("\nKARDS!!\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @FXML
    @Override
    public void initialize() {
    }

    @FXML
    @Override
    public void eventOne() {
        System.out.println("One");
    }

    @FXML
    @Override
    public void eventTwo() {
        loopToTen();
    }

    @Override
    public void eventThree() {
        new Thread() {
            @Override
            public void run() {
                loopToTen();
            }
        }.start();
    }

    private void loopToTen() {
        for (int c = 0; c < 10; c++) {
            try {
                Thread.sleep(100);
                System.out.println(c + 1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}

这是场景中使用的FXML,

<fx:root type="BorderPane" xmlns:fx="http://javafx.com/fxml/1"
    xmlns="http://javafx.com/javafx/8">
    <center>
        <HBox alignment="CENTER" spacing="10.0" BorderPane.alignment="CENTER">
            <children>
                <Button fx:id="cmdOne" mnemonicParsing="false" onAction="#eventOne"
                    text="One" />
                <Button fx:id="cmdTwo" mnemonicParsing="false" onAction="#eventTwo"
                    text="Two" />
                <Button fx:id="cmdThree" mnemonicParsing="false" onAction="#eventThree"
                    text="Three" />
            </children>
        </HBox>
    </center>
    <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
    </padding>
</fx:root>

这是包装场景的类。

public class ProxyWrapper {
    @SuppressWarnings("unchecked")
    public static <T> T wrap(T scene) {
        MyPointcut pointcut = new MyPointcut();
        MyMethodInterceptor aspect = new MyMethodInterceptor();
        Advisor advisor = new DefaultPointcutAdvisor(pointcut, aspect);
        ProxyFactory pf = new ProxyFactory();
        pf.setTarget(scene);
        pf.addAdvisor(advisor);
        return (T) pf.getProxy();
    }
}

这是 Pointcut 和 MethodInterceptor 类。

public class MyPointcut extends DynamicMethodMatcherPointcut {

    @Override
    public ClassFilter getClassFilter() {
        return new ClassFilter() {
            @Override
            public boolean matches(Class<?> clazz) {
                return clazz.getName().contains("MyScene");
            }
        };
    }

    @Override
    public boolean matches(Method method, Class<?> clazz, Object[] key) {
        System.out.println(method.getName() + " == event "
                + method.getName().contains("event"));
        return method.getName().contains("eventT");
    }

}

@Aspect
public class MyMethodInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("START: " + invocation.getMethod().getName());
        Object val = invocation.proceed();
        System.out.println("END: " + invocation.getMethod().getName());
        return val;
    }
}

最后这是项目的 pom.xml。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.hccs.sample</groupId>
    <artifactId>sample-aspectj</artifactId>
    <version>1.0.0</version>
    <name>sample-aspectj</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.5</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.5</version>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</project>

如果我有办法让 javafx 识别 spring aop 代理,请分享并帮助我,谢谢!

【问题讨论】:

  • 我遇到了类似的问题,但我根本无法让它工作 - FXML 组件始终为空(我也使用spring.aop.proxy-target-class=true)。有什么帮助吗? stackoverflow.com/questions/45933616/…

标签: spring javafx aop spring-aop fxml


【解决方案1】:

我不能完全弄清楚为什么会失败,但是将MySceneImpl 设为BorderPane 的子类会导致问题。基本上,为了拦截方法而创建的代理不会正确初始化 BorderPane 实例。

解决方法是使用聚合而不是继承。给MyScene添加方法:

public Parent getView() ;

并相应地更新实现类:

@Lazy
@Component
public class MySceneImpl implements MyScene {

    @FXML
    private Button cmdOne;
    @FXML
    private Button cmdTwo;
    @FXML
    private Button cmdThree;

    private final BorderPane view ;

    public MySceneImpl() {

        view = new BorderPane();

        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource(
                    "/application/MyScene.fxml"));
            loader.setRoot(view);
            loader.setController(this);
            loader.load();
            System.out.println("\nKARDS!!\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @FXML
    @Override
    public void initialize() {
    }

    @Override
    public Parent getView() {
        return view ;
    }

    @FXML
    @Override
    public void eventOne() {
        System.out.println("One");
    }

    @FXML
    @Override
    public void eventTwo() {
        loopToTen();
    }

    @Override
    public void eventThree() {
        new Thread() {
            @Override
            public void run() {
                loopToTen();
            }
        }.start();
    }

    private void loopToTen() {
        for (int c = 0; c < 10; c++) {
            try {
                Thread.sleep(100);
                System.out.println(c + 1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}

这里,不是将MySceneImpl 设为BorderPane 的子类,而是持有对BorderPane 实例的引用。该引用被传递给FXMLLoadersetRoot() 方法,因此它会填充FXML 中定义的按钮。最后从getView()方法返回。

现在只需更新Main 类以调用getView()

// primaryStage.setScene(new Scene((Parent) scene));
primaryStage.setScene(new Scene(scene.getView()));

【讨论】:

  • 谢谢,这解决了我遇到的异常。但是方法拦截器只有在手动调用 MyScene 接口的方法时才会执行。当调用javafx阶段的按钮时,方法拦截器不会触发。
  • 啊,是的。那里的问题是您正在调用loader.setController(this),显式传递this 引用。由于此处调用的构造函数是在目标对象上调用的(即不在代理上),所以FXMLLoader 引用的控制器是目标,而不是代理。所以处理程序方法没有装饰。您可能需要对它进行相当多的重组。特别是,FXMLLoader 必须从应用程序上下文生成控制器:您可以使用控制器工厂来执行此操作,但该工厂需要对应用程序上下文的引用。
  • 谢谢,我会试试的。如果我得到它的工作,回来找你,再次感谢!
  • 另一个问题,如果您愿意,在 MySceneImpl 类中,当控制器包装在代理中并调用 loader.load() 时,按钮不会被实例化,尽管这些方法现在被拦截了。你对解决这个问题有什么想法吗?谢谢!
猜你喜欢
  • 2021-12-27
  • 1970-01-01
  • 2019-10-30
  • 2010-10-02
  • 1970-01-01
相关资源
最近更新 更多