Spring实现AOP的4种方式

Spring实现AOP的4种方式

 

  先了解AOP的相关术语:
1.通知(Advice):
通知定义了切面是什么以及何时使用。描述了切面要完成的工作和何时需要执行这个工作。
2.连接点(Joinpoint):
程序能够应用通知的一个“时机”,这些“时机”就是连接点,例如方法被调用时、异常被抛出时等等。
3.切入点(Pointcut)
通知定义了切面要发生的“故事”和时间,那么切入点就定义了“故事”发生的地点,例如某个类或方法的名称,Spring中允许我们方便的用正则表达式来指定
4.切面(Aspect)
通知和切入点共同组成了切面:时间、地点和要发生的“故事”
5.引入(Introduction)
引入允许我们向现有的类添加新的方法和属性(Spring提供了一个方法注入的功能)
6.目标(Target)
即被通知的对象,如果没有AOP,那么它的逻辑将要交叉别的事务逻辑,有了AOP之后它可以只关注自己要做的事(AOP让他做爱做的事)
7.代理(proxy)
应用通知的对象,详细内容参见设计模式里面的代理模式
8.织入(Weaving)
把切面应用到目标对象来创建新的代理对象的过程,织入一般发生在如下几个时机:
(1)编译时:当一个类文件被编译时进行织入,这需要特殊的编译器才可以做的到,例如AspectJ的织入编译器
(2)类加载时:使用特殊的ClassLoader在目标类被加载到程序之前增强类的字节代码
(3)运行时:切面在运行的某个时刻被织入,SpringAOP就是以这种方式织入切面的,原理应该是使用了JDK的动态代理技术

Spring提供了4种实现AOP的方式:
1.经典的基于代理的AOP
2.@AspectJ注解驱动的切面
3.纯POJO切面
4.注入式AspectJ切面

首先看经典的基于代理的AOP:
Spring支持五种类型的通知:
Before(前)  org.apringframework.aop.MethodBeforeAdvice
After-returning(返回后) org.springframework.aop.AfterReturningAdvice
After-throwing(抛出后) org.springframework.aop.ThrowsAdvice
Arround(周围) org.aopaliance.intercept.MethodInterceptor
Introduction(引入) org.springframework.aop.IntroductionInterceptor

值的说明的是周围通知,他是由AOP Alliance中的接口定义的而非Spring,周围通知相当于前通知、返回后通知、抛出后通知的结合(传说中的完全体?好吧,我看日和看多

了)还有引入通知怎么玩我还没搞清楚,等心无杂念的时候玩玩

这东西怎么玩?这么几个步骤:
1.创建通知:实现这几个接口,把其中的方法实现了
2.定义切点和通知者:在Spring配制文件中配置这些信息
3.使用ProxyFactoryBean来生成代理

具体做法。。。大晚上的就举个睡觉的例子吧:

首先写一个接口叫Sleepable,这是一个牛X的接口,所有具有睡觉能力的东西都可以实现该接口(不光生物,包括关机选项里面的休眠)

package test.spring.aop.bean

public interface Sleepable{
 
    void sleep(); 
}

然后写一个Human类,他实现了这个接口

package test.spring.aop.bean

public Human implements Sleepable{
   
   /*这人莫非跟寡人差不多?
    *除了睡觉睡的比较好之外其余的什么也不会做?*/
   public void sleep(){
      System.out.println("睡觉了!梦中自有颜如玉!");
   }

}


好了,这是主角,不过睡觉前后要做些辅助工作的,最基本的是脱穿衣服,失眠的人还要吃安眠药什么的,但是这些动作与纯粹的睡觉这一“业务逻辑”是不相干的,如果把

这些代码全部加入到sleep方法中,是不是有违单一职责呢?,这时候我们就需要AOP了。

编写一个SleepHelper类,它里面包含了睡觉的辅助工作,用AOP术语来说它就应该是通知了,我们需要实现上面的接口

