AOP开发明确的的事项

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

12.2. AOP开发明确的的事项

12.2.1、需要编写的内容

  • 编写核心业务代码目标类的目标方法

  • 编写切面类切面类中有通知(增强功能方法)

  • 在配置文件中配置织入关系即将哪些通知与哪些连接点进行结合

12.2.2、AOP 技术实现的内容

Spring 框架监控切入点方法的执行。一旦监控到切入点方法被运行使用代理机制动态创建目标对象的代理对象根据通知类别在代理对象的对应位置将通知对应的功能织入完成完整的代码逻辑运行。

12.2.3、AOP 底层使用哪种代理方式

在 spring 中框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式。

12.2.4、知识要点

  • aop面向切面编程

  • aop底层实现基于JDK的动态代理 和 基于Cglib的动态代理

  • aop的重点概念

    Pointcut切入点被增强的方法
    
    Advice通知/ 增强封装增强业务逻辑的方法
    
    Aspect切面切点+通知
    
    Weaving织入将切点与通知结合的过程
    
  • 开发明确事项

    谁是切点切点表达式配置
    
    谁是通知切面类中的增强方法
    
    将切点和通知进行织入配置
    

12.2.5、 基于 XML 的 AOP 开发

12.2.5.1 快速入门

①导入 AOP 相关坐标

②创建目标接口和目标类内部有切点

③创建切面类内部有增强方法

④将目标类和切面类的对象创建权交给 spring

⑤在 applicationContext.xml 中配置织入关系

⑥测试代码

①导入 AOP 相关坐标

<!--导入spring的context坐标context依赖aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>

<!-- aspectj的织入 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>

②创建目标接口和目标类内部有切点

//接口
public interface TargetInterface {
  public void method();
}

//实现类
public class Target implements TargetInterface {
  @Override
  public void method() {
      System.out.println("Target running....");
  }
}

③创建切面类内部有增强方法

public class MyAspect {
  //前置增强方法
  public void before(){
      System.out.println("前置代码增强.....");
  }
}

④将目标类和切面类的对象创建权交给 spring管理

<!--配置目标类-->
<bean id="target" class="com.lfs.aop.Target"></bean>

<!--配置切面类-->
<bean id="myAspect" class="com.lfs.aop.MyAspect"></bean>

⑤在 applicationContext.xml 中配置织入关系

导入aop命名空间

<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xsi:schemaLocation="
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd">

⑤在 applicationContext.xml 中配置织入关系

配置切点表达式和前置增强的织入关系

<!--配置织入告诉spring框架哪些方法切点要进行增强前置增强/后置增强-->
<aop:config>
  <!--声明切面-->
  <!--引用myAspect的Bean为切面对象-->
  <aop:aspect ref="myAspect">
      <!--配置切点-->
      <!--配置Target的method方法执行时要进行myAspect的before方法前置增强-->
      <!--切面切点+通知,  要配置那个方法需要前置增强(要配置切入点,需要增强的方法) -->
      <aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.method())"></aop:before>
  </aop:aspect>
</aop:config>

⑥测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
  @Autowired
  private TargetInterface target;
  @Test
  public void test1(){
      target.method();
  }
}

12.2.5.2 切点表达式的写法

表达式语法

execution([修饰符] 返回值类型 包名.类名.方法名(参数))
  • 访问修饰符可以省略

  • 返回值类型、包名、类名、方法名可以使用星号* 代表任意

  • 包名与类名之间一个点 . 代表当前包下的类两个点 … 表示当前包及其子包下的类

  • 参数列表可以使用两个点 … 表示任意个数任意类型的参数列表

例如

execution(public void com.itheima.aop.Target.method())	//不常用具体的方法限制了
execution(void com.itheima.aop.Target.*(..))   //具体的类下的所有方法都会被增强无返回值

execution(* com.itheima.aop.*.*(..))   //常用推荐该包下的任意类的任意方法参数任意都会被增强返回值任意
execution(* com.itheima.aop..*.*(..))  //aop下及其子包下的任意类任意方法都会被增强
execution(* *..*.*(..))   //全部都任意

