spring aop参数传递

spring aop参数传递,第1张


spring aop参数传递

        

              

                             

使用示例

        

                          

            

HelloService

public interface HelloService {

    String hello();
    String hello(String name);
    String hello(String name, Integer age);
}

       

HelloServiceImpl

@Service
public class HelloServiceImpl implements HelloService {

    @Override
    public String hello() {
        return "hello";
    }

    @Override
    public String hello(String name) {
        return "hello " + name;
    }

    @Override
    public String hello(String name, Integer age) {
        return "hello " + name + " " + age;
    }
}

         

CustomAspect

@Aspect
@Component
public class CustomAspect {

    @Pointcut("execution(* *.hello(..))")
    public void fun(){

    }

    @Pointcut("execution(* *.hello(String)) && args(name))")
    public void fun2(String name){

    }
    @Pointcut("execution(* *.hello(String,..)) && args(name))")
    public void fun3(String name){

    }

    @Pointcut("execution(* *.hello(String,Integer)) && args(name,age)")
    public void fun4(String name, Integer age){

    }

    @Before("fun()")
    public void before(JoinPoint joinPoint){
        System.out.print("before ==> ");
        process(joinPoint);
    }

    @Before("fun2(name)")
    public void before2(JoinPoint joinPoint,String name){
        System.out.print("before2 ==> " + name + " ==> ");
        process(joinPoint);
    }

    @Before("fun3(name)")
    public void after3(JoinPoint joinPoint,String name){
        System.out.print("after3 ==> "+ name + " ==> ");
        process(joinPoint);
    }

    @After("fun4(name,age)")
    public void after4(JoinPoint joinPoint, String name, Integer age){
        System.out.print("after4 ==> "+ name + " " +age + " ==> ");
        process(joinPoint);
    }

    public void process(JoinPoint joinPoint){
        MethodSignature signature =(MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println(method.getDeclaringClass().getName()+"."+method.getName()+":被调用");
    }
}

           

 HelloController

@RestController
public class HelloController {

    @Resource
    private HelloService helloService;

    @RequestMapping("/hello")
    public String hello(){
        return helloService.hello();
    }

    @RequestMapping("/hello2")
    public String hello2(){
        return helloService.hello("瓜田李下");
    }

    @RequestMapping("/hello3")
    public String hello3(){
        return helloService.hello("瓜田李下",20);
    }
}

        

            

                             

使用示例

      

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

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

        

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

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

       

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

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

        

               

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/733115.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存