AOP
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("环绕通知开始");
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("最终通知");
}
}