成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何動態修改 Spring Aop 切面信息?讓自動日志輸出框架更好用

開發 架構
對于 Spring 項目,默認實現了基于 Aop 切面的日志輸出。發現一個問題,如果切面定義為全切范圍過大,于是 v0.2 版本就是基于注解 @AutoLog 實現的。

業務背景

很久以前開源了一款 auto-log[1] 自動日志打印框架。

其中對于 spring 項目,默認實現了基于 aop 切面的日志輸出。

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

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

如何才能動態指定 pointcut,讓用戶使用時可以自定義切面范圍呢?

自定義注解切面原理

常規 aop 方式

@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 獲取屬性配置的切面值,給定默認值。這樣用戶就可以很方便的自定義。

/**
 * 動態配置的切面
 * 自動日志輸出 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 接口即可。

/**
 * 切面攔截器
 *
 * @author binbin.hou
 * @since 0.3.0
 */
public class AutoLogAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        // 增強邏輯
    }
}

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

spring 整合使用

完整示例參考 SpringServiceTest[2]。

maven 引入

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>auto-log-spring</artifactId>
    <version>0.3.0</version>
</dependency>

注解聲明

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

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

測試代碼

@ContextConfiguration(classes = SpringConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringServiceTest {
    @Autowired
    private UserService userService;
    @Test
    public void queryLogTest() {
        userService.queryLog("1");
    }
}

輸出結果

信息: 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 的屬性值即可。

測試例子

完整測試代碼[3]

我們在配置文件 autoLogConfig.properties 中自定義下包掃描的范圍:

auto.log.pointcut=execution(* com.github.houbb.auto.log.test.dynamic.service.MyAddressService.*(..))

自定義測試 service。

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 中指定,此處僅作為演示。

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

測試

@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: https://github.com/houbb/auto-log

Gitee: https://gitee.com/houbinbin/auto-log

小結

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

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

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

References

[1] auto-log: https://github.com/houbb/auto-log。

[2] SpringServiceTest: https://github.com/houbb/auto-log/tree/master/auto-log-test/src/test/java/com/github/houbb/auto/log/spring/SpringServiceTest.java。

[3] 完整測試代碼: https://github.com/houbb/auto-log/blob/master/auto-log-test/src/test/java/com/github/houbb/auto/log/dynamic/SpringDynamicServiceTest.java。

責任編輯:姜華 來源: 今日頭條
相關推薦

2022-06-07 07:58:45

SpringSpring AOP

2024-06-11 00:04:00

對象AdvisorAdvice

2009-09-29 10:00:40

Spring AOP框

2021-11-30 05:55:02

微信工具騰訊

2012-02-10 10:32:33

JavaSpring

2022-06-08 08:04:28

Springservicerepository

2021-03-01 23:26:41

日志Spring BootAOP

2024-04-10 08:59:39

SpringAOP業務

2024-05-14 10:21:14

Web 框架Node.jsJavaScript

2009-08-24 09:46:40

面向切面編程AOP

2017-08-23 08:50:00

Safari瀏覽器擴展

2022-12-26 08:09:51

mac設置搜索

2021-09-01 10:35:19

asyncawaitVue

2021-10-28 23:57:01

日志Serilog框架

2013-09-17 10:37:03

AOPAOP教程理解AOP

2018-03-22 14:49:58

iOSiPhone手機

2022-02-08 17:07:54

Spring BooSpring Aop日志記錄

2010-11-01 09:25:00

2012-12-25 09:58:50

數據科學家大數據

2011-04-26 09:33:04

SpringAOP
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲 欧美 日韩在线 | 五月综合激情网 | 成人久久久 | 久久一区二区三区免费 | 中文二区| 国产日韩欧美 | www四虎影视| 日韩久草| 日本精品视频 | 中文字幕视频在线观看 | 玖玖国产 | 久久av一区二区三区 | 亚洲一区二区三区免费观看 | 7777在线视频| 欧美国产日韩精品 | 久久一区 | 在线观看涩涩视频 | 激情a| 最近中文字幕在线视频1 | 国产视频一区二区三区四区五区 | 久草视频网站 | 成人国产精品久久 | 成人在线观 | 欧产日产国产精品国产 | 国产精品视频导航 | 日本三级网站在线观看 | 成人在线观看免费 | 自拍偷拍亚洲欧美 | 国产小视频在线 | 日韩av一区二区在线观看 | 91精品久久久久久久久久入口 | 欧美日韩在线成人 | 天堂在线网 | 日日摸日日碰夜夜爽亚洲精品蜜乳 | 久久高清 | 久久久天堂 | 97久久久久久久久 | 日韩精品a在线观看图片 | 91精品久久久久久久久久 | 亚洲网站在线 | 国产中文字幕在线观看 |