javaFX项目中成功启动了AOP

痞子三分冷 提交于 2020-02-27 02:34:47

在我的帖子中说了javafx项目不能在按钮的事件上启用AOP,今天终于解决了这个问题:

1)项目目录截图:

2)pom.xml主要内容:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>de.roskenet</groupId>
            <artifactId>springboot-javafx-support</artifactId>
            <version>2.1.6</version>
        </dependency>

3)其它各aspect切面类,controller和View及fxml内容分别如下:

@FXMLController
public class HelloController {
    Logger logger = LoggerFactory.getLogger(HelloController.class);

    public void btnclickOnAction()
    {
        logger.info("HelloworldView ===btnclickOnAction");
        System.out.println("btnclickOnAction.....");
        JavafxSpringbootPomApplication.showView(SecondView.class, Modality.NONE);
    }
}
@FXMLController
public class SecondController {
    Logger logger = LoggerFactory.getLogger(SecondController.class);

    public void btnclickOnAction2()
    {
        logger.info("HelloworldView ===btnclickOnAction");
        System.out.println("btnclickOnAction.....");
    }
}
@FXMLView
public class HelloworldView extends AbstractFxmlView {}
@FXMLView(title = "second view")
public class SecondView extends AbstractFxmlView {}

helloword.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.joe.javafx_springboot_pom.controller.HelloController">
    <children>
        <Label layoutX="99.0" layoutY="109.0" prefHeight="34.0" prefWidth="394.0" text="Hello World!">
            <font>
                <Font size="26.0" />
            </font>
        </Label>
      <Button fx:id="btnid" layoutX="168.0" layoutY="164.0" mnemonicParsing="false" onAction="#btnclickOnAction" text="Button" />
    </children>
</Pane>

second.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.joe.javafx_springboot_pom.controller.SecondController">
    <children>
        <Label layoutX="99.0" layoutY="109.0" prefHeight="34.0" prefWidth="394.0" text="Hello World!">
            <font>
                <Font size="26.0" />
            </font>
        </Label>
      <Button fx:id="btnid2" layoutX="168.0" layoutY="164.0" mnemonicParsing="false" onAction="#btnclickOnAction2" text="Button" />
    </children>
</Pane>
@Aspect
@Configuration
@EnableAspectJAutoProxy
public class AspectClass {

    @Before("execution(* btnclickOnAction(..))")
    public void aspectfun(JoinPoint joinPoint)
    {
        System.out.println(joinPoint.getSignature().getName() + " @Before running.....");
    }
    @After("execution(* btnclickOnAction(..))")
    public void aspectfun2(JoinPoint joinPoint)
    {
        System.out.println(joinPoint.getSignature().getName() + "@After running....");
    }
}
@Aspect
@Configuration
@EnableAspectJAutoProxy
public class AspectClass2 {

    @Before("execution(* btnclickOnAction2(..))")
    public void aspectfun(JoinPoint joinPoint)
    {
        System.out.println(joinPoint.getSignature().getName() + " @Before running.....");
    }
    @After("execution(* btnclickOnAction2(..))")
    public void aspectfun2(JoinPoint joinPoint)
    {
        System.out.println(joinPoint.getSignature().getName() + "@After running....");
    }
}
@SpringBootApplication
public class JavafxSpringbootPomApplication  extends AbstractJavaFxApplicationSupport {

    public static void main(String[] args) {
//        SpringApplication.run(JavafxSpringbootPomApplication.class, args);
        launch(JavafxSpringbootPomApplication.class, HelloworldView.class,args);
    }

}

hellowordview中点击按钮显示secondview,并且两个view中的按钮都会启用AOP的@Before和@After,这才是我最希望要的功能。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!