package test.spring.aop.bean;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class SleepHelper implements MethodBeforeAdvice,AfterReturningAdvice{

    public void before(Method mtd, Object[] arg1, Object arg2)
            throws Throwable {
        System.out.println("通常情况下睡觉之前要脱衣服!");
    }

    public void afterReturning(Object arg0, Method arg1, Object[] arg2,
            Object arg3) throws Throwable {
        System.out.println("起床后要先穿衣服!");
    }
    
}

然后在spring配置文件中进行配置:
<bean id="sleepHelper" class="test.spring.aop.bean.SleepHelper">
</bean>

OK!现在创建通知的工作就完成了.

第二步是进行配置,这是很令人蛋疼的操作,尤其是这么热的天,Spring又把东西的名字起的见鬼的长!它为啥不能像usr这种风格呢?

首先要做的是配置一个切点,据说切点的表示方式在Spring中有好几种,但是常用的只有两种:1.使用正则表达式 2.使用AspectJ表达式 AspectJ我不是很熟悉(我也是熟悉

党 or 精通党?),我还是习惯用正则表达式

Spring使用org.springframework.aop.support.JdkRegexpMethodPointcut来定义正则表达式切点
<bean id="spleepPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
  <property name="pattern" value=".*sleep"/>
</bean>

pattern属性指定了正则表达式,它匹配所有的sleep方法

切点仅仅是定义了故事发生的地点,还有故事发生的时间以及最重要的故事的内容,就是通知了,我们需要把通知跟切点结合起来,我们要使用的通知者是:
org.springframework.aop.support.DefaultPointcutAdvisor

<bean id="sleepHelperAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
     <property name="advice" ref="sleepHelper"/>
     <property name="pointcut" ref="sleepPointcut"/>
</bean>

切入点和通知都配置完成,接下来该调用ProxyFactoryBean产生代理对象了

<bean id="humanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
     <property name="target" ref="human"/>
     <property name="interceptorNames" value="sleepHelperAdvisor" />
     <property name="proxyInterfaces" value="test.spring.aop.bean.Sleepable" />
</bean>

ProxyFactoryBean是一个代理,我们可以把它转换为proxyInterfaces中指定的实现该interface的代理对象:

import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import test.spring.aop.bean.Sleepable;


public class Test {

    public static void main(String[] args){
        ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Sleepable sleeper = (Sleepable)appCtx.getBean("humanProxy");
        sleeper.sleep();
    }
}

程序运行产生结果:
通常情况下睡觉之前要脱衣服!
睡觉啦~梦中自有颜如玉!
起床后要先穿衣服!

OK!这是我们想要的结果,但是上面这个过程貌似有点复杂,尤其是配置切点跟通知,Spring提供了一种自动代理的功能,能让切点跟通知自动进行匹配,修改配置文件如下:
 <bean id="sleepHelper" class="test.spring.aop.bean.SleepHelper">
  </bean>
  <bean id="sleepAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice" ref="sleepHelper"/>
    <property name="pattern" value=".*sleep"/>
  </bean>
  <bean id="human" class="test.spring.aop.bean.Human">
  </bean>
  <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>

执行程序:
public class Test {

    public static void main(String[] args){
        ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Sleepable sleeper = (Sleepable)appCtx.getBean("human");
        sleeper.sleep();
    }
}
成功输出结果跟前面一样!
只要我们声明了org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator(我勒个去的,名太长了)就能为方法匹配的bean自动创建代理!

但是这样还是要有很多工作要做,有更简单的方式吗?有!

一种方式是使用AspectJ提供的注解:

package test.mine.spring.bean;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class SleepHelper {

    public SleepHelper(){
        
    }
    
    @Pointcut("execution(* *.sleep())")
    public void sleeppoint(){}
    
    @Before("sleeppoint()")
    public void beforeSleep(){
        System.out.println("睡觉前要脱衣服!");
    }
    
    @AfterReturning("sleeppoint()")
    public void afterSleep(){
        System.out.println("睡醒了要穿衣服!");
    }
    
}

用@Aspect的注解来标识切面,注意不要把它漏了,否则Spring创建代理的时候会找不到它,@Pointcut注解指定了切点,@Before和@AfterReturning指定了运行时的通知,注

意的是要在注解中传入切点的名称

