spring aop 注解版代码_无分析

 

对HiService的getHi方法增强:

@Component
public class HiService {
    public void getHi() {
        System.out.println("hiService");
    }
}

 

package com.springmvc.service;


import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component("myAdvice")
@Aspect
public class MyAdvice {
    @Before(value = "execution(* com.springmvc.service.HiService.getHi(..))")
    public void startLog() {
        System.out.println("start log");
    }

    @After(value = "execution(* com.*.service.HiService.getHi(..))")
    public void stopLog() {
        System.out.println("end log");
    }
}

execution的返回值和包都可以用*通配符, 参数可以用 两个点

spring.xml:

AOP基于注解的配置,开启自动代理:

<aop:aspectj-autoproxy/>

 

测试类及结果:

@RestController
public class HiController {
    @Autowired
    HiService hiService;
    @RequestMapping("/hi")
    public String getHi() {
        hiService.getHi();
        return "";
    }
}

访问:http://localhost:8080/mvc/hi

 

posted @ 2020-10-24 15:35  圣金巫灵  阅读(99)  评论(0编辑  收藏  举报