spring-aop

文章目录

  • Spring-AOP
      • 1.入门案例
        • 1.1 模拟业务执行一万次
        • 1.2 AOP核心概念
        • 1.3 坐标导入
        • 1.4 aop初体验

Spring-AOP

  • 概念:AOP(Aspect Oriented Programming)面向切面编程,一种编程范式,指导开发者如何组织程序结构
    • OOP(Object Oriented Programming) 面向对象编程
  • 作用:在不惊动原有设计的基础上为其进行功能增强
  • Spring理念:无入侵式/无侵入式

1.入门案例

1.1 模拟业务执行一万次

package com.ttc.dao.impl;

import com.ttc.dao.BookDao;
import org.springframework.stereotype.Repository;

@Repository
public class BookDaoImpl implements BookDao{
    
    public void save(){
        Long startTime = System.currentTimeMillis();
        for(int i=0;i<10000;i++){
            System.out.println("book dao save");
        }
        Long endTime = System.currentTimeMillis();
        System.out.println("业务执行万次所需时间:" + (endTime - startTime));         
    }
    
    public void select(){
     	System.out.println("book dao select");
    }
    
    public void delete(){
        System.out.println("book dao delete");
    }
    
    public void update(){
        System.out.println("book dao update");
    }
    
}
package com.ttc.aop;

public class MyAdvice{
	public void method(){
        Long startTime = System.currentTimeMillis();
        for(int i=0;i<10000;i++){
            // 调用原始操作
        }
        Long endTime = System.currentTimeMills();
        System.out.println("业务执行万次所需时间:" + (endTime - startTime));
    }
}

1.2 AOP核心概念

  • 连接点(JoinPoint):原始方法是连接点
  • 切入点(PointCut):需要追加功能的方法是切入点
  • 通知(Advice):MyAdvice类中的method方法
  • 切面(Aspect):切面就是描述在哪个切入点上执行哪些通知
切入点
切面
通知

1.3 坐标导入

  • 导入spring-context的坐标包含了spring-aop
  • 导入aspect的坐标

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.ttcgroupId>
    <artifactId>aop-demoartifactId>
    <version>1.0-SNAPSHOTversion>

    <dependencies>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>5.2.10.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.9.4version>
        dependency>
    dependencies>
project>

1.4 aop初体验

  1. 制作MyAdvice
package com.ttc.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAdvice {
    // 描述切入点
    @Pointcut("execution (void com.ttc.dao.BookDao.update())")
    private void pt() {
    }
    
    // 在切入点之前执行通知
    @Before("pt()")
    public void method() {
        System.out.println(System.currentTimeMillis());
    }
}

  1. 配置SpringConfig
package com.ttc.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan("com.ttc")
// 启用注解开发的AOP
@EnableAspectJAutoProxy
public class SpringConfig {
}

  1. 启动类测试
package com.ttc;

import com.ttc.config.SpringConfig;
import com.ttc.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        BookDao bookDao = ctx.getBean(BookDao.class);
        bookDao.update();
    }
}

你可能感兴趣的:(java,spring,java,spring,boot)