12.2.5.3 通知的类型

通知的配置语法

<aop:通知类型 method=“切面类中方法名” pointcut=“切点表达式"></aop:通知类型>

在这里插入图片描述

Java类

//增强对象
public class MyAspect {
    //后置增强
    public void before() {
        System.out.println("前置增强................................");
    }

    //后置增强
    public void afterReturning() {
        System.out.println("后置增强................................");
    }

    //Proceeding JoinPoint 正在执行的链接点----切点
    //环绕增强
    public Object Around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("环绕前增强................................");
        Object proceed = point.proceed();//切点方法
        System.out.println("环绕后增强................................");
        return proceed;
    }

    //异常增强
    public void afterThrowing() {
        System.out.println("异常增强................................");
    }

    //最终增强
    public void after() {
        System.out.println("最终增强................................");
    }
}

xml

            <!--切面切点+通知,    要配置那个方法需要前置增强(要配置切入点,需要增强的方法) -->
           <!-- <aop:before method="before" pointcut="execution(public void com.lfs.aop.Target.save())"/>-->
            <aop:before method="before" pointcut="execution( * com.lfs.aop.*.*(..))"/>

            <!--后置增强-->
<aop:after-returning method="afterReturning" pointcut="execution(* com.lfs.aop.*.*(..))"/>

            <!--环绕增强-->
            <aop:around method="Around" pointcut="execution(* com.lfs.aop.*.*(..))"/>

            <!--异常增强-->
<aop:after-throwing method="afterThrowing" pointcut="execution(* com.lfs.aop.*.*(..))"/>

            <!--最终增强-->
            <aop:after method="after" pointcut="execution(* com.lfs.aop.*.*(..))"/>

12.2.5.4 切点表达式的抽取

当多个增强的切点表达式相同时可以将切点表达式进行抽取在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽取后的切点表达式。

//切点表达式
<aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
<aop:config>
    <!--引用myAspect的Bean为切面对象-->
    <aop:aspect ref="myAspect">
        <aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
        <aop:before method="before" pointcut-ref="myPointcut"></aop:before>
    </aop:aspect>
</aop:config>

12.2.5.5 知识要点

  • aop织入的配置
<aop:config>
    <aop:aspect ref=“切面类”>
        <aop:before method=“通知方法名称” pointcut=“切点表达式"></aop:before>
    </aop:aspect>
</aop:config>
  • 通知的类型前置通知、后置通知、环绕通知、异常抛出通知、最终通知
  • 切点表达式的写法
execution([修饰符] 返回值类型 包名.类名.方法名(参数))

12.2.6、基于注解的AOP开发

12.2.6.1 快速入门

基于注解的aop开发步骤

①创建目标接口和目标类内部有切点

②创建切面类内部有增强方法

③将目标类和切面类的对象创建权交给 spring

④在切面类中使用注解配置织入关系

⑤在配置文件中开启组件扫描和 AOP 的自动代理

⑥测试

①创建目标接口和目标类内部有切点

public interface TargetInterface {
    public void method();
}

public class Target implements TargetInterface {
    @Override
    public void method() {
        System.out.println("Target running....");
    }
}

②创建切面类内部有增强方法)

//切面类
public class MyAspect {
    //前置增强方法
    public void before(){
        System.out.println("前置代码增强.....");
    }
}

③将目标类和切面类的对象创建权交给 spring

//实现目标接口
@Component("target")
public class Target implements TargetInterface {
    @Override
    public void method() {
        System.out.println("Target running....");
    }
}
@Component("myAspect")
public class MyAspect {
    public void before(){
        System.out.println("前置代码增强.....");
    }
}

④在切面类中使用注解配置织入关系

//注解配置切面类配置织入
@Component("myAspect")
@Aspect
public class MyAspect {
    @Before("execution(* com.itheima.aop.*.*(..))") //前置增强
    public void before(){
        System.out.println("前置代码增强.....");
    }
}

