AOP的使用

    技术2022-07-10  135

    package com.whc.noteserver.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.noteserver.entity.NoteBook; import com.whc.noteserver.param.NoteBookParam; import com.whc.noteserver.service.NoteBookService; @Aspect @Component public class NoteBookAOP { @Autowired private NoteBookService noteBookService; @Pointcut("execution (* com.whc.noteserver.service.NoteBookService.addNoteBook(..))") private void pointCut() { } @Before("pointCut()") public void before() { System.out.println("before add"); } // @AfterReturning("pointCut()") // public void afterReturning() { // System.out.println("afterreturning add"); // } @Around("pointCut()") public Object around(ProceedingJoinPoint proceedingJoinPoint) { Object obj=null; System.out.println("around add before"); Object[] args=proceedingJoinPoint.getArgs(); NoteBookParam noteBookParam=new NoteBookParam(); noteBookParam.setUserid(((NoteBook)args[0]).getUserid()); int re=noteBookService.getCount(noteBookParam); if(re<5) { try { obj=proceedingJoinPoint.proceed(); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ System.out.println("around add after return -1"); return -1; } System.out.println("around add after"); return obj; } /* * @AfterThrowing("pointCut()") public void afterThrowing(JoinPoint joinpoint, * Throwable e) { System.out.println("error:"+e.getMessage()); * * } */ /* * @After("pointCut()") public void after() { System.out.println("after"); } */ }
    Processed: 0.015, SQL: 9