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();
	}
结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值