Spring Aop使用demo(Spring Boot)

╰半夏微凉° 2022-04-15 00:53 313阅读 0赞

pom.xml:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-aop</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-test</artifactId>
  13. <scope>test</scope>
  14. </dependency>
  15. </dependencies>

创建User类:

  1. package com.example.demo.dao;
  2. /*
  3. * @author zzf
  4. * @date 2018年11月19日 下午5:19:28
  5. */
  6. public class User {
  7. String name;
  8. Integer age;
  9. public String getName() {
  10. return name;
  11. }
  12. public void setName(String name) {
  13. this.name = name;
  14. }
  15. public Integer getAge() {
  16. return age;
  17. }
  18. public void setAge(Integer age) {
  19. this.age = age;
  20. }
  21. public User(String name, Integer age) {
  22. super();
  23. this.name = name;
  24. this.age = age;
  25. }
  26. @Override
  27. public String toString() {
  28. return "User [name=" + name + ", age=" + age + "]";
  29. }
  30. }

创建接口:

  1. package com.example.demo.concert;
  2. import com.example.demo.dao.User;
  3. /*
  4. * @author zzf
  5. * @date 2018年11月19日 上午11:09:02
  6. */
  7. public interface Performance {
  8. public void perform1();
  9. public void perform2() throws Exception;
  10. public void perform3(int age,String name);
  11. public void perform4(User user);
  12. }

实现类:

  1. package com.example.demo.concert.impl;
  2. import org.springframework.stereotype.Component;
  3. import com.example.demo.concert.Performance;
  4. import com.example.demo.dao.User;
  5. /*
  6. * @author zzf
  7. * @date 2018年11月19日 下午1:33:49
  8. */
  9. @Component
  10. public class PerformanceImpl implements Performance{
  11. @Override
  12. public void perform1() {
  13. // TODO Auto-generated method stub
  14. System.out.println("方法运行1");
  15. }
  16. @Override
  17. public void perform2() throws Exception {
  18. // TODO Auto-generated method stub
  19. // TODO Auto-generated method stub
  20. System.out.println("方法运行2");
  21. throw new Exception("抛出异常");
  22. }
  23. @Override
  24. public void perform3(int age,String name) {
  25. // TODO Auto-generated method stub
  26. System.out.println("方法运行-age:"+age+"-name:"+name);
  27. }
  28. @Override
  29. public void perform4(User user) {
  30. // TODO Auto-generated method stub
  31. System.out.println(user);
  32. }
  33. }

