面向切面编程(AOP)

面向切面编程(AOP)是对于面向对象编程(OOP)的补充


添加aop依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

不用添加@EnableAspectJAutoProxy注解,springboot中aspectj默认开启。

package com.example.demo;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class WebLogAspect {
    @Pointcut("execution(public * com.example.demo.ady..*.*(..)))")
    public void webLog(){}
    @After("webLog()")
    public void doAfter(){
        System.out.println("拦截后的逻辑");
    }
    @Before("webLog()")
    public void doBefore(){
       System.out.println("拦截前的逻辑");
    }
}
package com.example.demo.ady;
import org.springframework.web.bind.annotation.*;
@RestController
public class HelloController
{
    @RequestMapping(value="/hello",method= RequestMethod.GET)
    @ResponseBody
    public String hello(){
       System.out.print("heool");
        return "hello";
    }
}

运行localhost:8080/hello

springboot之AOP_面向对象编程