Is using Spring AOP for logging a good idea?

后端 未结 3 1054
耶瑟儿~
耶瑟儿~ 2020-12-08 06:58

I\'m reading up on Spring at the moment and one of the examples used for a use of AOP is logging the start and end of method calls.

I\'ve also read that using AOP ca

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 07:25

    I used Spring AOP for implementing logging so I share my observations:

    • Performance impact is not sufficient, it is less than impact of logging itself
    • Having aspects configured in Spring configuration, you are able to completely disable logging code if necessary
    • Debugging becomes more problematic as stack traces become rather longer
    • Such decision sufficiently affects design. It is not only that you get a bunch of interfaces and aspect classes, but you production classes must be very "thin". Don't forget, you are unable to intercept calls to non-public methods. Self-calls (even to public methods) also cannot be intercepted (as you working with naked this handle instead of handle wrapped by AOP) and thus cannot be logged. So all logging can happen only on interface boundaries. (This concerns using Proxy-based aspect weaving, there's an option of runtime subclassing with cglib, but I didn't use it)
    • Writing pointcuts can be very tricky. IntelliJ Idea helps greatly determining which methods are to be adviced by pointcut.
    • Generally, I liked this approach and think it is worth using, but it appeared far more complicated than I expected

提交回复
热议问题