切面类:

  1. package com.example.demo;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.After;
  4. import org.aspectj.lang.annotation.AfterReturning;
  5. import org.aspectj.lang.annotation.AfterThrowing;
  6. import org.aspectj.lang.annotation.Around;
  7. import org.aspectj.lang.annotation.Aspect;
  8. import org.aspectj.lang.annotation.Before;
  9. import org.aspectj.lang.annotation.Pointcut;
  10. import org.springframework.stereotype.Component;
  11. import com.example.demo.dao.User;
  12. /*
  13. * @author zzf
  14. * @date 2018年11月19日 下午1:14:47
  15. */
  16. @Aspect
  17. @Component
  18. public class Audience {
  19. //定义切点
  20. @Pointcut("execution(** com.example.demo.concert.Performance.*(..))")//所有的方法任意参数
  21. public void performance() {}
  22. //传递单个值
  23. @Pointcut("execution(** com.example.demo.concert.Performance.*(int,String)) && args(age,name)")
  24. public void performance2(int age,String name) {}
  25. //传递对象
  26. @Pointcut("execution(** com.example.demo.concert.Performance.*(com.example.demo.dao.User)) && args(user)")
  27. public void performance3(User user) {}
  28. @Before("performance()")
  29. public void beforePerformance1() {
  30. System.out.println("performance-方法开始前1");
  31. }
  32. @Before("performance()")
  33. public void beforePerformance2() {
  34. System.out.println("performance-方法开始前2");
  35. }
  36. @AfterReturning("performance()")
  37. public void AfterPerformance() {
  38. System.out.println("performance-方法正常退出后");
  39. }
  40. @AfterThrowing("performance()")
  41. public void AfterTrownPerformance() {
  42. System.out.println("performance-方法异常退出");
  43. }
  44. @Around("performance()")
  45. public void AroudPerformance(ProceedingJoinPoint jp) throws Throwable {
  46. System.out.println("performance-环绕通知方法前");
  47. jp.proceed();
  48. System.out.println("performance-环绕通知方法后");
  49. }
  50. /***************************************************************/
  51. @Before("performance2(age,name)")
  52. public void beforePerformance1_2(int age,String name) {
  53. System.out.println("performance2-方法开始前1-age:"+age+"-name:"+name);
  54. }
  55. @Before("performance2(age,name)")
  56. public void beforePerformance2_2(int age,String name) {
  57. System.out.println("performance2-方法开始前2-age:"+age+"-name:"+name);
  58. }
  59. @AfterReturning("performance2(age,name)")
  60. public void AfterPerformance2(int age,String name) {
  61. System.out.println("performance2-方法正常退出后-age:"+age+"-name:"+name);
  62. }
  63. @AfterThrowing("performance2(age,name)")
  64. public void AfterTrownPerformance2(int age,String name) {
  65. System.out.println("performance2-方法异常退出-age:"+age+"-name:"+name);
  66. }
  67. @Around("performance2(age,name)")
  68. public void AroudPerformance2(ProceedingJoinPoint jp,int age,String name) throws Throwable {
  69. System.out.println("performance2-环绕通知方法前-age:"+age+"-name:"+name);
  70. jp.proceed();
  71. System.out.println("performance2-环绕通知方法后-age:"+age+"-name:"+name);
  72. }
  73. /**********************************************************************/
  74. @Around("performance3(user)")
  75. public void AroudPerformance3(ProceedingJoinPoint jp,User user) throws Throwable {
  76. System.out.println("performance3-环绕通知方法前-User:"+user);
  77. jp.proceed();
  78. System.out.println("performance3-环绕通知方法后-User:"+user);
  79. }
  80. }

控制器:

  1. package com.example.demo.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.validation.annotation.Validated;
  5. import org.springframework.web.bind.annotation.Mapping;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import com.example.demo.concert.Performance;
  11. import com.example.demo.dao.User;
  12. import com.fasterxml.jackson.databind.ser.std.StdKeySerializers.Default;
  13. /*
  14. * @author zzf
  15. * @date 2018年11月19日 下午1:38:58
  16. */
  17. @RestController
  18. public class TestController {
  19. @Autowired
  20. Performance performance;
  21. @RequestMapping(value = "/test1")
  22. public void run1() throws Exception {
  23. performance.perform1();
  24. }
  25. @RequestMapping(value = "/test2")
  26. public void run2() throws Exception {
  27. performance.perform2();
  28. }
  29. @RequestMapping(value = "/test3")
  30. public void run3(int age,String name) throws Exception {
  31. performance.perform3(age,name);
  32. }
  33. @RequestMapping(value = "/test4")
  34. public void run4(User a) throws Exception {
  35. performance.perform4(a);
  36. }
  37. }

目录结构:

20181119180245917.png

运行:

http://localhost:8888/test4?age=9&name=aaa

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3NTk4MDEx_size_16_color_FFFFFF_t_70

http://localhost:8888/test3?age=99&name=嗷嗷嗷

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3NTk4MDEx_size_16_color_FFFFFF_t_70 1

http://localhost:8888/test2

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3NTk4MDEx_size_16_color_FFFFFF_t_70 2

http://localhost:8888/test1

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3NTk4MDEx_size_16_color_FFFFFF_t_70 3

发表评论

表情:
评论列表 (有 0 条评论,313人围观)

还没有评论,来说两句吧...

相关阅读

    相关 spring boot aop

    AOP一些理论和spring boot环境搭建免去,直接aop实现(LZ也是自己跟着官网学spring boot,原理概念性的东西,也是看官网和别人的分享,这里不敢说多了,会出