public interface HelloService {
String hello(String name);
}
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String name) {
if (name == null) {
throw new RuntimeException("name must not empty!");
}
System.out.println("HelloServiceImpl hello() ==> " + name);
return name;
}
}
编写一个切面
@Component
@Aspect
class MyAspect01 {
@Pointcut("execution(* com.example.springboot.service.HelloService.hello(..))")
public void pointCut() {}
@Before("pointCut()")
public void before(JoinPoint joinPoint) {
System.out.println("before ...... " + Arrays.deepToString(joinPoint.getArgs()));
}
@After("pointCut()")
public void after() {
System.out.println("after ......");
}
@AfterReturning("pointCut()")
public void afterReturning() {
System.out.println("afterReturning ......");
}
@AfterThrowing("pointCut()")
public void afterThrowing() {
System.out.println("afterThrowing ......");
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("around before ......");
Object proceed = joinPoint.proceed();
System.out.println("around after ......");
return proceed;
}
}
控制器测试
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public String hello(String name) {
return helloService.hello(name);
}
}
一个切面的执行结果
当name不为空时
around before ......
before ...... [zhangsan]
HelloServiceImpl hello() ==> zhangsan
around after ......
after ......
afterReturning ......
当name为空时
around before ......
before ...... [null]
after ......
afterThrowing ......
多切面的代码
@Component
@Aspect
@Order(1)
class MyAspect01 {
@Pointcut("execution(* com.example.springboot.service.HelloService.hello(..))")
public void pointCut() {}
@Before("pointCut()")
public void before(JoinPoint joinPoint) {
System.out.println("MyAspect01 before ...... " + Arrays.deepToString(joinPoint.getArgs()));
}
@After("pointCut()")
public void after() {
System.out.println("MyAspect01 after ......");
}
@AfterReturning("pointCut()")
public void afterReturning() {
System.out.println("MyAspect01 afterReturning ......");
}
@AfterThrowing("pointCut()")
public void afterThrowing() {
System.out.println("MyAspect01 afterThrowing ......");
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("MyAspect01 around before ......");
Object proceed = joinPoint.proceed();
System.out.println("MyAspect01 around after ......");
return proceed;
}
}
@Component
@Aspect
@Order(2)
class MyAspect02 {
@Pointcut("execution(* com.example.springboot.service.HelloService.hello(..))")
public void pointCut() {}
@Before("pointCut()")
public void before(JoinPoint joinPoint) {
System.out.println("MyAspect02 before ...... " + Arrays.deepToString(joinPoint.getArgs()));
}
@After("pointCut()")
public void after() {
System.out.println("MyAspect02 after ......");
}
@AfterReturning("pointCut()")
public void afterReturning() {
System.out.println("MyAspect02 afterReturning ......");
}
@AfterThrowing("pointCut()")
public void afterThrowing() {
System.out.println("MyAspect02 afterThrowing ......");
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("MyAspect02 around before ......");
Object proceed = joinPoint.proceed();
System.out.println("MyAspect02 around after ......");
return proceed;
}
}
多切面的执行结果
MyAspect01 around before ......
MyAspect01 before ...... [zhangsan]
MyAspect02 around before ......
MyAspect02 before ...... [zhangsan]
HelloServiceImpl hello() ==> zhangsan
MyAspect02 around after ......
MyAspect02 after ......
MyAspect02 afterReturning ......
MyAspect01 around after ......
MyAspect01 after ......
MyAspect01 afterReturning ......