Spring AOP Not working properly

[亡魂溺海] 提交于 2019-12-11 15:03:24

问题


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

Main Class:

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 Class:

@Aspect
public class AspectTest{

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

    }

}

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?


回答1:


@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.



来源:https://stackoverflow.com/questions/50037395/spring-aop-not-working-properly

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