然后我们在Spring配置文件上下点功夫,首先是增加AOP的XML命名空间和声明相关schema
命名空间:
xmlns:aop="http://www.springframework.org/schema/aop"
schema声明:
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

然后加上这个标签:
<aop:aspectj-autoproxy/> 有了这个Spring就能够自动扫描被@Aspect标注的切面了

最后是运行,很简单方便了:
public class Test {

    public static void main(String[] args){
        ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Sleepable human = (Sleepable)appCtx.getBean("human");
        human.sleep();
    }
}

下面我们来看最后一种常用的实现AOP的方式:使用Spring来定义纯粹的POJO切面

前面我们用到了<aop:aspectj-autoproxy/>标签,Spring在aop的命名空间里面还提供了其他的配置元素:
<aop:advisor> 定义一个AOP通知者
<aop:after> 后通知
<aop:after-returning> 返回后通知
<aop:after-throwing> 抛出后通知
<aop:around> 周围通知
<aop:aspect>定义一个切面
<aop:before>前通知
<aop:config>顶级配置元素,类似于<beans>这种东西
<aop:pointcut>定义一个切点

我们用AOP标签来实现睡觉这个过程:
代码不变,只是修改配置文件,加入AOP配置即可:
<aop:config>
    <aop:aspect ref="sleepHelper">
    <aop:before method="beforeSleep" pointcut="execution(* *.sleep(..))"/>
    <aop:after method="afterSleep" pointcut="execution(* *.sleep(..))"/>
    </aop:aspect>
</aop:config>

完!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388029.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何使用Plotly在Python中为任何DataFrame绘制地图的卫星视图

Chart-Studio和Mapbox简介 (Introduction to Chart-Studio and Mapbox) Folium and Geemap are arguably the best GIS libraries/tools to plot satellite-view maps or any other kinds out there, but at times they require an additional authorization to use the Google…

Java入门系列-26-JDBC

认识 JDBC JDBC (Java DataBase Connectivity) 是 Java 数据库连接技术的简称&#xff0c;用于连接常用数据库。 Sun 公司提供了 JDBC API &#xff0c;供程序员调用接口和类&#xff0c;集成在 java.sql 和 javax.sql 包中。 Sun 公司还提供了 DriverManager 类用来管理各种不…

3.19PMP试题每日一题

在房屋建造过程中&#xff0c;应该先完成卫生管道工程&#xff0c;才能进行电气工程施工&#xff0c;这是一个&#xff1a;A、强制性依赖关系B、选择性依赖关系C、外部依赖关系D、内部依赖关系 作者&#xff1a;Tracy19890201&#xff08;同微信号&#xff09;转载于:https://…

Can't find temporary directory:internal error

今天我机子上的SVN突然没有办法进行代码提交了&#xff0c;出现的错误提示信息为&#xff1a; Error&#xff1a;Cant find temporary directory:internal error 然后试了下其他的SVN源&#xff0c;发现均无法提交&#xff0c;并且update时也出现上面的错误信息。对比项目文件…

snowflake 数据库_Snowflake数据分析教程

snowflake 数据库目录 (Table of Contents) Introduction 介绍 Creating a Snowflake Datasource 创建雪花数据源 Querying Your Datasource 查询数据源 Analyzing Your Data and Adding Visualizations 分析数据并添加可视化 Using Drilldowns on Your Visualizations 在可视化…

jeesite缓存问题

jeesite&#xff0c;其框架主要为&#xff1a; 后端 核心框架&#xff1a;Spring Framework 4.0 安全框架&#xff1a;Apache Shiro 1.2 视图框架&#xff1a;Spring MVC 4.0 服务端验证&#xff1a;Hibernate Validator 5.1 布局框架&#xff1a;SiteMesh 2.4 工作流引擎…

高级Python:定义类时要应用的9种最佳做法

重点 (Top highlight)At its core, Python is an object-oriented programming (OOP) language. Being an OOP language, Python handles data and functionalities by supporting various features centered around objects. For instance, data structures are all objects, …

Java 注解 拦截器

