Spring使用AspectJ的面向切面AOP的通知

本文详细介绍了面向切面编程(AOP)中的五种通知类型:前置通知、后置通知、环绕通知、异常通知和最终通知,并通过两种实现方式——基于XML配置与基于注解的方式进行了具体说明。

1.通知

      五种通知:前置通知 、后置通知、 环绕通知、异常通知 、最终通知 。

2.通知基于XML的实现

   自定义切面类MyAspect:

        //前置通知
	public void before() {
		System.out.println("前置通知方法");
	}
	//后置通知 :改变不了结果值
	public void afterReturning() {
		System.out.println("后置通知方法");
	}              
	//环绕通知
	public Object around(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("环绕通知方法,目标方法执行之前");
		//执行目标方法
		Object result = pjp.proceed();
		System.out.println("环绕通知方法,目标方法执行之后");	
		return ((String)result).toUpperCase();
	}  
	//异常通知
	public void AfterThrowing(Exception ex) {
		System.out.println("异常通知方法  ex = " + ex.getMessage());
	}
	//最终通知:无论是否抛出异常都会执行
	public void after() {
		System.out.println("最终通知方法");
	}

SomeServiceImpl实现ISomeService接口:

        @Override
	public void doFirst() {
		System.out.println("执行主业务逻辑doFirst()");
	}
	@Override
	public String doSecond() {
		System.out.println("执行主业务逻辑doSecond()");
		return "abcde";
	}
	@Override
	public void doThird() {
		//System.out.println("执行主业务逻辑doThird()");
		System.out.println("执行主业务逻辑doThird() 。。。" + 3/0);
	}

applicationContext.xml配置文件:

	<!-- 注册目标对象 -->
   	<bean id="someService" class="com.aop_xml.SomeServiceImpl"></bean>
   	<!-- 注册切面 -->
	<bean id="MyAspect" class="com.aop_xml.MyAspect"></bean>
	<!-- aop配置-->
	<aop:config>
		<aop:pointcut expression="execution(* *..ISomeService.doFirst(..))" id="doFirstPointcut"/>
		<aop:pointcut expression="execution(* *..ISomeService.doSecond(..))" id="doSecondPointcut"/>
		<aop:pointcut expression="execution(* *..ISomeService.doThird(..))" id="doThirdPointcut"/>
		<aop:aspect ref="MyAspect">
			<!--前置通知  -->
			<aop:before method="before" pointcut-ref="doFirstPointcut"/>
			<aop:before method="before(org.aspectj.lang.JoinPoint)" pointcut-ref="doFirstPointcut"/>
			<!--后置通知  -->
			<aop:after-returning method="afterReturning" pointcut-ref="doSecondPointcut"/>
			<aop:after-returning method="afterReturning(java.lang.String)" pointcut-ref="doSecondPointcut" returning="result"/>
			<!-- 环绕通知 -->
			<aop:around method="around" pointcut-ref="doSecondPointcut"/>
			<!-- 异常通知 -->
			<aop:after-throwing method="AfterThrowing" pointcut-ref="doThirdPointcut" throwing="ex"/>
			<!-- 最终通知 -->
			<aop:after method="after" pointcut-ref="doThirdPointcut"/>
		</aop:aspect>
	</aop:config>	

测试类:

    @Test
	public void test01() {
		//加载Spring配置文件,创建Spring容器对象
		String resource = "com/aop_xml/applicationContext.xml";
		ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(resource);
		//从容器中获取指定Bean对象
		ISomeService service = (ISomeService) ac.getBean("someService");
		service.doFirst();
		System.out.println("-------------------");
		String result = service.doSecond();
		System.out.println(result);
		System.out.println("-------------------");
		service.doThird();
	}

 

3.通知基于注解的实现

ISomeService与其实现类SomeServiceImpl和测试类与基于xml的一样,这里就不再写了。

自定义切面类:

@Aspect  //表示当前类为切面
public class MyAspect {
	//前置通知
	@Before("execution(* *..ISomeService.doFirst(..))")
	public void before() {
		System.out.println("前置通知方法");
	}
	//后置通知 :改变不了结果值
	@AfterReturning("execution(* *..ISomeService.doSecond(..))")
	public void afterReturning() {
		System.out.println("后置通知方法");
	}              
	//环绕通知
	@Around("execution(* *..ISomeService.doSecond(..))")
	public Object around(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("环绕通知方法,目标方法执行之前");
		//执行目标方法
		Object result = pjp.proceed();
		System.out.println("环绕通知方法,目标方法执行之后");	
		return ((String)result).toUpperCase();
	}  
	//异常通知
	@AfterThrowing(value="doThirdPointcut()",throwing="ex")
	public void AfterThrowing(Exception ex) {
		System.out.println("异常通知方法  ex = " + ex.getMessage());
	}
	
	//最终通知:无论是否抛出异常都会执行
	@After("doThirdPointcut()")
	public void after() {
		System.out.println("最终通知方法");
	}
	
	//定义切入点
	@Pointcut("execution(* *..ISomeService.doThird(..))")
	private void  doThirdPointcut() {}

applicationContext.xml配置文件:

	<!-- 注册目标对象 -->
   	<bean id="someService" class="com.aop_annotation.SomeServiceImpl"></bean>
   	<!-- 注册切面 -->
	<bean id="myAdvice" class="com.aop_annotation.MyAspect"></bean>
	<!-- 生成代理对象-->
	<aop:aspectj-autoproxy/>

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RWTHeart

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值