简体   繁体   English

Spring @Around用于异常处理

[英]Spring @Around for exception handling

The situation is the following: there is a framework that is extended by my application. 情况如下:我的应用程序扩展了一个框架。 So I have no main() or any other entry points declared by me (I override methods provided by that framework and it arranges everything else). 因此,我没有main()或我声明的任何其他入口点(我重写了该框架提供的方法,并安排了其他所有内容)。 This framework can be made secure; 这个框架可以保证安全。 the security is built on top of apache mina. 安全性是基于Apache Mina构建的。

When an unsecure connection is made to the app, mina throws an exception, here's the stack trace: 当与应用程序建立不安全的连接时,mina会引发异常,这是堆栈跟踪:

Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
        at sun.security.ssl.EngineInputRecord.bytesInCompletePacket(EngineInputRecord.java:171) ~[na:1.7.0_21]
        at sun.security.ssl.SSLEngineImpl.readNetRecord(SSLEngineImpl.java:845) ~[na:1.7.0_21]
        at sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:758) ~[na:1.7.0_21]
        at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:624) ~[na:1.7.0_21]
        at org.apache.mina.filter.support.SSLHandler.unwrap0(SSLHandler.java:657) ~[mina-filter-ssl-1.1.7.jar:na]
        at org.apache.mina.filter.support.SSLHandler.unwrapHandshake(SSLHandler.java:613) ~[mina-filter-ssl-1.1.7.jar:na]
        at org.apache.mina.filter.support.SSLHandler.handshake(SSLHandler.java:493) ~[mina-filter-ssl-1.1.7.jar:na]
        at org.apache.mina.filter.support.SSLHandler.messageReceived(SSLHandler.java:306) ~[mina-filter-ssl-1.1.7.jar:na]
        at org.apache.mina.filter.SSLFilter.messageReceived(SSLFilter.java:392) ~[mina-filter-ssl-1.1.7.jar:na]
        ... 12 common frames omitted

I wanted to catch this using an AOP trick, so I can inform the user in a nice way, and can send an alert. 我想使用AOP技巧来捕获此错误,因此我可以很好地通知用户并发送警报。 I came up with this aspect: 我想到了这方面:

@Aspect
public class SSLExceptionCatcherAdvice {

    private static final Logger LOG = LoggerFactory.getLogger(SSLExceptionCatcherAdvice.class);

    @Around("execution(* *.unwrap(..))")
    public void catchException(ProceedingJoinPoint pjp) {
        try {
            pjp.proceed();
        } catch (Throwable exception) {
            LOG.info("########################################");
        }
    }

}

But, it does not get invoked at all. 但是,它根本不会被调用。 The aspect is fine, btw, it can catch other method invocations from my codebase. 顺便说一句,这很好,它可以捕获我代码库中的其他方法调用。

Any advice on this? 有什么建议吗? Thx in advance. 提前谢谢。

Spring by default uses a proxy based AOP solution and as a result of that only Spring managed beans will be able to have those aspects applied. 默认情况下,Spring使用基于代理的AOP解决方案,因此,只有Spring托管的bean才能应用这些方面。 It will not work on non-spring managed beans. 它不适用于非春季托管的Bean。 See this section of the reference guide . 请参阅参考指南的本部分

You are trying to intercept the exection on a javax. 您正在尝试拦截Javax上的执行javax. package which is a special case and will only work in a load-time weaving environment and not with a proxy-based or compile-time based solution. 软件包,这是一种特殊情况,只能在加载时编织环境中使用,而不能与基于代理或基于编译时的解决方案一起使用。 Also weaving them with loadtime weaving might also be tricky as those classes are probably already loaded before loadtime-weaving kicks in. 将它们与加载时编织结合起来也可能很棘手,因为这些类可能在加载时编织之前就已经加载了。

The aspect is fine, btw, it can catch other method invocations from my codebase. 顺便说一句,这很好,它可以捕获我代码库中的其他方法调用。

Actually your aspect is flawed, an @Around advice should always return Object and should always return the result of calling the proceed() method (unless you are rethrowing the Exception . Your aspect breaks properly returning the result and now effectivly every method returns null . 实际上,您的方面有缺陷, @Around通知应始终返回Object并应始终返回调用proceed()方法的结果(除非您抛出Exception 。您的方面正常中断,返回结果,现在有效地每个方法都返回null

Links: 链接:

  1. Proxying Mechanisms | 代理机制| reference guide 参考指南
  2. Load-time weaving with AspectJ in the Spring Framework | 在Spring Framework中使用AspectJ进行加载时编织 reference guide 参考指南
  3. Around advice | 周围建议| reference guide 参考指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM