josh
josh

Reputation: 419

Spring AOP Not working properly

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?

Upvotes: 0

Views: 281

Answers (1)

kriegaex
kriegaex

Reputation: 67297

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

Upvotes: 1

Related Questions