Spring AOP 代理不适用于 JavaFX

Spring AOP proxies does not work with JavaFX

美好的一天,我正在开发一个在 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$launchApplication7(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda/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.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$launchApplication13(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda/50630420.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1051754451.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1570685826.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater5(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda/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$null1(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda/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>

这是环绕场景的Class。

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();
    }
}

这是切入点和 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 代理,请分享并帮助我,谢谢!

我不太明白为什么会失败,但是让 MySceneImpl 成为 BorderPane 的子 class 是导致问题的原因。基本上,为拦截方法而创建的代理不会正确初始化 BorderPane 实例。

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

public Parent getView() ;

并相应地更新实现 class:

@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 的子 class,而是持有对 BorderPane 实例的引用。该引用将传递给 FXMLLoadersetRoot() 方法,因此它会填充 FXML 中定义的按钮。最后,它从 getView() 方法返回。

现在只需更新 Main class 即可调用 getView():

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