异常方面可能看起来像这样:
@Aspectpublic class ExceptionAspect { private static final Logger log = LoggerFactory.getLogger(ExceptionAspect.class); public Object handle(ProceedingJoinPoint pjp) throws Throwable { try { return pjp.proceed(); } catch (Throwable t) { // so something with t: log, wrap, return default, ... log.warn("invocation of " + pjp.getSignature().toLongString() + " failed", t); // I hate logging and re-raising, but let's do it for the sake of this example throw t; } }}
spring会议:
<!-- log exceptions for any method call to any object in a package called 'svc' --><bean name="exceptionAspect" /><aop:config> <aop:aspect ref="exceptionAspect"> <aop:around method="handle" pointcut="execution(* org.example..svc..*.*(..))" /> </aop:aspect></aop:config>
编辑:
如果您希望记录器代表包装好的Bean登录,则可以这样做:
LoggerFactory.getLogger(pjp.getTarget().getClass()).warn("damn!");
或者,如果您更喜欢此方法的声明类,而不是实际的(可能是代理/自动生成的类型):
LoggerFactory.getLogger(pjp.getSignature().getDeclaringType()).warn("damn!");
老实说,我无法估计每次调用LoggerFactory.getLogger(..)会对性能产生的影响。我认为应该不会太糟,因为无论如何例外都是例外(即罕见)。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)