spring aop使用示例


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() {
        System.out.println("HelloService hello()方法被调用");

        return "hello";
    }

    @Override
    public String hello(String name) {
        System.out.println("HelloService hello(name)方法被调用");

        return "hello" + name;
    }

    @Override
    public String hello(String name, Integer age) {
        System.out.println("HelloService hello(name,age)方法被调用");

        return "hello" + name +" "+age;
    }
}

          

HelloService2

public interface HelloService2 {

    String hello();
}

        

HelloService2Impl

@Service
public class HelloService2Impl implements HelloService2 {

    @Override
    public String hello() {
        System.out.println("HelloService2 hello2被调用");

        return "hello2";
    }
}

         

CustomAspect

@Aspect
@Component
public class CustomAspect {

    @Pointcut("execution(* *.hello())")   //匹配名为hello的方法,方法无参数
    public void fun(){

    }

    @Pointcut("execution(* *.hello*())")  //匹配前缀为hello的方法,方法无参数
    public void fun2(){

    }

    @Pointcut("execution(* *.hello*(..))") //匹配前缀为hello的方法,方法为任意参数
    public void fun3(){

    }

    @Pointcut("this(com.example.demo.service.HelloService)")   //当前调用对象(代理对象)为HelloService或者其子类
    @Pointcut("this(com.example.demo.service.HelloService+)")  //两者等效
    public void fun4(){

    }

    @Pointcut("target(com.example.demo.service.HelloService2)") //当前调用对象代理的目标对象为HelloService2或者其子类
    public void fun5(){

    }

    @Pointcut("within(com.example.demo.service..*)")  //当前调用对象为com.example.demo.service包及子包中的任意类
    public void fun6(){

    }

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

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

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

    @After("fun4()")
    public void after4(JoinPoint joinPoint){
        System.out.print("after4 ==> ");
        process(joinPoint);
    }

    @After("fun5()")
    public void after5(JoinPoint joinPoint){
        System.out.print("after5 ==> ");
        process(joinPoint);
    }

    @After("fun6()")
    public void after6(JoinPoint joinPoint){
        System.out.print("after6 ==> ");
        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;

    @Resource
    private HelloService2 helloService2;

    @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);
    }

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

       

             

                                

使用测试

       

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

before ==> com.example.demo.controller.HelloController.hello:被调用
before2 ==> com.example.demo.controller.HelloController.hello:被调用
before3 ==> com.example.demo.controller.HelloController.hello:被调用
before ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用
before2 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用
before3 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用
HelloService hello()方法被调用
after6 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用
after4 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用

       

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

before2 ==> com.example.demo.controller.HelloController.hello2:被调用
before3 ==> com.example.demo.controller.HelloController.hello2:被调用
before3 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用
HelloService hello(name)方法被调用
after6 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用
after4 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用

     

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

before2 ==> com.example.demo.controller.HelloController.hello3:被调用
before3 ==> com.example.demo.controller.HelloController.hello3:被调用
before3 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用
HelloService hello(name,age)方法被调用
after6 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用
after4 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用

       

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

before2 ==> com.example.demo.controller.HelloController.hello4:被调用
before3 ==> com.example.demo.controller.HelloController.hello4:被调用
before ==> com.example.demo.service.impl.HelloService2Impl.hello:被调用
before2 ==> com.example.demo.service.impl.HelloService2Impl.hello:被调用
before3 ==> com.example.demo.service.impl.HelloService2Impl.hello:被调用
HelloService2 hello2被调用
after6 ==> com.example.demo.service.impl.HelloService2Impl.hello:被调用
after5 ==> com.example.demo.service.impl.HelloService2Impl.hello:被调用

     

             

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值