天天看點

如何動态修改 spring aop 切面資訊?讓自動日志輸出架構更好用

作者:架構師之道

業務背景

很久以前開源了一款 auto-log 自動日志列印架構。

其中對于 spring 項目,預設實作了基于 aop 切面的日志輸出。

但是發現一個問題,如果切面定義為全切範圍過大,于是 v0.2 版本就是基于注解 @AutoLog 實作的。

隻有指定注解的類或者方法才會生效,但是這樣使用起來很不友善。

如何才能動态指定 pointcut,讓使用者使用時可以自定義切面範圍呢?

如何動态修改 spring aop 切面資訊?讓自動日志輸出架構更好用

自定義注解切面原理

正常 aop 方式

java複制代碼@Aspect
@Component
@EnableAspectJAutoProxy
@Deprecated
public class AutoLogAop {

    @Pointcut("@within(com.github.houbb.auto.log.annotation.AutoLog)" +
            "|| @annotation(com.github.houbb.auto.log.annotation.AutoLog)")
    public void autoLogPointcut() {
    }

    /**
     * 執行核心方法
     *
     * 相當于 MethodInterceptor
     *
     * @param point 切點
     * @return 結果
     * @throws Throwable 異常資訊
     * @since 0.0.3
     */
    @Around("autoLogPointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        // 日志增強邏輯
    }

}
           

發現這裡的 @Pointcut 注解屬性是一個常量,無法友善地動态修改。

于是去查資料,找到了另一種更加靈活的方式。

可以指定 pointcut 的方式

我們通過 @Value 擷取屬性配置的切面值,給定預設值。這樣使用者就可以很友善的自定義。

java複制代碼/**
 * 動态配置的切面
 * 自動日志輸出 aop
 * @author binbin.hou
 * @since 0.3.0
 */
@Configuration
@Aspect
//@EnableAspectJAutoProxy
public class AutoLogDynamicPointcut {

    /**
     * 切面設定,直接和 spring 的配置對應 ${},可以從 properties 或者配置中心讀取。更加靈活
     */
    @Value("${auto.log.pointcut:@within(com.github.houbb.auto.log.annotation.AutoLog)||@annotation(com.github.houbb.auto.log.annotation.AutoLog)}")
    private String pointcut;

    @Bean("autoLogPointcutAdvisor")
    public AspectJExpressionPointcutAdvisor autoLogPointcutAdvisor() {
        AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
        advisor.setExpression(pointcut);
        advisor.setAdvice(new AutoLogAdvice());
        return advisor;
    }

}
           

當然,這裡的 Advice 和以前的 aop 不同,需要重新進行實作。

AutoLogAdvice

隻需要實作 MethodInterceptor 接口即可。

java複制代碼/**
 * 切面攔截器
 *
 * @author binbin.hou
 * @since 0.3.0
 */
public class AutoLogAdvice implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        // 增強邏輯
    }

}
           

介紹完了原理,我們一起來看下改進後的日志列印元件的效果。

spring 整合使用

完整示例參考 SpringServiceTest

maven 引入

xml複制代碼<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>auto-log-spring</artifactId>
    <version>0.3.0</version>
</dependency>
           

注解聲明

使用 @EnableAutoLog 啟用自動日志輸出

java複制代碼@Configurable
@ComponentScan(basePackages = "com.github.houbb.auto.log.test.service")
@EnableAutoLog
public class SpringConfig {
}
           

測試代碼

java複制代碼@ContextConfiguration(classes = SpringConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringServiceTest {

    @Autowired
    private UserService userService;

    @Test
    public void queryLogTest() {
        userService.queryLog("1");
    }

}
           
  • 輸出結果
vbnet複制代碼資訊: public java.lang.String com.github.houbb.auto.log.test.service.impl.UserServiceImpl.queryLog(java.lang.String) param is [1]
五月 30, 2020 12:17:51 下午 com.github.houbb.auto.log.core.support.interceptor.AutoLogMethodInterceptor info
資訊: public java.lang.String com.github.houbb.auto.log.test.service.impl.UserServiceImpl.queryLog(java.lang.String) result is result-1
五月 30, 2020 12:17:51 下午 org.springframework.context.support.GenericApplicationContext doClose
           

切面自定義

原了解釋

spring aop 的切面讀取自 @Value("${auto.log.pointcut}"),預設為值 @within(com.github.houbb.auto.log.annotation.AutoLog)||@annotation(com.github.houbb.auto.log.annotation.AutoLog)

也就是預設是讀取被 @AutoLog 指定的方法或者類。

當然,這并不夠友善,我們希望可以想平時寫 aop 注解一樣,指定 spring aop 的掃描範圍,直接在 spring 中指定一下 auto.log.pointcut 的屬性值即可。

測試例子

完整測試代碼

我們在配置檔案 autoLogConfig.properties 中自定義下包掃描的範圍:

c複制代碼auto.log.pointcut=execution(* com.github.houbb.auto.log.test.dynamic.service.MyAddressService.*(..))
           

自定義測試 service

java複制代碼package com.github.houbb.auto.log.test.dynamic.service;

import org.springframework.stereotype.Service;

@Service
public class MyAddressService {

    public String queryAddress(String id) {
        return "address-" + id;
    }

}
           

自定義 spring 配置,指定我們定義的配置檔案。springboot 啥的,可以直接放在 application.properties 中指定,此處僅作為示範。

java複制代碼@Configurable
@ComponentScan(basePackages = "com.github.houbb.auto.log.test.dynamic.service")
@EnableAutoLog
@PropertySource("classpath:autoLogConfig.properties")
public class SpringDynamicConfig {
}
           

測試

java複制代碼@ContextConfiguration(classes = SpringDynamicConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringDynamicServiceTest {

    @Autowired
    private MyAddressService myAddressService;

    @Autowired
    private MyUserService myUserService;

    @Test
    public void queryUserTest() {
        // 不會被日志攔截
        myUserService.queryUser("1");
    }

    @Test
    public void queryAddressTest() {
        // 會被日志攔截
        myAddressService.queryAddress("1");
    }

}
           

開源位址

為了便于大家學習,項目已開源。

Github: github.com/houbb/auto-…
Gitee: gitee.com/houbinbin/a…

小結

這個項目很長一段時間拘泥于注解的方式,我個人用起來也不是很友善。

最近才想到了改進的方法,人還是要不斷學習進步。

關于日志最近還學到了 aspect 的編譯時增強,和基于 agent 的運作時增強,這 2 種方式都很有趣,有機會會做學習記錄。

作者:老馬嘯西風

連結:https://juejin.cn/post/7258483153869865020