package com.whc.wznoteserver2.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.whc.wznoteserver2.entity.NoteBook;
import com.whc.wznoteserver2.param.NoteBookParam;
import com.whc.wznoteserver2.service.NoteBookService;
@Aspect
@Component
public class NoteBookCountAOP {
@Autowired
NoteBookService noteBookService;
@Pointcut("execution(* com.whc.wznoteserver2.service.NoteBookService.addNoteBook(..))")
private void pointCut() {
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) {
System.out.println("环绕通知开始");
//1、从数据库取出用户信息,判断该用户是否缴费用户
//是缴费用户,可以添加
//不是缴费用户,从数据库查询,该用户已经添加了多少个笔记本
//如果多于5个,不允许添加
//小于5个,允许添加
Object[] args=proceedingJoinPoint.getArgs();
NoteBookParam noteBookParam= new NoteBookParam();
noteBookParam.setUserid(((NoteBook)args[0]).getUserid());
int count=noteBookService.getCount(noteBookParam);
Object obj=null;
if(count<5) {
try {
obj=proceedingJoinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
return obj;
}else {
return -1;
}
}
@AfterThrowing(value="pointCut()",throwing="e")
public void countException(JoinPoint joinPoint,Throwable e) {
System.out.println("异常:"+e.getMessage());
}
@After("pointCut()")
public void fina(JoinPoint joinPoint) {
System.out.println("最终通知");
}
}
Java_学习笔记(AOP)
最新推荐文章于 2024-07-14 11:47:33 发布