关于java:九Spring从入门到入土AOP就这么简单

9次阅读

共计 5373 个字符,预计需要花费 14 分钟才能阅读完成。

AOP

什么是 AOP

​ 面向切面编程。通过预编译的形式和运行期动静代理实现程序性能的对立保护的一种技术。AOP 是 OOP 的连续,是软件开发中的一个热点,也是 Spring 框架中的一个重要内容,是函数式编程的一种衍生泛型,利用 AOP 能够对业务逻辑的各个局部进行隔离,从而使业务逻辑各个局部的耦合度升高,进步程序的可重用性,同时进步了开发效率。

AOP 在 Spring 中的作用

  • 提供申明式事务;容许用户自定义切面

外围名词

  • 横切关注点:横跨应用程序多个模块的办法或性能。即是,与咱们业务逻辑无关的,然而咱们须要关注的中央,就是横切关注点,如:日志、平安、缓存、事务
  • 切面:横切关注点被模块化的个性对象,即:它是一个类
  • 告诉:切面必须要实现的工作。即它是类中的一个办法
  • 指标:被告诉对象
  • 代理:向指标对象利用告诉当前创立的对象。
  • 切入点:切面告诉执行的“地点的定义
  • 连接点:与切入点匹配的执行点

Spring 中反对的五种类型的 Advice

告诉类型 连接点 实现接口
前置告诉 办法前 MethodBeforeAdvice
后置告诉 办法后 AfterReturningAdvice
盘绕告诉 办法前后 MethodInterceptor
异样抛出告诉 办法抛出异样 ThrowsAdvice
引介告诉 类中减少新办法属性 IntroductionOnterceptor

即 Aop 在不扭转原有代码的状况下,去减少新的性能

应用 Spring 实现 Aop

应用 AOP,须要导入一个依赖包

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.9.4</version>
</dependency>

第一种形式——通过 Spring API 实现

业务接口和实现类
public interface UserService {public void add();

   public void delete();

   public void update();

   public void search();}
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 search() {System.out.println("查问用户");
  }
}
加强类
前置加强
public class Log implements MethodBeforeAdvice {

   //method : 要执行的指标对象的办法
   //objects : 被调用的办法的参数
   //Object : 指标对象
   @Override
   public void before(Method method, Object[] objects, Object o) throws Throwable {System.out.println( o.getClass().getName() + "的" + method.getName() + "办法被执行了");
  }
}
后置加强
public class AfterLog implements AfterReturningAdvice {
   //returnValue 返回值
   //method 被调用的办法
   //args 被调用的办法的对象的参数
   //target 被调用的指标对象
   @Override
   public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {System.out.println("执行了" + target.getClass().getName()
       +"的"+method.getName()+"办法,"
       +"返回值:"+returnValue);
  }
}
去 Spring 的文件中注册, 并实现 aop 切入实现
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

   <!-- 注册 bean-->
   <bean id="userService" class="com.zhonghu.service.UserServiceImpl"/>
   <bean id="log" class="com.zhonghu.log.Log"/>
   <bean id="afterLog" class="com.zhonghu.log.AfterLog"/>

   <!--aop 的配置 -->
   <aop:config>
       <!-- 切入点 expression: 表达式匹配要执行的办法 -->
       <aop:pointcut id="pointcut" expression="execution(* com.zhonghu.service.UserServiceImpl.*(..))"/>
       <!-- 执行盘绕加强; advice-ref 执行办法 . pointcut-ref 切入点 -->
       <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
       <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
   </aop:config>

</beans>  
测试
public class MyTest {
   @Test
   public void test(){ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
       // 动静代理代理的是接口
       UserService userService = (UserService) context.getBean("userService");
       userService.search();}
}

Aop 的重要性:很重要,肯定要了解其中的思路

​ Spring 的 Aop 就是将公共的业务(日志、平安)和畛域业务联合起来,当执行畛域业务时,将会把公共业务加起来,实现公共业务的反复利用,畛域业务更加纯正,程序员只须要专一畛域业务。

​ 其本质还是动静代理

第二种形式:自定义类来实现 Aop

指标业务不变仍旧是 userServiceImpl

切入类
public class DiyPointcut {public void before(){System.out.println("--------- 办法执行前 ---------");
  }
   public void after(){System.out.println("--------- 办法执行后 ---------");
  }
   
}
去 spring 中配置
<!-- 第二种形式自定义实现 -->
<!-- 注册 bean-->
<bean id="diy" class="com.zhonghu.config.DiyPointcut"/>

<!--aop 的配置 -->
<aop:config>
   <!-- 第二种形式:应用 AOP 的标签实现 -->
   <aop:aspect ref="diy">
       <aop:pointcut id="diyPonitcut" expression="execution(* com.zhonghu.service.UserServiceImpl.*(..))"/>
       <aop:before pointcut-ref="diyPonitcut" method="before"/>
       <aop:after pointcut-ref="diyPonitcut" method="after"/>
   </aop:aspect>
</aop:config>
测试
public class MyTest {
   @Test
   public void test(){ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
       UserService userService = (UserService) context.getBean("userService");
       userService.add();}
}

第三种形式——应用注解

注解实现的加强类
package com.zhonghu.config;

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 PointCut {@Before("execution(* com.zhonghu.pojo.User.*(..))")
    public void befer(){System.out.println("办法执行前");
    }

    @After("execution(* com.zhonghu.pojo.User.*(..))")
    public void after(){System.out.println("办法执行后");
    }
    // 在盘绕加强中,咱们能够给定一个参数,代表咱们要解决切入的点。@Around("execution(* com.zhonghu.pojo.User.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {System.out.println("盘绕前");
        System.out.println("签名:"+jp.getSignature());
        // 执行指标办法 proceed
        Object proceed = jp.proceed();
        System.out.println("盘绕后");
        System.out.println(proceed);
    }
}
在 spring 配置文件中,注册 bean,并减少反对注解的配置
<!-- 第三种形式: 注解实现 -->
<bean id="annotationPointcut" class="com.zhonghu.config.AnnotationPointcut"/>
<aop:aspectj-autoproxy/>

输入后果:

切面的执行程序:

aop:aspectj-autoproxy:阐明

  • 通过 aop 创立的命名空间的 <aop:aspectj-autoproxy /> 申明主动为 spring 容器中那些配置 @aspectJ 切面的 bean 创立代理,织入切面。当然,spring 在外部仍旧采纳 AnnotationAwareAspectJAutoProxyCreator 进行主动代理的创立工作,但具体实现的细节曾经被 <aop:aspectj-autoproxy /> 暗藏起来了
  • <aop:aspectj-autoproxy /> 有一个 proxy-target-class 属性,默认为 false,示意应用 jdk 动静代理织入加强,当配为 <aop:aspectj-autoproxy poxy-target-class=”true”/> 时,示意应用 CGLib 动静代理技术织入加强。不过即便 proxy-target-class 设置为 false,如果指标类没有申明接口,则 spring 将主动应用 CGLib 动静代理

最初

  • 如果感觉看完有播种,心愿能给我点个赞,这将会是我更新的最大能源,感激各位的反对
  • 欢送各位关注我的公众号【java 冢狐】,专一于 java 和计算机基础知识,保障让你看完有所播种,不信你打我
  • 如果看完有不同的意见或者倡议,欢送多多评论一起交换。感激各位的反对以及厚爱。

欢送关注公众号“Java 冢狐”,获取最新消息

正文完
 0