Spring AOP 面向切面编程必须知道的事
1、概览
什么是面向切面编程?
面向切面编程是一种编程范式(其他常见的编程范式有 面向过程编程,面向对象编程OOP,面向函数编程,面向事件驱动编程,面向切面编程),它不是一种编程语言,面向切面编程可以解决特定的问题,但是不能解决所有问题,它是面向对象编程的补充,不是替代。
它可以很大程度上解决代码重复性问题,而且可以实现关注点分离,比如功能性需求和非功能性需求的分离,从而实现集中管理,增强代码的可读性,可维护性。
2、AOP常见的使用场景
在系统开发过程中常见的使用场景 主要有
权限控制
缓存控制
事务控制
审计日志
性能监控
分布式追踪
异常处理
3、Spring AOP 两个主要关注点
Pointcut express
切面表达式,主要表达通过怎样的方式找到切面插入的逻辑点,pointcut express 提供了丰富的表达式可以让我们进行切面的插入。
五种Advice
找到切入点后,需要明确在什么时机进行代码植入,主要有五种,如下:
@Before 前置通知
@After(finally) ,后置通知,在方法执行完之后切入
@AfterReturning,返回通知,返回值返回之后
@AfterThrowing,异常通知,抛出异常之后
@Around ,环绕通知,环绕通知包含了上面所有的类型
以上两个关注点 总结一句话就是 在什么地方什么时机进行我们的代码切入。
4、常见切面表达式
1、within表达式,匹配包或者类 下面的方法
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* //匹配ProductService类里头的所有方法
* @Pointcut("within(com.ruoli.service.ProductService)")
* //匹配com.ruoli包及子包下所有类的方法
* @Pointcut("within(com.ruoli..*)")
*/
@Aspect
@Component
public class PkgTypeAspectConfig {
@Pointcut("within(com.ruoli.service.sub.*)")
public void matchType(){}
@Before("matchType()")
public void before(){
System.out.println("");
System.out.println("###before");
}
}
2、对象匹配
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* //匹配AOP对象的目标对象为指定类型的方法,即LogService的aop代理对象的方法
* @Pointcut("this(com.ruoli.log.Loggable)")
* //匹配实现Loggable接口的目标对象(而不是aop代理后的对象)的方法
* @Pointcut("target(com.ruoli.log.Loggable)")
* //this 可以拦截 DeclareParents(Introduction)
* //target 不拦截 DeclareParents(Introduction)
* //匹配所有以Service结尾的bean里头的方法
* @Pointcut("bean(*Service)")
* Created by cat on 2016-12-04.
*/
@Aspect
@Component
public class ObjectAspectConfig {
@Pointcut("bean(logService)")
public void matchCondition(){}
@Before("matchCondition()")
public void before(){
System.out.println("");
System.out.println("###before");
}
}
3、参数匹配,配置指定参数的方法
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* //匹配任何以find开头而且只有一个Long参数的方法
* @Pointcut("execution(* *..find*(Long))")
* //匹配任何以find开头的而且第一个参数为Long型的方法
* @Pointcut("execution(* *..find*(Long,..))")
* //匹配任何只有一个Long参数的方法
* @Pointcut("within(com.ruoli..*) && args(Long)")
* //匹配第一个参数为Long型的方法
* @Pointcut("within(com.ruoli..*) && args(Long,..)")
* Created by cat on 2016-12-04.
*/
@Aspect
@Component
public class ArgsAspectConfig {
@Pointcut("args(Long,String) && within(com.ruoli.service.*)")
public void matchArgs(){}
@Before("matchArgs()")
public void before(){
System.out.println("");
System.out.println("###before");
}
}
4、注解匹配
主要有 方法级别注解,类级别注解,参数级别注解。
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* //匹配方法标注有AdminOnly的注解的方法
* @Pointcut("@annotation(com.ruoli.anno.AdminOnly) && within(com.ruoli..*)")
* //匹配标注有NeedSecured的类底下的方法 //class级别
* @Pointcut("@within(com.ruoli.anno.NeedSecured) && within(com.ruoli..*)")
* //匹配标注有NeedSecured的类及其子类的方法 //runtime级别
* 在spring context的环境下,二者没有区别
* @Pointcut("@target(com.ruoli.anno.NeedSecured) && within(com.ruoli..*)")
* //匹配传入的参数类标注有Repository注解的方法
* @Pointcut("@args(com.ruoli.anno.NeedSecured) && within(com.ruoli..*)")
* Created by cat on 2016-12-04.
*/
@Aspect
@Component
public class AnnoAspectConfig {
@Pointcut("@args(com.ruoli.anno.NeedSecured) && within(com.ruoli..*)")
public void matchAnno(){}
@Before("matchAnno()")
public void before(){
System.out.println("");
System.out.println("###before");
}
}
5、execution 表达式
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* //匹配任何公共方法
@Pointcut("execution(public * com.ruoli.service.*.*(..))")
//匹配com.imooc包及子包下Service类中无参方法
@Pointcut("execution(* com.ruoli..*Service.*())")
//匹配com.imooc包及子包下Service类中的任何只有一个参数的方法
@Pointcut("execution(* com.ruoli..*Service.*(*))")
//匹配com.imooc包及子包下任何类的任何方法
@Pointcut("execution(* com.ruoli..*.*(..))")
//匹配com.imooc包及子包下返回值为String的任何方法
@Pointcut("execution(String com.ruoli..*.*(..))")
//匹配异常
execution(public * com.ruoli.service.*.*(..) throws java.lang.IllegalAccessException)
*
*/
@Aspect
@Component
public class ExecutionAspectConfig {
@Pointcut("execution(public * com.ruoli.service..*Service.*(..) throws java.lang.IllegalAccessException)")
public void matchCondition(){}
@Before("matchCondition()")
public void before(){
System.out.println("");
System.out.println("###before");
}
}
5、五种通知代码示例
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.omg.CORBA.Object;
import org.springframework.stereotype.Component;
/**
* @Before("matchAnno()")
* @After("matchAnno())") //相当于finally
* @AfterReturning("matchException()")
* @AfterThrowing("matchException()")
* @Around("matchException()")
* @Before(value = "matchLongArg() && args(productId)")
* public void beforeWithArgs(Long productId)
* @AfterReturning(value = "matchReturn()",returning = "returnValue")
* public void getReulst(Object returnValue)
*
*/
@Aspect
@Component
public class AdviceAspectConfig {
/******pointcut********/
@Pointcut("@annotation(com.ruoli.anno.AdminOnly) && within(com.ruoli..*)")
public void matchAnno(){}
@Pointcut("execution(* *..find*(Long)) && within(com.ruoli..*) ")
public void matchLongArg(){}
@Pointcut("execution(public * com.ruoli.service..*Service.*(..) throws java.lang.IllegalAccessException) && within(com.ruoli..*)")
public void matchException(){}
@Pointcut("execution(String com.ruoli..*.*(..)) && within(com.ruoli..*)")
public void matchReturn(){}
/******advice********/
@Before("matchLongArg() && args(productId)")
public void before(Long productId){
System.out.println("###before,get args:"+productId);
}
@Around("matchException()")
public java.lang.Object after(ProceedingJoinPoint joinPoint){
System.out.println("###before");
java.lang.Object result = null;
try{
result = joinPoint.proceed(joinPoint.getArgs());
System.out.println("###after returning");
}catch (Throwable e){
System.out.println("###ex");
//throw
e.printStackTrace();
}finally {
System.out.println("###finally");
}
return result;
}
}

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
Step by Step!Kubernetes持续部署指南
本文是作者通过亲身实践,从零基础开始,一步一步总结出来的Kubernetes持续部署工作流程。文章从前期的工具准备开始,到复刻存储库、测试、构建镜像、构建流水线最后进行部署,所有的工作流程都一一展现在文章中,对于想要拥有全自动持续交付流水线的用户将有很大的借鉴意义。 在很久很久以前的一份工作中,我的任务是将老式的LAMP堆栈切换到Kubernetes上。那会儿我的老板总是追逐新技术,认为只需要几天时间就能够完成新旧技术的迭代——鉴于那时我们甚至对容器的工作机制一无所知,所以不得不说老板的想法真的很大胆。 在阅读了官方文档并且搜索了很多信息之后,我们开始感到不知所措——有许多新的概念需要学习:pod、容器以及replica等。对我而言,Kubernetes似乎只是为一群聪明的开发者而设计的。 然后我做了我在这一状况下常做的事:通过实践来学习。通过一个简单的例子可以很好地理解错综复杂的问题,所以我自己一步一步完成了整个部署过程。 最后,我们做到了,虽然远未达到规定的一周时间——我们花了将近一个月的时间来创建三个集群,包括它们的开发、测试和生产。 本文我将详细介绍如何将应用程序部署到Kube...
-
下一篇
海量数据搜索---demo展示百度、谷歌搜索引擎的实现
在我们平常的生活工作中,百度、谷歌这些搜索网站已经成为了我们受教解惑的学校,俗话说得好,“有问题找度娘”。那么百度是如何在海量数据中找到自己需要的数据呢?为什么它搜索的速度如此之快?我们都知道是因为百度的搜索引擎,那么搜索引擎到底是个什么东西呢?可能有的程序员会想到es,但是es并不能代表搜索引擎,它只是其中的一种工具,不过这种工具确实好用,效率很高。 本文会向大家讲述搜索引擎的基本知识以及中文分词的一些方法、然后会做一个小的demo来尝试数据检索。让大家初步了解搜索引擎的实现。 一、搜索引擎介绍 1.1 搜索引擎是什么 这里引用百度百科的介绍: 搜索引擎(Search Engine)是指根据一定的策略、运用特定的计算机程序从互联网上搜集信息,在对信息进行组织和处理后,为用户提供检索服务,将用户检索相关的信息展示给用户的系统。 1.2 搜索引擎分类 搜索引擎包括全文索引、目录索引、元搜索引擎、垂直搜索引擎、集合式搜索引擎、门户搜索引擎与免费链接列表等。 本文主要介绍全文索引,即百度使用的搜索引擎分类。 全文索引 首先是数据库中数据的搜集,搜索引擎的自动信息搜集功能分两种: 一种是定期搜...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- MySQL数据库在高并发下的优化方案
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Docker容器配置,解决镜像无法拉取问题
- Docker安装Oracle12C,快速搭建Oracle学习环境
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- 2048小游戏-低调大师作品
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作