Aop实现接口方式

  • 前置通知(Before advice):在某连接点之前执行的通知,但这个通知不能阻止连接点之前的执行流程(除非它抛出一个异常)。

  • 后置通知(After returning advice):在某连接点正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。

  • 异常通知(After throwing advice):在方法抛出异常退出时执行的通知。

  • 最终通知(After (finally) advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。

  • 环绕通知(Around Advice):包围一个连接点的通知,如方法调用。这是最强大的一种通知类型。环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它自己的返回值或抛出异常来结束执行。

1、环绕通知:

public class LogInterceptor implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation arg0) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("aop begin");
		Object obj = arg0.proceed();
		System.out.println("aop end");
		return obj;
	}
}


2、前置通知:

public class BeforInterceptor implements MethodBeforeAdvice {

	@Override
	public void before(Method arg0, Object[] arg1, Object arg2)
			throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("BeforInterceptor");
		System.out.println("切入点对象:"+arg2+"  参数:"+arg1[0]+"  切入点:"+arg0.getName());
	}

}

3、 后置通知:

public class AfterInterceptor implements AfterReturningAdvice {
	@Override
	public void afterReturning(Object arg0, Method arg1, Object[] arg2,
			Object arg3) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println(arg0+"  "+arg3);
	}
}
4、 异常通知:

ThrowsAdvice是一个标示接口,我们可以在类中定义一个或多个,来捕获定义异常通知的bean抛出的异常,并在抛出异常前执行相应的方法

public class ExceptionInterceptor implements ThrowsAdvice{
	public void afterThrowing(Exception e){
		System.out.println(e);
		System.out.println("ExceptionInterceptor Exception");
	}
	public void afterThrowing(NullPointerException e){
		System.out.println(e);
		System.out.println("ExceptionInterceptor  NullPointerException");
	}
}
异常通知被拦截后就不会执行其它的方法了,比如拦截到了NullPointerException就不会拦截Exception

xml:

	<aop:config>
		<aop:pointcut expression="execution(* org.han.service.*.*(..))"
			id="logpoint" />
		<!-- 环绕通知 -->
		 <aop:advisor advice-ref="loginter1" pointcut-ref="logpoint"/> 

		<!-- 前置通知 -->
		 <aop:advisor advice-ref="loginter2" pointcut-ref="logpoint"/> 

		<!-- 后置通知 -->
		 <aop:advisor advice-ref="loginter3" pointcut-ref="logpoint"/>
		 
		<!-- 异常通知-->
		<aop:advisor advice-ref="loginter4" pointcut-ref="logpoint"/>
	</aop:config>

services:

public class LoginService {
	public void login(){
		System.out.println("yyy正在登录");
		User u=null;
		System.out.println(u.getUname());
		System.out.println("xxx正在登录");
	}
	public User register(){
		System.out.println("注册成功");
		return new User("han","zhou");
	}
	public User getUser(String uname,String pwd){
		System.out.println("getUser");
		return new User(uname,pwd);
	}
	public void login(String uname){
		System.out.println(uname+"正在登录");
	}
}






  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP可以使用AspectJ注解来实现接口重复调用的拦截。假设我们有一个接口`UserService`,其中有一个`saveUser`方法,我们希望在该方法调用时进行拦截,防止重复调用。我们可以定义一个切面`DuplicateInvokeAspect`,并为其添加一个`@Around`注解的方法,该方法将拦截`UserService`的`saveUser`方法,并在该方法中添加重复调用判断逻辑。 ``` @Aspect public class DuplicateInvokeAspect { private Map<String, Long> lastInvokeMap = new ConcurrentHashMap<>(); @Around("execution(* com.example.UserService.saveUser(..))") public Object checkDuplicateInvoke(ProceedingJoinPoint joinPoint) throws Throwable { String methodName = joinPoint.getSignature().getName(); if (lastInvokeMap.containsKey(methodName)) { long lastInvokeTime = lastInvokeMap.get(methodName); long currentTime = System.currentTimeMillis(); if (currentTime - lastInvokeTime < 5000) { // 限制5秒内不能重复调用 throw new RuntimeException("不能重复调用" + methodName); } } lastInvokeMap.put(methodName, System.currentTimeMillis()); return joinPoint.proceed(); } } ``` 在上述代码中,我们使用`ConcurrentHashMap`来存储方法名和上一次调用时间的映射关系。在`checkDuplicateInvoke`方法中,我们首先判断该方法是否已经在`lastInvokeMap`中存在,如果存在则判断距离上一次调用的时间是否已经超过了5秒,如果没有超过则抛出异常,否则将当前时间更新为上一次调用时间,并继续执行原有的方法逻辑。 最后,我们需要在Spring配置文件中启用该切面: ``` <aop:aspectj-autoproxy/> <bean id="duplicateInvokeAspect" class="com.example.DuplicateInvokeAspect"/> ``` 这样就完成了接口重复调用的拦截功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值