AOP案例(注解形式)

配置文件

<!-- 1、开启AOP自动代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

业务层代码

package com.zhwl.register.service.impl;
@Service
public class RegisterServiceImpl implements RegisterService {
    @Override
    public void fun() {
		
    }
}

AOP类

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
@Component
//这是多例模式注解。
//@Scope("prototype")
@Aspect
public class AopTx {
	//异常通知
    //execution表达式参考:https://blog.csdn.net/qq_39706570/article/details/104131914
//这里的execution只对fun方法拦截
	@AfterThrowing("execution(* com.zhwl.register.service.impl.RegisterServiceImpl.fun(..))")
	public void afterThrowing() {
		
	}
	//环绕通知
    //execution表达式参考:https://blog.csdn.net/qq_39706570/article/details/104131914
//这里的execution只对fun方法拦截
	@Around("execution(* com.zhwl.register.service.impl.RegisterServiceImpl.fun(..))")
	public void around(ProceedingJoinPoint pjp) throws Throwable {
		//方法之前
		
		pjp.proceed();
		
		//方法之后
		
	}
}

备注

 execution表达式参考:https://blog.csdn.net/qq_39706570/article/details/104131914

发布了124 篇原创文章 · 获赞 119 · 访问量 51万+

猜你喜欢

转载自blog.csdn.net/qq_39706570/article/details/104145259
今日推荐