Spring AOP 无法正常工作 [英] Spring AOP Not working properly

查看:59
本文介绍了Spring AOP 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Spring/Swing 应用程序中使用 AOP 方法处理异常,但无法使其正常工作.

I'm trying to handle exceptions with AOP approach in my Spring/Swing Application and I couldn't make it work.

主类:

public class MainFrame extends JFrame {

    private JPanel mainPanel;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MainFrame() {
        initializeMainPanel();
    }

    private void initializeMainPanel() {

        exitLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                throw new Exception("test");
            }
        });
    }

}

方面类:

@Aspect
public class AspectTest{

    @AfterThrowing(pointcut = "execution(* com.test.MainFrame.*(..))", throwing = "ex")
    public void logError(Exception ex) throws Throwable {
        // ex.printStackTrace();

    }

}

因此,我在我的鼠标监听器中抛出了一个异常,并希望在我的 AspectTest 类的 AfterThrowing 方法中捕获它,但它不起作用.

So, I throw an exception within my Mouse Listener and expect to catch it in my AspectTest class' AfterThrowing method but it does not work.

有人可以帮助我了解我在这里缺少什么吗?

Can someone please help me to understand what I'm missing here?

推荐答案

@AfterThrowing 无法捕捉异常,只能注意到它们并记录它们或做类似的事情.如果您想处理某个方面的异常,您需要使用 @Around 建议.

@AfterThrowing cannot catch exceptions, only notice them and log them or do something similar. If you want to handle exceptions in an aspect you need to use an @Around advice.

这篇关于Spring AOP 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