NetBeans で Java Swing を使用して、業界向けの ERP を開発しています。log4Jを使用して発生した例外をログに記録したい! およびAOP。多くの検索を行いましたが、AOP と Swing ベースのアプリケーションの統合は得られませんでした。これは、ほとんどの例が Web フレーム作業 (Spring など) 用であるためです。よろしくお願いします。
1 に答える
3
私はあなたの問題を見ることができません。Swing かどうかに関係なく、Java クラスを使用しているだけです。AspectJを他の Java クラスに適用できる場合は、Swing クラスにも適用できます。基本的な概念を理解すれば、AspectJ を使用した AOP は本当に楽しいものです。具体的な質問がありましたら、できる限りお手伝いさせていただきます。
更新:わかりました、少し余裕があったので、サンプル コードを使用して簡単なデモをハックしました。
メイン ウィンドウの作成中に 1 つ、ポップアップ ダイアログで [OK] をクリックしたときに 1 つ、2 つの RuntimeExceptions をスローする最小限の Swing の例:
import java.awt.event.*;
import javax.swing.*;
public final class MinimalSwingApplication {
public static void main(String... aArgs) {
MinimalSwingApplication app = new MinimalSwingApplication();
app.buildAndDisplayGui();
}
private void buildAndDisplayGui() {
JFrame frame = new JFrame("Main window");
buildContent(frame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private void buildContent(JFrame aFrame) {
JPanel panel = new JPanel();
panel.add(new JLabel("Hello"));
JButton ok = new JButton("Show pop-up dialog");
ok.addActionListener(new ShowDialog(aFrame));
panel.add(ok);
aFrame.getContentPane().add(panel);
throw new RuntimeException("Oops!");
}
private static final class ShowDialog implements ActionListener {
private JFrame fFrame;
ShowDialog(JFrame aFrame) {
fFrame = aFrame;
}
public void actionPerformed(ActionEvent aEvent) {
JOptionPane.showMessageDialog(fFrame, "I am the a pop-up dialog");
throw new RuntimeException("Something unexpected happened here");
}
}
}
例外をログに記録するサンプルアスペクト (JDK ロギングを使用しますが、Log4J に簡単に切り替えることができます):
import java.util.logging.*;
public aspect SwingExceptionLogger {
static Logger logger = Logger.getLogger(SwingExceptionLogger.class.getName());
Object around() : execution(* MinimalSwingApplication..*(..)) {
try {
return proceed();
} catch (Exception e) {
logger.log(Level.WARNING, "Swing exception: " + e.getMessage());
return null;
}
}
}
アプリケーションを起動し、ポップアップ ダイアログを 2 回開いたり閉じたりした場合の出力例:
10.11.2012 09:42:28 MinimalSwingApplication buildContent_aroundBody5$advice
WARNUNG: Swing exception: Oops!
10.11.2012 09:42:33 MinimalSwingApplication$ShowDialog actionPerformed_aroundBody1$advice
WARNUNG: Swing exception: Something unexpected happened here
10.11.2012 09:42:37 MinimalSwingApplication$ShowDialog actionPerformed_aroundBody1$advice
WARNUNG: Swing exception: Something unexpected happened here
必要に応じて、AWT/Swing スレッドからのみログに記録するようにポイントカットをいつでも調整できます。飲み込む代わりに、例外を再スローすることもできます。気軽に体験したり、質問したりしてください。
于 2012-11-06T09:14:28.537 に答える