Spring註解AOP使用方法

目錄

1.定義主配置類

2.定義目標類

3.定義切面類

4.開始測試


1.定義主配置類

package com.qz.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import com.qz.aop.LogAspects;
import com.qz.aop.MathCaculator;

/**
 * 【AOP底層就是動態代理】
 * AOP: 指的是在程序運行期間動態的將某段代碼插入到某個方法的指定位置的編程方式
 * 1、導入AOP模塊;Spring-aspects Jar包
 * 2、定義一個業務邏輯類(Mathcaculator)
 * 3、定義一個切面類(LogAspects),切面類裏面的方法需要動態感知目標方法執行到什麼位置
 *		通知方法:
 *				前置通知:@Before 在目標方法運行之前運行
 *				後置通知:@After 在目標方法運行結束之後運行(無論正常還是異常)
 *				返回通知:@AfterReturning 在目標方法正常返回之後運行
 *				異常通知:@AfterThrowing 在目標方法出現異常之後運行
 *				環繞通知:@Around 動態代理,手動推進目標方法的運行
 * 4、給切面類目標方法標註何時何地運行(通知註解);
 * 5、將切面類和目標方法類都加入到spring容器中;
 * 6、必須告訴Spring哪個是切面類,給切面類上加一個註解(@Aspect)
 * [重點]7、開啓基於註解的切面(@EnableAspectJAutoProxy)
 */
@Configuration
@EnableAspectJAutoProxy
public class MainConfigOfAOP {
	
	@Bean
	public MathCaculator caculator(){
		return new MathCaculator();
	}
	
	@Bean
	public LogAspects logAspects(){
		return new LogAspects();
	}
}

2.定義目標類

package com.qz.aop;

public class MathCaculator {
	
	public int div(int i,int j){
		System.out.println("Math............");
		return i/j;
	}
}

3.定義切面類

package com.qz.aop;

import java.util.Arrays;

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;

@Aspect
public class LogAspects {
	/**
	 * 抽取公共的切入表達式
	 * 1、本類使用此切面表達式:@Before(" pointCut()")
	 * 2、其他切面類引用:@Before("com.qz.aop.LogAspects.pointCut()")
	 */
	@Pointcut("execution(public int com.qz.aop.MathCaculator.*(..))") //有很多的寫的方法
	public void pointCut(){};
	
	/*
	在目標方法之前切入
	@Before("public int com.qz.aop.MathCaculator.div(int, int)")  //指定div方法切入,指定參數類型
	@Before("public int com.qz.aop.MathCaculator.*(..)")  //所有的方法、所有的參數類型
	public void logStart(){
		System.out.println("除法開始運行。。。參數:{}");
	}
	*/
	
	@Before("pointCut()")  //所有的方法、所有的參數類型
	public void logStart(JoinPoint joinPoint){
		System.out.println( joinPoint.getSignature().getName() + "方法開始...參數列表:{" + Arrays.asList(joinPoint.getArgs()) + "}");
	}
	
	@After("pointCut()")
	public void logEnd(JoinPoint joinPoint){
		System.out.println(joinPoint.getSignature().getName() +"結束。。。");
	}
	
	@AfterReturning(value="pointCut()",returning="myresult")
	public void logReturn(JoinPoint joinPoint,Object myresult){//JoinPoint joinPoint必須放在第一個參數裏面
		System.out.println(joinPoint.getSignature().getName() +"正常返回。。。返回值:{"+ myresult +"}");
	}
	
	@AfterThrowing(value="pointCut()",throwing="myException")
	public void logException(JoinPoint joinPoint,Exception myException){
		System.out.println(joinPoint.getSignature().getName() +"除法異常。。。異常信息:。。。"+myException);
	}
}

4.開始測試

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.qz.aop.MathCaculator;
import com.qz.config.MainConfigOfAOP;

public class AOPTest {
	
	@Test
	public void test(){
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfigOfAOP.class);
		MathCaculator bean = context.getBean(MathCaculator.class);
		int div = bean.div(4, 2);
	}
}
/*
結果:
div方法開始...參數列表:{[4, 2]}
Math............
div結束。。。
div正常返回。。。返回值:{2}
*/
//異常測試:
public class AOPTest {
	
	@Test
	public void test(){
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfigOfAOP.class);
		MathCaculator bean = context.getBean(MathCaculator.class);
		int div = bean.div(4, 0);
	}
}
/*
結果:
div方法開始...參數列表:{[4, 0]}
Math............
div結束。。。
div除法異常。。。異常信息:。。。java.lang.ArithmeticException: / by zero
*/

 

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