SpringMVC配置aop實例教程

已經配置好的demo下載:
請點擊
教程開始:

1.首先一個最基本的springmvc框架;
相關的教程:https://blog.csdn.net/qq_23095607/article/details/100902345
相關的springmvc demo:https://download.csdn.net/download/qq_23095607/11752267

2.總體的結構爲:
在這裏插入圖片描述
3.在這個基礎上導入依賴:

<!--spring-aop-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>${spring.version}</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>${spring.version}</version>
</dependency>

4.springContext.xml開啓AOP功能

<aop:aspectj-autoproxy/>

注意:一定要檢查一下你要掃描的包是不是包含了所有的註解,如下:

<!-- 搜索spring控件,掃描所有帶有註解的包,別丟東西了 -->
<context:component-scan base-package="com."></context:component-scan>
<aop:aspectj-autoproxy/>

5.編寫相關的切面,以及切面邏輯,這裏就不一一講了,可以自行百度,直接貼代碼:

package com.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @desc: 經紀人切面
 * @author: CSH 參考:https://www.cnblogs.com/chuijingjing/p/9806651.html
 **/
@Aspect
@Component
public class BrokerAspect {
	/**
	 * 定義切入點,切入點爲com.example.demo.aop.AopController中的所有函數 通過@Pointcut註解聲明頻繁使用的切點表達式
	 */
	@Pointcut("execution( * com.huaxi.AopController.*(..)))")
	public void BrokerAspect() {
	}

	/**
	 * @description 在連接點執行之前執行的通知 一定要保證JoinPoint處在參數列表的第一位,否則拋異常
	 */
	@Before("BrokerAspect()")
	public void doBeforeGame(JoinPoint joinPoint) {
		System.out.println("------before advice start------");
		Object target = joinPoint.getTarget().getClass().getName();
		String name = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();// 2.傳參
		System.out.println("所用的類名爲:" + target);
		System.out.println("所用的方法名爲:" + name);
		System.out.print("傳遞的參數有:");
		for (Object object : args) {
			System.out.println(object);
		}
		System.out.println("------before advice end------");
	}

	/**
	 * @description 在連接點執行之後執行的通知(返回通知和異常通知的異常) 一定要保證JoinPoint處在參數列表的第一位,否則拋異常
	 */
	@After("BrokerAspect()")
	public void doAfterGame(JoinPoint joinPoint) {
		System.out.println("------after advice start------");
		Object target = joinPoint.getTarget().getClass().getName();
		String name = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();// 2.傳參
		System.out.println("所用的類名爲:" + target);
		System.out.println("所用的方法名爲:" + name);
		System.out.print("傳遞的參數有:");
		for (Object object : args) {
			System.out.println(object);
		}
		System.out.println("------after advice end------");
	}

	/**
	 * @description 在連接點執行之後執行的通知(返回通知) 一定要保證JoinPoint處在參數列表的第一位,否則拋異常
	 */
	@AfterReturning(returning = "rvt", pointcut = "BrokerAspect()")
	public void doAfterReturningGame(JoinPoint joinPoint, Object rvt) {
		System.out.println("------after-return advice start------");
		Object target = joinPoint.getTarget().getClass().getName();
		String name = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();// 2.傳參
		System.out.println("所用的類名爲:" + target);
		System.out.println("所用的方法名爲:" + name);
		System.out.print("傳遞的參數有:");
		for (Object object : args) {
			System.out.println(object);
		}
		System.out.println("返回值爲:" + rvt);
		System.out.println("------after-return advice end------");
	}

	/**
	 * @description 在連接點執行之後執行的通知(異常通知) 一定要保證JoinPoint處在參數列表的第一位,否則拋異常
	 */
	@AfterThrowing(throwing = "ex", pointcut = "BrokerAspect()")
	public void doAfterThrowingGame(JoinPoint joinPoint, Throwable ex) {
		System.out.println("------AfterThrowing advice start------");
		Object target = joinPoint.getTarget().getClass().getName();
		String name = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();// 2.傳參
		System.out.println("所用的類名爲:" + target);
		System.out.println("所用的方法名爲:" + name);
		System.out.print("傳遞的參數有:");
		for (Object object : args) {
			System.out.println(object);
		}
		System.out.println("拋出的異常爲::" + ex);
		System.out.println("------AfterThrowing advice end------");
	}

	
	//---------------------------------------------------------------------------------------
	
	//-----------------由於@around功能跟強大,影響到了頁面的邏輯,本人已經屏蔽,有需要的可以放開測試-----------------------------------------
	
	//-----------------------------------------------------------------------------------------
	/*
	 * @description近似於Before增強處理和AfterReturing增強處理的總結 可以修改參數,自動調用方法等強大的功能
	 * 第一個形參必須是ProceedJoinPoint類型(至少含有一個形參 環繞通知需要返回返回值,否則真正調用者將拿不到返回值,只能得到一個null。
	 * 環繞通知有控制目標方法是否執行、有控制是否返回值、有改變返回值,改變參數的能力。
	 */	
	
/*	@Around("BrokerAspect()")
	public Object processTx(ProceedingJoinPoint joinPoint)throws Throwable{
		System.out.println("------Around advice start------");
		Object target = joinPoint.getTarget().getClass().getName();
		String name = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();// 2.傳參
		System.out.println("所用的類名爲:" + target);
		System.out.println("所用的方法名爲:" + name);
		System.out.print("傳遞的參數有:");
		for (Object object : args) {
			System.out.println(object);
		}

		// 獲取目標方法原始的調用參數
		if (args != null && args.length >= 1) {
			// 修改目標方法的第一個參數
			args[0] = "【經過了around  advice 不需要請在後臺註銷整個@around】" + args[0];
			System.out.println(args[0]);
		}

		// 以改變後的參數去執行目標方法,並保存目標方法執行後的返回值
		//rvt爲返回值,可以自定義就行返回。
		Object rvt = joinPoint.proceed(args);
		// 如果rvt的類型是Integer,將rvt改爲它的平方
		if (rvt != null && rvt instanceof Integer)
			rvt = (Integer) rvt * (Integer) rvt;
		System.out.println("------Around advice end------");
		return rvt;
	}*/

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章