天天看點

spring之AOP學習

AOP的開發中的相關術語: Joinpoint(連接配接點):      所謂連接配接點是指那些被攔截到的點。在spring中,這些點指的是方法,因為spring隻支援方法類型的連接配接點. Pointcut(切入點):      所謂切入點是指我們要對哪些Joinpoint進行攔截的定義. Advice(通知/增強):      所謂通知是指攔截到Joinpoint之後所要做的事情就是通知.通知分為前置通知,後置通知,異常通知,最終通知,環繞通知(切面要完成的功能) Introduction(引介):      引介是一種特殊的通知在不修改類代碼的前提下, Introduction可以在運作期為類動态地添加一些方法或Field. Target(目标對象):      代理的目标對象 Weaving(織入):      是指把增強應用到目标對象來建立新的代理對象的過程,spring采用動态代理織入,而AspectJ采用編譯期織入和類裝在期織入 Proxy(代理):      一個類被AOP織入增強後,就産生一個結果代理類 Aspect(切面):       是切入點和通知(引介)的結合

Spring按照通知Advice在目标類方法的連接配接點位置,可以分為5類 前置通知(在方法之前執行)      org.springframework.aop.MethodBeforeAdvice 在目标方法執行前實施增強 後置通知(在方法之後執行)      org.springframework.aop.AfterReturningAdvice 在目标方法執行後實施增強 環繞通知      org.aopalliance.intercept.MethodInterceptor 在目标方法執行前後實施增強 異常抛出通知      org.springframework.aop.ThrowsAdvice 在方法抛出異常後實施增強 引介通知      org.springframework.aop.IntroductionInterceptor 在目标類中添加一些新的方法和屬性

一個簡單的例子: 待增強類:

public class StudentDao {
	public void save(){
		System.out.println("我在執行添加操作。。。。。");
	}
	public int update(){
		System.out.println("我在執行修改操作。。。。。");
		return 10000;
	}
	public void delete(){
		System.out.println("我在執行删除操作。。。。。");
	}
	public void find(){
		System.out.println("我在執行查詢操作。。。。。");
		int d= 11/0;
	}
}
           

AspectJ類

public class SelfAspectJ {
	public void before(){
		System.out.println("前置增強=============");
	}
	
	public void afterReturing(Object obj){
		System.out.println("後置增強============="+obj);
	}
	
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
		System.out.println("環繞前增強===========");
		Object obj = joinPoint.proceed();
		System.out.println("環繞後增強===========");
		return obj;
	}
	
	public void afterThrowing(Throwable e){
		System.out.println("異常抛出通知=========="+e.getMessage());
	}
	
	public void after(){
		System.out.println("最終通知=============");
	}
}
           

配置檔案中:

<bean id = "studentDao" class="com.study.StudentDao"></bean>
	
	<bean id="selfAspectJ" class="com.study.SelfAspectJ"></bean>
	
	<!-- Aop的配置 -->
	<aop:config>
		<!-- 切入點的配置,哪些類的哪些方法需要增強 -->
		<aop:pointcut expression="execution(* com.study.StudentDao.save(..))" id="cut1"/>
		<aop:pointcut expression="execution(* com.study.StudentDao.update(..))" id="cut2"/>
		<aop:pointcut expression="execution(* com.study.StudentDao.delete(..))" id="cut3"/>
		<aop:pointcut expression="execution(* com.study.StudentDao.find(..))" id="cut4"/>
		<!-- 配置切面 -->
		<aop:aspect ref="selfAspectJ">
			<!-- 前置增強 -->
			<aop:before method="before" pointcut-ref="cut1" />
			<!-- 後置增強 -->
			<aop:after-returning method="afterReturing" pointcut-ref="cut2" returning="obj" />
			<!-- 環繞增強 -->
			<aop:around method="around" pointcut-ref="cut3" />
			<!-- 異常抛出增強 -->
			<aop:after-throwing method="afterThrowing" pointcut-ref="cut4" throwing="e" />
			<!-- 最終增強 -->
			<aop:after method="after" pointcut-ref="cut4"/>
		</aop:aspect>
	</aop:config>
           

測試類

@Test
	public void testOne(){
		studentDao.save();
		System.out.println("***********************************");
		studentDao.update();
		System.out.println("***********************************");
		studentDao.delete();
		System.out.println("***********************************");
		studentDao.find();
	}
           

結果:

spring之AOP學習