最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

動(dòng)態(tài)修改spring?aop?切面信息提升自動(dòng)日志輸出框架效率

 更新時(shí)間:2023年07月23日 09:04:10   作者:老馬嘯西風(fēng)  
這篇文章主要為大家介紹了動(dòng)態(tài)修改spring?aop切面信息提升自動(dòng)日志輸出框架效率,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

業(yè)務(wù)背景

很久以前開源了一款 auto-log 自動(dòng)日志打印框架。

其中對(duì)于 spring 項(xiàng)目,默認(rèn)實(shí)現(xiàn)了基于 aop 切面的日志輸出。

但是發(fā)現(xiàn)一個(gè)問(wèn)題,如果切面定義為全切范圍過(guò)大,于是 v0.2 版本就是基于注解 @AutoLog 實(shí)現(xiàn)的。

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

如何才能動(dòng)態(tài)指定 pointcut,讓用戶使用時(shí)可以自定義切面范圍呢?

自定義注解切面原理

常規(guī) 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() {
    }
    /**
     * 執(zhí)行核心方法
     *
     * 相當(dāng)于 MethodInterceptor
     *
     * @param point 切點(diǎn)
     * @return 結(jié)果
     * @throws Throwable 異常信息
     * @since 0.0.3
     */
    @Around("autoLogPointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        // 日志增強(qiáng)邏輯
    }
}

發(fā)現(xiàn)這里的 @Pointcut 注解屬性是一個(gè)常量,無(wú)法方便地動(dòng)態(tài)修改。

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

可以指定 pointcut 的方式

我們通過(guò) @Value 獲取屬性配置的切面值,給定默認(rèn)值。這樣用戶就可以很方便的自定義。

/**
 * 動(dòng)態(tài)配置的切面
 * 自動(dòng)日志輸出 aop
 * @author binbin.hou
 * @since 0.3.0
 */