⑤在配置文件中开启组件扫描和 AOP 的自动代理

<!--组件扫描-->
<context:component-scan base-package="com.itheima.aop"/>

<!--aop的自动代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

⑥测试代码

//测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
    @Autowired
    private TargetInterface target;
    @Test
    public void test1(){
        target.method();
    }
}

12.2.6.2 注解配置 AOP 详解

1) 注解通知的类型

通知的配置语法@通知注解(“切点表达式")

在这里插入图片描述

2) 切点表达式的抽取

同 xml配置
aop 一样我们可以将切点表达式抽取。抽取方式是在切面内定义方法在该方法上使用@Pointcut注解定义切点表达式然后在在增强注解中进行引用。具体如下

@@Component("myAspect")
@Aspect
public class MyAspect {
    @Before("MyAspect.myPoint()")
    public void before(){
        System.out.println("前置代码增强.....");
    }
    @Pointcut("execution(* com.itheima.aop.*.*(..))")
    public void myPoint(){
        
    }
}

12.2.6.3 知识要点

  • 注解aop开发步骤

①使用@Aspect标注切面类

②使用@通知注解标注通知方法

③在配置文件中配置aop自动代理 aop:aspectj-autoproxy/

  • 通知注解类型

在这里插入图片描述

12.3.Aop在Spring中的作用

提供声明式事务允许用户自定义切面

  • 横切关注点跨越应用程序多个模块的方法或功能。即是与我们业务逻辑无关的但是我们需要关注的部分就是横切关注点。如日志安全缓存事务等等…。
  • 切面(ASPECT)横切关注点被模块化的特殊对象。即它是一个类。
  • 通知(Advice)切面必须要完成的工作。即它是类中的一个方法。
  • 目标(Target)被通知对象。
  • 代理(Poy)向目标对象应用通知之后创建的对象。
  • 切入点(PointCut)切面通知执行的"地点"的定义。
  • 连接点(JointPoint)与切入点匹配的执行点。

12.4.使用spring实现Aop

【重点】使用AOP织入需要导入一依赖包

<dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
    </dependencies>

service

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}
public class UserServiceImpl implements UserService{

    @Override
    public void add() {
        System.out.println("增加了一个用户");
    }

    @Override
    public void delete() {
        System.out.println("删除了一个用户");
    }

    @Override
    public void update() {
        System.out.println("更新了一个用户");
    }

    @Override
    public void select() {
        System.out.println("查询了一个用户");
    }
}

方式一、使用原生spring API接口【主要springAPI接口实现】

spring配置——applicationContext.xml

test

import com.blue.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是接口
        UserService userService = context.getBean("userService", UserService.class);
        
        userService.add();
    }
}

方式二、自定义类【主要是切面定义】

public class DiyPointCut {
    public void before(){
        System.out.println("=============方法执行前===========");
    }

    public void after(){
        System.out.println("=============方法执行后===========");
    }
}
<!-- 方式二自定义类   -->
    <bean id="diy" class="com.blue.diy.DiyPointCut"/>

    <aop:config>
        <!--   自定义切面ref 要因用的类     -->
        <aop:aspect ref="diy">
            <aop:pointcut id="point" expression="execution(* com.blue.service.UserServiceImpl.*(..))"/>
            <!--  通知  -->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:before method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

方式三使用注解方式实现AOP

    <!-- 方式三-->
    <bean id="annotationPointOut" class="com.blue.diy.AnnotationPointCut"/>
        
    <!--开启注解支持  自动代理-->
    <aop:aspectj-autoproxy/>

AnnotationPointCut

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AnnotationPointCut {
    @Before("execution(* com.blue.service.UserServiceImpl.*(..))")
    public  void before(){
        System.out.println("=============方法执行前===========");
    }

    @After("execution(* com.blue.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("=============方法执行后===========");
    }

}
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6