• 【JavaSpring】Aop的通知类型,获取数据


    AOP 通知描述了抽取的共性功能,根据共性功能抽取的位置不同,最终运行代码时要将其加入到合理的位置

    前置通知

    1. @Pointcut("execution(void org.example.dao.BookDao.update())")
    2. private void pt() {
    3. }
    4. @Before("pt()")
    5. public void before() {
    6. System.out.println("before advice");
    7. }

    后置通知

    1. @Pointcut("execution(void org.example.dao.BookDao.update())")
    2. private void pt() {
    3. }
    4. @After("pt()")
    5. public void after() {
    6. System.out.println("after advice");
    7. }

    环绕通知(获取异常和返回值)

    无对原始操作的调用

    显然原始方法不执行了

    1. @Pointcut("execution(void org.example.dao.BookDao.update())")
    2. private void pt() {
    3. }
    4. @Around("pt()")
    5. public void around() {
    6. System.out.println("around before advice");
    7. System.out.println("around after advice");
    8. }

     

    加上对原始操作的调用

    无返回值

    1. @Pointcut("execution(void org.example.dao.BookDao.update())")
    2. private void pt() {
    3. }
    4. @Around("pt()")
    5. public void around(ProceedingJoinPoint pjp) throws Throwable {
    6. System.out.println("around before advice");
    7. pjp.proceed();
    8. System.out.println("around after advice");
    9. }

    如果有返回值

    1. @Around("pt()")
    2. public Object around(ProceedingJoinPoint pjp) throws Throwable {
    3. System.out.println("around before advice");
    4. Object obj=pjp.proceed();
    5. System.out.println("around after advice");
    6. return obj;
    7. }

    返回后通知(获取返回值)

    成功运行后执行

    1. @Pointcut("execution(void org.example.dao.BookDao.update())")
    2. private void pt() {
    3. }
    4. @AfterReturning("pt()")
    5. public void afterReturning() {
    6. System.out.println("afterReturning advice");
    7. }

    和后置通知的区别

    如果抛出异常,AfterReturning不运行,但After正常运行 

    抛出异常后通知(获取异常)

    1. @Pointcut("execution(void org.example.dao.BookDao.update())")
    2. private void pt() {
    3. }
    4. @AfterThrowing("pt()")
    5. public void afterThrowing() {
    6. System.out.println("afterThrowing advice");
    7. }

    测试(通知类型,如果想看看效果,直接复制) 

    1. package org.example.dao.impl;
    2. import org.example.dao.BookDao;
    3. import org.springframework.stereotype.Repository;
    4. @Repository
    5. public class BookDaoImpl implements BookDao {
    6. public void save() {
    7. System.out.println("book dao save");
    8. }
    9. public void update() {
    10. System.out.println("book dao update");
    11. }
    12. public void delete() {
    13. System.out.println("book dao delete");
    14. }
    15. public int select() {
    16. System.out.println("book dao select");
    17. return 100;
    18. }
    19. }
    1. package org.example.config;
    2. import org.springframework.context.annotation.ComponentScan;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.context.annotation.EnableAspectJAutoProxy;
    5. @Configuration
    6. @ComponentScan("org.example")
    7. @EnableAspectJAutoProxy//有用注解开发的AOP
    8. public class SpringConfig {
    9. }
    1. package org.example.aop;
    2. import org.aspectj.lang.ProceedingJoinPoint;
    3. import org.aspectj.lang.annotation.*;
    4. import org.springframework.stereotype.Component;
    5. @Component
    6. @Aspect//当作Aop处理
    7. public class MyAdvice {
    8. @Pointcut("execution(void org.example.dao.BookDao.update())")
    9. private void pt() {
    10. }
    11. @Before("pt()")
    12. public void before() {
    13. System.out.println("before advice");
    14. }
    15. @After("pt()")
    16. public void after() {
    17. System.out.println("after advice");
    18. }
    19. @Around("pt()")
    20. public Object around(ProceedingJoinPoint pjp) throws Throwable {
    21. System.out.println("around before advice");
    22. Object obj = pjp.proceed();
    23. System.out.println("around after advice");
    24. return obj;
    25. }
    26. @AfterReturning("pt()")
    27. public void afterReturning() {
    28. System.out.println("afterReturning advice");
    29. }
    30. @AfterThrowing("pt()")
    31. public void afterThrowing() {
    32. System.out.println("afterThrowing advice");
    33. }
    34. }
    1. package org.example.dao;
    2. public interface BookDao {
    3. void save();
    4. void update();
    5. int select();
    6. }
    1. package org.example;
    2. import org.example.config.SpringConfig;
    3. import org.example.dao.BookDao;
    4. import org.springframework.context.ApplicationContext;
    5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    6. /**
    7. * Hello world!
    8. *
    9. */
    10. public class App
    11. {
    12. public static void main( String[] args )
    13. {
    14. ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
    15. BookDao bookDao=ctx.getBean(BookDao.class);
    16. bookDao.update();
    17. }
    18. }

    测试(获取参数,JoinPoint必须是第一位)

    1. package org.example.aop;
    2. import org.aspectj.lang.JoinPoint;
    3. import org.aspectj.lang.ProceedingJoinPoint;
    4. import org.aspectj.lang.annotation.*;
    5. import org.springframework.stereotype.Component;
    6. import java.util.Arrays;
    7. @Component
    8. @Aspect//当作Aop处理
    9. public class MyAdvice {
    10. @Pointcut("execution(* org.example.dao.BookDao.findName(..))")
    11. private void pt() {
    12. }
    13. @Before("pt()")
    14. public void before(JoinPoint jp) {
    15. Object[] args = jp.getArgs();
    16. System.out.println("before advice:" + Arrays.toString(args));
    17. }
    18. @After("pt()")
    19. public void after(JoinPoint jp) {
    20. Object[] args = jp.getArgs();
    21. System.out.println("after advice:" + Arrays.toString(args));
    22. }
    23. @Around("pt()")
    24. public Object around(ProceedingJoinPoint pjp) throws Throwable {
    25. System.out.println("around before advice");
    26. Object obj = pjp.proceed();
    27. Object[] args = pjp.getArgs();
    28. System.out.println("around after advice:" + Arrays.toString(args));
    29. return obj;
    30. }
    31. @AfterReturning(value = "pt()", returning = "res")
    32. public void afterReturning(Object res) {
    33. System.out.println("afterReturning advice:" + res);
    34. }
    35. @AfterThrowing(value = "pt()", throwing = "t")
    36. public void afterThrowing(Throwable t) {
    37. System.out.println("afterThrowing advice"+t);
    38. }
    39. }
    1. package org.example.dao;
    2. public interface BookDao {
    3. String findName(int id);
    4. }

  • 相关阅读:
    android开发获取View坐标位置的几种方式
    基于PHP+MySQL家庭医生签约预约诊疗管理信息系统
    java入门,记一次微服务间feigin请求的问题
    Camunda 创建 流程图 (二)
    哈希表题目:键盘行
    15-1 Java反射机制概述
    HashTable读为什么要加锁
    HCIP实验(08)
    介绍下Java内存区域(运行时数据区)
    MySQL性能指标TPS\QPS\IOPS如何压测?
  • 原文地址:https://blog.csdn.net/David_Hzy/article/details/132954043