@Configuration
@Aspect
//@EnableAspectJAutoProxy
public class AutoLogDynamicPointcut {
    /**
     * 切面設(shè)置,直接和 spring 的配置對(duì)應(yīng) ${},可以從 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;
    }
}

當(dāng)然,這里的 Advice 和以前的 aop 不同,需要重新進(jìn)行實(shí)現(xiàn)。

AutoLogAdvice

只需要實(shí)現(xiàn) MethodInterceptor 接口即可。

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

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

}

介紹完了原理,我們一起來(lái)看下改進(jìn)后的日志打印組件的效果。

spring 整合使用

完整示例參考 SpringServiceTest

maven 引入

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

注解聲明

使用 @EnableAutoLog 啟用自動(dòng)日志輸出

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

測(cè)試代碼

@ContextConfiguration(classes = SpringConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringServiceTest {

    @Autowired
    private UserService userService;

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

}

輸出結(jié)果

信息: 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}"),默認(rèn)為值 @within(com.github.houbb.auto.log.annotation.AutoLog)||@annotation(com.github.houbb.auto.log.annotation.AutoLog)

也就是默認(rèn)是讀取被 @AutoLog 指定的方法或者類。

當(dāng)然,這并不夠方便,我們希望可以想平時(shí)寫 aop 注解一樣,指定 spring aop 的掃描范圍,直接在 spring 中指定一下 auto.log.pointcut 的屬性值即可。

測(cè)試?yán)?/h3>

完整測(cè)試代碼

我們?cè)谂渲梦募?nbsp;autoLogConfig.properties 中自定義下包掃描的范圍:

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

自定義測(cè)試 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 {
}

測(cè)試

@ContextConfiguration(classes = SpringDynamicConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringDynamicServiceTest {

    @Autowired
    private MyAddressService myAddressService;

    @Autowired
    private MyUserService myUserService;

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

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

}

開源地址

為了便于大家學(xué)習(xí),項(xiàng)目已開源。

Github: https://github.com/houbb/auto-log

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

小結(jié)

這個(gè)項(xiàng)目很長(zhǎng)一段時(shí)間拘泥于注解的方式,我個(gè)人用起來(lái)也不是很方便。

最近才想到了改進(jìn)的方法,人還是要不斷學(xué)習(xí)進(jìn)步。

關(guān)于日志最近還學(xué)到了 aspect 的編譯時(shí)增強(qiáng),和基于 agent 的運(yùn)行時(shí)增強(qiáng),這 2 種方式都很有趣,有機(jī)會(huì)會(huì)做學(xué)習(xí)記錄。

以上就是動(dòng)態(tài)修改spring aop 切面信息使自動(dòng)日志輸出框架更好用的詳細(xì)內(nèi)容,更多關(guān)于spring aop切面信息動(dòng)態(tài)修改的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java 將List中的實(shí)體類按照某個(gè)字段進(jìn)行分組并存放至Map中操作

    Java 將List中的實(shí)體類按照某個(gè)字段進(jìn)行分組并存放至Map中操作

    這篇文章主要介紹了Java 將List中的實(shí)體類按照某個(gè)字段進(jìn)行分組并存放至Map中操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • 聊聊springboot中如何自定義消息轉(zhuǎn)換器

    聊聊springboot中如何自定義消息轉(zhuǎn)換器

    SpringBoot通過(guò)HttpMessageConverter處理HTTP數(shù)據(jù)轉(zhuǎn)換,支持多種媒體類型,接下來(lái)通過(guò)本文給大家介紹springboot中如何自定義消息轉(zhuǎn)換器,感興趣的朋友一起看看吧
    2025-08-08
  • Java生成PDF文件的實(shí)例代碼

    Java生成PDF文件的實(shí)例代碼

    Java生成PDF文件的實(shí)例代碼,需要的朋友可以參考一下
    2013-05-05
  • Spring中緩存注解@Cache的使用詳解

    Spring中緩存注解@Cache的使用詳解

    這篇文章主要介紹了Spring中緩存注解@Cache的使用詳解,使用注解對(duì)數(shù)據(jù)進(jìn)行緩存功能的框架,只需要簡(jiǎn)單地加一個(gè)注解,就能實(shí)現(xiàn)緩存功能,大大簡(jiǎn)化我們?cè)跇I(yè)務(wù)中操作緩存的代碼,需要的朋友可以參考下
    2023-07-07
  • 詳解如何使用Java編寫圖形化的窗口

    詳解如何使用Java編寫圖形化的窗口

    這篇文章主要介紹了如何使用Java編寫圖形化的窗口,是Java的本地GUI軟件開發(fā)的基礎(chǔ),需要的朋友可以參考下
    2015-10-10
  • Java設(shè)計(jì)模式中組合模式應(yīng)用詳解

    Java設(shè)計(jì)模式中組合模式應(yīng)用詳解

    組合模式,又叫部分整體模式,它創(chuàng)建了對(duì)象組的數(shù)據(jù)結(jié)構(gòu)組合模式使得用戶對(duì)單個(gè)對(duì)象和組合對(duì)象的訪問(wèn)具有一致性。本文將通過(guò)示例為大家詳細(xì)介紹一下組合模式,需要的可以參考一下
    2022-11-11
  • 使用mybatis報(bào)Invalid bound statement解決分析

    使用mybatis報(bào)Invalid bound statement解決分析

    這篇文章主要為大家介紹了使用mybatis報(bào)Invalid bound statement原因解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • 使用Jackson 處理 null 或者 空字符串

    使用Jackson 處理 null 或者 空字符串

    這篇文章主要介紹了使用Jackson 處理 null 或者 空字符串,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Springboot?定時(shí)任務(wù)分布式下冪等性解決方案

    Springboot?定時(shí)任務(wù)分布式下冪等性解決方案

    這篇文章主要介紹了Springboot定時(shí)任務(wù)分布式下冪等性如何解決,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • SpringBoot 自動(dòng)裝配的原理詳解分析

    SpringBoot 自動(dòng)裝配的原理詳解分析

    這篇文章主要介紹了SpringBoot 自動(dòng)裝配的原理詳解分析,文章通過(guò)通過(guò)一個(gè)案例來(lái)看一下自動(dòng)裝配的效果展開詳情,感興趣的小伙伴可以參考一下
    2022-08-08

最新評(píng)論

莱州市| 庆云县| 盱眙县| 商洛市| 菏泽市| 沙田区| 普兰县| 孙吴县| 崇阳县| 九龙坡区| 吉林市| 昭平县| 行唐县| 建宁县| 阿拉尔市| 二连浩特市| 呼伦贝尔市| 宁强县| 三江| 宿松县| 嵊泗县| 苍梧县| 荆门市| 德州市| 马鞍山市| 汉源县| 崇州市| 东明县| 寿宁县| 介休市| 贺兰县| 新竹县| 垫江县| 繁昌县| 汾阳市| 祁连县| 江华| 昆明市| 博兴县| 南投县| 沙洋县|