Spring Boot - AOP(面向切面)
AOP 全称 Aspect Oriented Programming(面向切面),AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果。其与设计模式完成的任务差不多,是提供另一种角度来思考程序的结构,来弥补面向对象编程的不足。
通俗点讲就是提供一个为一个业务实现提供切面注入的机制,通过这种方式,在业务运行中将定义好的切面通过切入点绑定到业务中,以实现将一些特殊的逻辑绑定到此业务中。
比如,若是需要一个记录日志的功能,首先想到的是在方法中通过日志框架来进行记录日志,但写下来发现一个问题,在整个业务中其实核心的业务代码并没有多少,都是一些记录日志或其他辅助性的一些代码。而且很多业务有需要相同的功能,比如都需要记录日志,这时候又需要将这些记录日志的功能复制一遍,即使是封装成框架,也是需要调用之类的。
AOP 的基本概念
- Aspect:切面,一个关注点的模块化,这个关注点可能会横切多个对象。事务管理是J2EE应用中一个关于横切关注点的很好的例子。在Spring AOP中,切面可以使用基于模式或者基于@Aspect 注解的方式来实现。
- Joinpoint:连接点,在程序执行过程中某个特定的点,比如某方法调用的时候或者处理异常的时候。在Spring AOP中,一个连接点总是表示一个方法的执行。
- Advice:通知,在切面的某个特定的连接点上执行的动作。其中包括了"Around"、"Before"和"After"等不同类型的通知(通知的类型将在后面部分进行讨论)。许多AOP框架(包括Spring)都是以拦截器做通知模型,并维护一个以连接点为中心的拦截器链。
- Pointcut:切入点,匹配连接点的断言。通知和一个切入点表达式关联,并在满足这个切入点的连接点上运行(例如,当执行某个特定名称的方法时)。切入点表达式如何和连接点匹配是AOP的核心:Spring 默认使用AspectJ切入点语法。
- Introduction:引入,用来给一个类型声明额外的方法或属性(也被称为连接类型声明(inter-type declaration))。Spring允许引入新的接口(以及一个对应的实现)到任何被代理的对象。例如,你可以使用引入来使一个bean实现IsModified接口,以便简化缓存机制。
- Target Object:目标对象,被一个或者多个切面所通知的对象。也被称做被通知(advised)对象。既然 Spring AOP是通过运行时代理实现的,这个对象永远是一个被代理(proxied)对象。
- AOP Proxy:AOP代理,AOP框架创建的对象,用来实现切面契约(例如通知方法执行等等)。在Spring中,AOP代理可以是JDK动态代理或者CGLIB代理。
- Weaving:织入,把切面连接到其它的应用程序类型或者对象上,并创建一个被通知的对象。这些可以在编译时(例如使用AspectJ编译器),类加载时和运行时完成。Spring和其他纯Java AOP框架一样,在运行时完成织入。
快速入门
我们通过实现记录方法执行时间的跟踪日志来演示 Spring AOP,示例如下:
- 创建项目
创建 Spring Boot 项目,命名为 spring-aop,增加 spring-boot-starter-aop 依赖开启 AOP 支持,pom.xml 文件内容如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.lixue</groupId>
<artifactId>spring-aop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-aop</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
<relativePath/><!--lookupparentfromrepository-->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 创建注解
我们通过注解来标注切面,因此我们需要创建注解类,使用 @Target 注解使用在方法中,使用 @Retention 注解用来声明注解的保留策略,代码如下:
package org.lixue;
import java.lang.annotation.*;
@Target(value={ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EanbleLogTrace{
String name() default "";
}
- 创建切面类
创建切面类 TraceLogAspect,用于定义跟踪日志的切入点(被 org.lixue.EnableLogTrace 注解标注的方法和类为切入点),和通知方法,代码如下:
package org.lixue;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class TraceLogAspect{
private ThreadLocal<Long> startTimeMillis;
public TraceLogAspect(){
startTimeMillis=new ThreadLocal<>();
}
/**
*定义切入点,使用注解方式,声明了被org.lixue.EnableLogTrace注解标注的方法和类为切入点
*/
@Pointcut("@annotation(org.lixue.EnableLogTrace) || within(@org.lixue.EnableLogTrace *)")
private void pointcut(){
}
/**
*定义通知before(方法执行前),切面使用pointcut()定义的
*
*@param joinPoint
*/
@Before("pointcut()")
public void before(JoinPointjoinPoint){
Object[] args=joinPoint.getArgs();
String methodName=joinPoint.getSignature().getName();
startTimeMillis.set(System.currentTimeMillis());
StringBuilder stringBuilder=new StringBuilder();
for(inti=0;i<args.length;i++){
stringBuilder.append(args[i]+"");
}
System.out.println("beforename="+methodName+"\targs="+stringBuilder.toString()
+"startTimeMillis="+startTimeMillis.get()+"\tThread="+Thread.currentThread().getId());
}
/**
*定义通知After(方法执行后),切面使用pointcut()定义的
*
*@param joinPoint
*/
@After("pointcut()")
public void after(JoinPointjoinPoint){
Object[] args=joinPoint.getArgs();
String methodName=joinPoint.getSignature().getName();
try{
long executingTimeMillis=System.currentTimeMillis()-startTimeMillis.get();
StringBuilder stringBuilder=newStringBuilder();
for(inti=0;i<args.length;i++){
stringBuilder.append(args[i]+"");
}
System.out.println("aftername="+methodName+"\targs="+stringBuilder.toString()
+"executingTimeMillis="+executingTimeMillis+"\tThread="+Thread.currentThread().getId());
}finally{
startTimeMillis.remove();
}
}
}
- 创建示例类
创建示例类,使用注解 @EnableLogTrace 标注类或者方法
package org.lixue;
import org.springframework.stereotype.Service;
import java.util.Random;
@EnableLogTrace(name="trace")
@Service
public class AccountService{
private Random random;
publicAccountService(){
random = new Random();
}
public boolean login(Stringname,Stringpassword){
int sleep;
try{
sleep=random.nextInt(100);
Thread.sleep(sleep);
System.out.println("loginname="+name+"\tpassword="+password+"\tsleep="+sleep+"\tThread="
+Thread.currentThread().getId());
return true;
}catch(InterruptedExceptione){
e.printStackTrace();
return false;
}
}
}
- 测试验证
编写单元测试类,通过自动注入 AccountService 类,来调用 login 方法,看日志输入:
package org.lixue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AccountServiceTest{
@Autowired
private AccountService accountService;
@Test
public void login() throws Exception{
accountService.login("lixue","123123");
}
}
执行单元测试输出如下:
before name=login args=lixue 123123 startTimeMillis=1525590400031 Thread=1
login name=lixue password=123123 sleep=86 Thread=1
after name=login args=lixue 123123 executingTimeMillis=114 Thread=1
Spring Boot - AOP(面向切面)的更多相关文章
- spring总结————AOP面向切面总结
spring总结————AOP面向切面 一.spring aop概念 spring aop面向切面编程,java是面向对象的语言. 真正的service层代码 业务逻辑层再处理业务之前和之后都要进行一 ...
- Spring:AOP面向切面编程
AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...
- Spring 08: AOP面向切面编程 + 手写AOP框架
核心解读 AOP:Aspect Oriented Programming,面向切面编程 核心1:将公共的,通用的,重复的代码单独开发,在需要时反织回去 核心2:面向接口编程,即设置接口类型的变量,传入 ...
- Spring框架 AOP面向切面编程(转)
一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...
- 【spring源码学习】spring的AOP面向切面编程的实现解析
一:Advice(通知)(1)定义在连接点做什么,为切面增强提供织入接口.在spring aop中主要描述围绕方法调用而注入的切面行为.(2)spring定义了几个时刻织入增强行为的接口 => ...
- Spring的AOP面向切面编程
什么是AOP? 1.AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 功能: 让关注点代码与业务代码分离! 关注点: 重复代码就叫做关注点: ...
- spring:AOP面向切面编程02
参考: https://blog.csdn.net/jeffleo/article/details/54136904 一.AOP的核心概念AOP(Aspect Oriented Programming ...
- Spring注解 - AOP 面向切面编程
基本概念: AOP:Aspect Oriented Programming,即面向切面编程 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式 前置通知(@Before):在目标 ...
- Spring框架——AOP面向切面编程
简介 AOP练习 使用动态代理解决问题 Spring AOP 用AspectJ注解声明切面 前置后置通知 利用方法签名编写AspectJ切入点表达式 指定切面的优先级 基于XML的配置声明切面 Spr ...
- Spring之AOP(面向切面编程)_入门Demo
AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,AOP可 ...
随机推荐
- 【亲测】Asp.net Mvc5 + EF6 code first 方式连接MySQL总结
本文原文地址为:https://www.cnblogs.com/summit7ca/p/5423637.html 原文测试环境为windows 8.1+Vs2013+MySql5.7.12 本人在wi ...
- mac下修改mysql默认字符集为utf8
1.首先检查默认安装的字符集 mysql> show variables like '%char%'; +--------------------------+----------------- ...
- 2018-2019-2 《网络对抗技术》Exp5 MSF基础应用 20165326
Exp5 MSF基础应用 实践内容 主动攻击实践 ms17_010_enternalblue 靶机:win7 x64成功 针对浏览器的攻击 ms14_064_ole_code_execution 靶机 ...
- .Net Core+Angular6 学习 第四部分(EF Core(Code First))
目的: 打算通过EF core 练习从database receive data 显示到UI. 1. 创建一个新的project Model.定义一个 base interface entity以及实 ...
- sql嵌套查询
嵌套查询的意思是,一个查询语句(select-from-where)查询语句块可以嵌套在另外一个查询块的where子句中,称为嵌套查询.其中外层查询也称为父查询,主查询.内层查询也称子查询,从查询. ...
- vue启动报错
在安装依赖后,启动时报错 修复方法:将项目node_modules文件夹删除掉,重新cnpm install即可
- 【sklearn入门】通过sklearn实现k-means并可视化聚类结果
import numpy as np from sklearn.cluster import KMeans from mpl_toolkits.mplot3d import Axes3D import ...
- 十分钟搞定 pandas
原文:http://pandas.pydata.org/pandas-docs/stable/10min.html 译者:ChaoSimple 校对:飞龙 官方网站上<10 Minutes to ...
- USGS bulk批量下载工具
最近美国EarthExplorer上批量下载遥感数据---官方给出了批量下载工具BULK 下载地址:https://earthexplorer.usgs.gov/bulk/ bulk 使用帮助文档 根 ...
- 海思hi3516 ive运动目标检测简单实现
在做车牌识别项目,通过先对识别区域内进行目标识别,能降低CPU的占用率,在检测到有运动目标的时候,再做车牌识别. //图像差分 s32Ret = HI_MPI_IVE_Sub(&IveHand ...