Spring学习笔记(16)----使用Spring配置文件实现AOP

15 篇文章 0 订阅
5 篇文章 0 订阅

前面介绍了使用注解的方式,下面介绍使用配置文件的方式实现AOP。

使用配置方式,Interceptor类中不包含任何注解。

package com.szy.spring;

import org.aspectj.lang.ProceedingJoinPoint;

public class Interceptor
{
	public void doBefore()
	{
		System.out.println("----------------执行前置通知-----------------");
	}
	
	public void doAfterReturning()
	{
		System.out.println("----------------执行后置通知-----------------");
	}
	
	public void doAfter()
	{
		System.out.println("----------------执行最终通知-----------------");
	}
	
	public void doAfterThrowing()
	{
		System.out.println("----------------执行意外通知-----------------");
	}
	
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable
	{
		System.out.println("----------------进入判断方法-----------------");
		Object result=pjp.proceed();  //该方法必须被执行
		System.out.println("----------------退出判断方法-----------------");
		return result;
	}
}

 紧着这我们在配置文件中配置切面、切入点、通知等:

<bean id="aspetbean" class="com.szy.spring.Interceptor"/>
	<aop:config>
		<aop:aspect id="aspet" ref="aspetbean">
			<aop:pointcut id="cut" expression="execution (* com.szy.spring.UserManagerImpl.*(..))"/>
			<aop:before pointcut-ref="cut" method="doBefore"/>
			<aop:after-returning pointcut-ref="cut" method="doAfterReturning"/>
			<aop:after pointcut-ref="cut" method="doAfter"/>
			<aop:after-throwing pointcut-ref="cut" method="doAfterThrowing"/>
			<aop:around pointcut-ref="cut" method="doAround"/>
		</aop:aspect>
	</aop:config>

 运行测试代码输入正常结果。

在实际开发中AOP一般用于权限设置等。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值