動(dòng)態(tài)修改spring?aop?切面信息提升自動(dòng)日志輸出框架效率
業(yè)務(wù)背景
很久以前開源了一款 auto-log 自動(dòng)日志打印框架。
其中對(duì)于 spring 項(xiàng)目,默認(rèn)實(shí)現(xiàn)了基于 aop 切面的日志輸出。
但是發(fā)現(xiàn)一個(gè)問題,如果切面定義為全切范圍過大,于是 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 的方式
我們通過 @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è)谂渲梦募?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 刪除數(shù)組元素與刪除重復(fù)數(shù)組元素的代碼
在java中刪除數(shù)組元素與過濾重復(fù)數(shù)組元素我們都會(huì)需要去遍歷數(shù)組然后根據(jù)我們?cè)O(shè)置的值或方法進(jìn)行去除數(shù)組2013-10-10Spring Boot開箱即用可插拔實(shí)現(xiàn)過程演練與原理解析
本文通過深入探討Spring Boot的背景歷史、業(yè)務(wù)場(chǎng)景、功能點(diǎn)以及底層原理,并通過Java代碼手寫模擬了Spring Boot的啟動(dòng)過程和自動(dòng)配置功能,為開發(fā)者提供了一個(gè)全面的理解,感興趣的朋友跟隨小編一起看看吧2024-11-11java中@DateTimeFormat和@JsonFormat注解的使用
本文主要介紹了java中@DateTimeFormat和@JsonFormat注解的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Java中new與clone操作對(duì)象的比較方法舉例
這篇文章主要給大家介紹了關(guān)于Java中new與clone操作對(duì)象的比較方法,在java中對(duì)象的誕生是我們開發(fā)人員new出來(lái)的,對(duì)象的使用也是我們開發(fā)人員進(jìn)行操作的,需要的朋友可以參考下2024-07-07使用IntelliJ?IDEA創(chuàng)建簡(jiǎn)單的Java?Web項(xiàng)目完整步驟
這篇文章主要介紹了如何使用IntelliJ?IDEA創(chuàng)建一個(gè)簡(jiǎn)單的JavaWeb項(xiàng)目,實(shí)現(xiàn)登錄、注冊(cè)和查看用戶列表功能,使用Servlet和JSP技術(shù),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01