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:被调用