当前位置:   article > 正文

spring aop参数传递_spring aop 参数传递

spring aop 参数传递

spring aop参数传递

        

              

                             

使用示例

        

                          

            

HelloService

  1. public interface HelloService {
  2. String hello();
  3. String hello(String name);
  4. String hello(String name, Integer age);
  5. }

       

HelloServiceImpl

  1. @Service
  2. public class HelloServiceImpl implements HelloService {
  3. @Override
  4. public String hello() {
  5. return "hello";
  6. }
  7. @Override
  8. public String hello(String name) {
  9. return "hello " + name;
  10. }
  11. @Override
  12. public String hello(String name, Integer age) {
  13. return "hello " + name + " " + age;
  14. }
  15. }

         

CustomAspect

  1. @Aspect
  2. @Component
  3. public class CustomAspect {
  4. @Pointcut("execution(* *.hello(..))")
  5. public void fun(){
  6. }
  7. @Pointcut("execution(* *.hello(String)) && args(name))")
  8. public void fun2(String name){
  9. }
  10. @Pointcut("execution(* *.hello(String,..)) && args(name))")
  11. public void fun3(String name){
  12. }
  13. @Pointcut("execution(* *.hello(String,Integer)) && args(name,age)")
  14. public void fun4(String name, Integer age){
  15. }
  16. @Before("fun()")
  17. public void before(JoinPoint joinPoint){
  18. System.out.print("before ==> ");
  19. process(joinPoint);
  20. }
  21. @Before("fun2(name)")
  22. public void before2(JoinPoint joinPoint,String name){
  23. System.out.print("before2 ==> " + name + " ==> ");
  24. process(joinPoint);
  25. }
  26. @Before("fun3(name)")
  27. public void after3(JoinPoint joinPoint,String name){
  28. System.out.print("after3 ==> "+ name + " ==> ");
  29. process(joinPoint);
  30. }
  31. @After("fun4(name,age)")
  32. public void after4(JoinPoint joinPoint, String name, Integer age){
  33. System.out.print("after4 ==> "+ name + " " +age + " ==> ");
  34. process(joinPoint);
  35. }
  36. public void process(JoinPoint joinPoint){
  37. MethodSignature signature =(MethodSignature) joinPoint.getSignature();
  38. Method method = signature.getMethod();
  39. System.out.println(method.getDeclaringClass().getName()+"."+method.getName()+":被调用");
  40. }
  41. }

           

 HelloController

  1. @RestController
  2. public class HelloController {
  3. @Resource
  4. private HelloService helloService;
  5. @RequestMapping("/hello")
  6. public String hello(){
  7. return helloService.hello();
  8. }
  9. @RequestMapping("/hello2")
  10. public String hello2(){
  11. return helloService.hello("瓜田李下");
  12. }
  13. @RequestMapping("/hello3")
  14. public String hello3(){
  15. return helloService.hello("瓜田李下",20);
  16. }
  17. }

        

            

                             

使用示例

      

localhost:8080/hello,控制台输出:

  1. before ==> com.example.demo.controller.HelloController.hello:被调用
  2. before ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用

        

localhost:8080/hello2,控制台输出:

  1. after3 ==> 瓜田李下 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用
  2. before ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用
  3. before2 ==> 瓜田李下 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用

       

localhost:8080/hello3,控制台输出:

  1. before ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用
  2. after4 ==> 瓜田李下 20 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用

        

               

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/647732
推荐阅读
相关标签
  

闽ICP备14008679号