Spring AOP

Aspect Oriented Programming(AOP):意為:面向切面編程。從本質上講,它是一種無需修改代碼即可向現有代碼添加行為的方法。通過預編譯方式和運行期間動態代理實現程序功能的統一維護的一種技術。是Spring框架中的一個重要內容,利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程式的可重用性,同時提高了開發的效率。

主要功能 日誌記錄、性能統計、安全控制、事務處理、異常處理等等。 我們知道AOP是通過動態代理實現的,而Spring的AOP是實現有兩種,一個是JDK動態代理,一個是CGLIB實現。

技術詳解參考

實作測試 - 效能統計(Method執行時間)

annotation方式:

定義一個annotation介面

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecutionTime {
}

aspect class

@Aspect
@Component
public class LogExecutionTimeAop {
	Logger log = LoggerFactory.getLogger("aopTest");

	@Pointcut("@annotation(com.ecommerce.mini.anno.LogExecutionTime)")
	public void logExecution() {
	}

	@Around("logExecution()")
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
		log.debug("---> around");
		long start = System.currentTimeMillis();

		Object obj = joinPoint.proceed();

		long executionTime = System.currentTimeMillis() - start;
		log.debug("---> around " + joinPoint.getSignature() + " executed in " + executionTime + "ms");
		return obj;
	}

	@After("logExecution()")
	public void after(JoinPoint joinPoint) {
		log.debug("---> after");
	}

	@AfterReturning(value = "logExecution()", returning = "returnObject")
	public void afterReturning(JoinPoint joinPoint, Object returnObject) {
		log.debug("---> AfterReturning");
	}

	@Before("logExecution()")
	public void before(JoinPoint joinPoint) {
		log.debug("---> before");
	}
}

Controller

output

execution方式:

aspect class 增加

output

實作測試 - 日誌紀錄

實際應用場景會出現在記錄後台使用者CRUD記錄,當問題出現時可以根據當時User傳送的資料來當偵錯測試的依據。Server Reponse 有一定規範,操作上會更有架構性。

aspect class

Controller

output

結語

在實際使用場景上,還是有許多需要考慮的地方,比如在比對前後異動資訊時,需要不去影響核心邏輯部分資料,在切分工作時要再更細心一點。

這是我實際透過AOP 紀錄 user action log

Last updated