面向切面 (AOP)aspect oriented programming
将通用逻辑从业务逻辑中分离出来
- 创建 ```java package com.example.luckymoney.aspect;
import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;
@Aspect @Component public class HttpAspect {
@Before("execution(public * com.example.luckymoney.controller.LuckymoneyController.*(..))")
public void logBefore() {
System.out.println("11111");
}
@After("execution(public * com.example.luckymoney.controller.LuckymoneyController.*(..))")
public void logAfter() {
System.out.println("22222");
}
}
2. 改进
```java
package com.example.luckymoney.aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class HttpAspect {
@Pointcut("execution(public * com.example.luckymoney.controller.LuckymoneyController.*(..))")
public void log() {
}
@Before("log()")
public void logBefore() {
System.out.println("11111");
}
@After("log()")
public void logAfter() {
System.out.println("22222");
}
}