Spring学习04-[Spring容器核心技术AOP学习]

AOP学习

AOP介绍


  • 如何在Spring中创建一个所谓切面?
    @Aspect+@Component+通知+切点
  • 切面里面的代码怎么运行在业务方法(之前、之后)?
    通知+切点

使用

实现一个对service方法的增强,添加日志

  • 添加依赖
xml 复制代码
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

对业务方法添加计算时间的增强

  • 业务类
java 复制代码
@Service
public class UserService {

    public void add(){
        System.out.println("增加");
    }

    public void delete(){
        System.out.println("删除");
    }

    public void update(){
        System.out.println("修改");
    }

    public void query(){
        System.out.println("查询");
    }
}
  • 切面类
    啥叫面向切面编程:面向切面编程就是面向切面类进行编程
java 复制代码
@Aspect //标记为切面类
@Component
public class LogAspect {


    //实现方法用时 切点表达式
    @Around("execution(* com.sping.service.UserService.*(..))")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        //记录方法用时
        long startTime = System.currentTimeMillis();
        //执行具体的方法
        try {
            joinPoint.proceed(); //连接点就是具体的方法
        }catch (Exception e){
            System.out.println("方法执行异常");
            throw new Exception();
        }

        long endTime = System.currentTimeMillis();
        System.out.println("方法用时:" + (endTime - startTime) + "ms");
        return null;
    }


}
  • 测试
java 复制代码
  @Autowired
    private UserService userService;

    @Test
    public void test() throws InterruptedException {
        userService.add();
    }

@EnableAspectJAutoProxy


AOP的术语





切面、切点、通知、连接点;顾问(通知+切点)

通知

  • 前置通知
  • 后置通知
  • 返回通知
  • 异常通知
    在定义切点的时候,可以把切点定义到另一个方法中,然后在通知中引入即可
java 复制代码
@Aspect
@Component
public class LogAspect{

    @Pointcut("execution( * com.sping.service.UserService.*(..))")
    public void PointCut(){

    }


    @AfterReturning("PointCut()")
    public void log(){
        System.out.println("后置通知执行");
    }
}

环绕通知和其他通知的区别:环绕通知需要手动去拿到连接点执行目标方法,环绕方法更加的灵活

前置通知@Before

在目标方法执行之前执行

java 复制代码
@Aspect
@Component
public class LogAspect{

    @Before("execution(* com.sping.service.*.*(..))")
    public void log(){
        System.out.println("前置通知执行");
    }
}

可以看到标注了前置通知后,前置通知的增强方法先执行,然后再执行目标方法

后置通知@After

与前置通知相反,后置通知是目标方法执行之后才会执行,通知里的增强方法

java 复制代码
@Aspect
@Component
public class LogAspect{
    @After("execution(* com.sping.service.*.*(..))")
    public void log(){
        System.out.println("后置通知执行");
    }
}

返回通知@AfterReturning

其实也是个后置通知,不过就是在方法返回后执行

相关推荐
Java&Develop23 分钟前
springboot + mysql8降低版本到 mysql5.7
java·spring boot·后端
sg_knight25 分钟前
从单体架构到微服务:架构演进之路
java·spring boot·spring·spring cloud·微服务·云原生·架构
华清远见成都中心36 分钟前
嵌入式开发入门指南:从零开始学习嵌入式
学习·嵌入式
武昌库里写JAVA39 分钟前
MacOS Python3安装
java·开发语言·spring boot·学习·课程设计
eternal__day1 小时前
Spring Cloud:构建云原生微服务架构的最佳工具和实践
java·spring cloud·微服务·云原生·架构
cdut_suye1 小时前
【Linux系统】从 C 语言文件操作到系统调用的核心原理
java·linux·数据结构·c++·人工智能·机器学习·云计算
forestsea1 小时前
Maven 插件参数注入与Mojo开发详解
java·maven·mojo
荔枝吻1 小时前
【抽丝剥茧知识讲解】引入mybtis-plus后,mapper实现方式
java·sql·mybatis
在未来等你1 小时前
互联网大厂Java求职面试:构建高并发直播平台的架构设计与优化
java·spring boot·微服务·kubernetes·高并发·分布式系统·直播平台
ghost1432 小时前
C#学习第22天:网络编程
开发语言·学习·c#