springboot+javafx使用aop切面导致的fx:id不能被注入问题

记录一个我遇到得问题

问题描述

我本来使用AOP切面来进行全局异常管理,但是使用AOP之后fxml中通过fx:id绑定得参数无法被注入

java 复制代码
@Slf4j
@Component
@Aspect
public class GlobalExceptionAspect {

    @AfterThrowing(pointcut = "execution(* com.shkj.videoclassification..*(..))", throwing = "ex")
    public void handleServiceException(Exception ex) {
        // 处理异常逻辑
//        System.err.println("Caught an exception in service layer: " + ex.getMessage());
//        ex.printStackTrace();
        log.error(ex.getMessage());
        AlertUtils.showAlert("系统错误",ex.getMessage());

    }
}

解决办法

不使用全局异常处理集合,使用try catch捕获异常进行处理

java 复制代码
@Component
@Slf4j
public class  GlobalExceptionHandler  {

    public static void handleException(Exception ex) {
        // 使用 Platform.runLater 确保在 JavaFX 线程中安全地显示弹窗
        Platform.runLater(() -> {
            log.error(ex.getMessage());
            AlertUtils.showAlert("系统错误", ex.getMessage());
        });

    }
}

手动触发异常

java 复制代码
   @FXML
    public void initialize() {
        try {
            System.out.println("初始化");
            System.out.println(labelMsg);
            throw new CheckedException("初始化失败");
        } catch (Exception e) {
            GlobalExceptionHandler.handleException(e);
        }

    }
相关推荐
小王不爱笑1325 分钟前
Java 异常全解析:从原理到实战,搞定异常处理
java·开发语言
lay_liu5 分钟前
springcloud springboot nacos版本对应
spring boot·spring·spring cloud
tumeng07116 分钟前
springboot项目架构
spring boot·后端·架构
LES000LIE9 分钟前
Spring Cloud
后端·spring·spring cloud
人工智能AI技术9 分钟前
Spring Boot 3.5正式普及!Java虚拟线程+GraalVM原生镜像,启动仅0.3秒
java
没有bug.的程序员19 分钟前
撕裂微服务网关的认证风暴:Spring Security 6.1 与 JWT 物理级免登架构大重构
java·spring·微服务·架构·security·jwt
小王不爱笑13222 分钟前
Java Set 集合全家桶:HashSet、LinkedHashSet、TreeSet 详解与实战
java·开发语言
杨过姑父24 分钟前
java 面试,jvm笔记
java·jvm·面试
mldlds30 分钟前
Spring Boot应用关闭分析
java·spring boot·后端
zjjsctcdl31 分钟前
Spring Boot与MyBatis
spring boot·后端·mybatis