场景描述&#xff1a;现在需要对部分Controller或者Controller里面的服务方法进行权限拦截。如果存在我们自定义的注解&#xff0c;通过自定义注解提取所需的权限值&#xff0c;然后对比session中的权限判断当前用户是否具有对该控制器或控制器方法的访问权限。如果没有相关权限…

医疗大数据处理流程_我们需要数据来大规模改善医疗流程

医疗大数据处理流程Note: the fictitious examples and diagrams are for illustrative purposes ONLY. They are mainly simplifications of real phenomena. Please consult with your physician if you have any questions.注意&#xff1a;虚拟示例和图表仅用于说明目的。 …

What's the difference between markForCheck() and detectChanges()

https://stackoverflow.com/questions/41364386/whats-the-difference-between-markforcheck-and-detectchanges转载于:https://www.cnblogs.com/chen8840/p/10573295.html

ASP.NET Core中使用GraphQL - 第七章 Mutation

ASP.NET Core中使用GraphQL - 目录 ASP.NET Core中使用GraphQL - 第一章 Hello WorldASP.NET Core中使用GraphQL - 第二章 中间件ASP.NET Core中使用GraphQL - 第三章 依赖注入ASP.NET Core中使用GraphQL - 第四章 GrahpiQLASP.NET Core中使用GraphQL - 第五章 字段, 参数, 变量…

POM.xml红叉解决方法

方法/步骤 1用Eclipse创建一个maven工程&#xff0c;网上有很多资料&#xff0c;这里不再啰嗦。 2右键maven工程&#xff0c;进行更新 3在弹出的对话框中勾选强制更新&#xff0c;如图所示 4稍等片刻&#xff0c;pom.xml的红叉消失了。。。

JS前台页面验证文本框非空

效果图&#xff1a; 代码&#xff1a; 源代码&#xff1a; <script type"text/javascript"> function check(){ var xm document.getElementById("xm").value; if(xm null || xm ){ alert("用户名不能为空"); return false; } return …

python对象引用计数器_在Python中借助计数器对象对项目进行计数

python对象引用计数器前提 (The Premise) When we deal with data containers, such as tuples and lists, in Python we often need to count particular elements. One common way to do this is to use the count() function — you specify the element you want to count …

套接字设置为(非)阻塞模式

当socket 进行TCP 连接的时候&#xff08;也就是调用connect 时&#xff09;&#xff0c;一旦网络不通&#xff0c;或者是ip 地址无效&#xff0c;就可能使整个线程阻塞。一般为30 秒&#xff08;我测的是20 秒&#xff09;。如果设置为非阻塞模式&#xff0c;能很好的解决这个…

经典问题之「分支预测」

问题 来源 &#xff1a;stackoverflow 为什么下面代码排序后累加比不排序快&#xff1f; public static void main(String[] args) {// Generate dataint arraySize 32768;int data[] new int[arraySize];Random rnd new Random(0);for (int c 0; c < arraySize; c)data…

vi

vi filename :打开或新建文件&#xff0c;并将光标置于第一行首 vi n filename &#xff1a;打开文件&#xff0c;并将光标置于第n行首 vi filename &#xff1a;打开文件&#xff0c;并将光标置于最后一行首 vi /pattern filename&#xff1a;打开文件&#xff0c;并将光标置…

数字图像处理 python_5使用Python处理数字的高级操作

数字图像处理 pythonNumbers are everywhere in our daily life — there are phone numbers, dates of birth, ages, and other various identifiers (driver’s license and social security numbers, for example).电话号码在我们的日常生活中无处不在-电话号码&#xff0c;…

05精益敏捷项目管理——超越Scrum

00.我们不是不知道它会给我们带来麻烦&#xff0c;只是没想到麻烦会有这么多。——威尔.罗杰斯 01.知识点&#xff1a; a.Scrum是一个强大、特意设计的轻量级框架&#xff0c;器特性就是将软件开发中在制品的数量限制在团队层级&#xff0c;使团队有能力与业务落班一起有效地开…

带标题的图片轮询展示

为什么80%的码农都做不了架构师&#xff1f;>>> <div> <table width"671" cellpadding"0" cellspacing"0"> <tr height"5"> <td style"back…