在前两篇博客中,介绍了AOP实现的基础:静态代理和动态代理,这篇博客介绍spring中AOP的实现。

一、采用Annotation方式

首先引入jar包:aspectjrt.jar && aspectweaver.jar

applicationContext配置文件:

<span style="font-family:KaiTi_GB2312;font-size:18px;"><?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 启用AspectJ对Annotation的支持 -->
<span style="color:#ff0000;"><aop:aspectj-autoproxy/></span> <bean id="user" class="com.angel.spring.User"/> <bean id="successfulHandler" class="com.angel.spring.SuccessfulHandler"/> </beans>
</span>

接口和实现类省略

代理拦截类:

<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.angel.spring;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; @Aspect
public class SuccessfulHandler { /**
* 定义Pointcut,Pointcut的名称为addAddMethod(),此方法没有返回值和参数
* 该方法就是一个标识,不进行调用
*/
@Pointcut("execution(* add*(..))")
private void addAddMethod(){}; /**
* 定义Advice,表示我们的Advice应用到哪些Pointcut订阅的Joinpoint上
*/
//@Before("addAddMethod()")
@After("addAddMethod()")
private void checkSecurity() {
System.out.println("-------Angel,方法执行成功!-------");
}
}
</span>

通过以上的配置,就可以实现对于方法调用的拦截。我们使用的注解有@Aspect,标记此类是一个横切面的插入类(拦截类),然后使用@Pointcut,标记我们的拦截方法需要作用于那些地方,最后使用@Before或者@After标记我们的拦截方法是作用于被调用方法前或者后。

但是,通过注解的方式,我们就需要为每个方法都添加注解,如果要修改的话,还得逐一修改,这样子,虽然同样轻便,但是如果有一个地方统一配置AOP的话,那就会更为简便,接下来,我们看第二种实现方式:采用配置方式

二、采用配置方式

applicationContext的配置:

<span style="font-family:KaiTi_GB2312;font-size:18px;"><?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 启用AspectJ对Annotation的支持 -->
<!-- <aop:aspectj-autoproxy/> --> <bean id="user" class="com.angel.spring.User" /> <bean id="successfulHandler" class="com.angel.spring.SuccessfulHandler" /> <aop:config>
<aop:aspect id="IsSuccessfulAspect" ref="successfulHandler">
<!-- com.bjpowernode.spring包下所有的类所有的方法
<aop:pointcut id="addAddMethod" expression="execution(* com.bjpowernode.spring.*.*(..))"/> -->
<!-- com.bjpowernode.spring包下所有的类的以add和del开头的方法-->
<aop:pointcut id="addAddMethod" expression="execution(* com.angel.spring.*.add*(..)) || execution(* com.angel.spring.*.del*(..))" />
<aop:before method="checkSecurity" pointcut-ref="addAddMethod" />
</aop:aspect>
</aop:config> </beans>
</span>

拦截类:

<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.angel.spring;

import org.aspectj.lang.JoinPoint;

public class SuccessfulHandler {

	private void checkSecurity(JoinPoint joinPoint) {

		System.out.println("-------Angel,方法执行成功!-------");
}
}</span>

经过这样的配置就可以实现方法的拦截了,但有时候,我们不仅需要拦截方法名称,我们还需要知道,在执行这个方法的时候,用户到底传递了什么样的参数,那么,我们需要在拦截类采用Advice中添加一个JoinPoint参数,取得客户端调用的方法名称及参数值,如:

<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.angel.spring;

import org.aspectj.lang.JoinPoint;

public class SuccessfulHandler {

	private void checkSecurity(JoinPoint joinPoint) {
for (int i=0; i<joinPoint.getArgs().length; i++) {
System.out.println(joinPoint.getArgs()[i]);
}
System.out.println(joinPoint.getSignature().getName());
System.out.println("-------Angel,方法执行成功!-------");
}
}</span>

三、采用CGLIB代理

以上的例子,我们都是采用JDK的代理实现的AOP,JDK的动态代理是代理的接口,如果有些类并没有实现接口,那么我们将不得不采用另外一种代理方式:CGLIB动态代理。如:

首先,引入支撑CGLIB的jar包cglib-nodep.jar

其次,在applicationContext配置文件中,强制开启CGLIB代理:

<span style="font-family:KaiTi_GB2312;font-size:18px;"><!-- 强制使用CGLIB代理 -->
<aop:aspectj-autoproxy proxy-target-class="true" /></span>

区别:User类不再实现IUser接口,而是一个单独的类,代理成功!

四、总结

JDK的动态代理机制只能代理实现了接口的类,而不能实现接口的类就不能实现JDK的动态代理,cglib是针对类来实现代理的,他的原理是对指定的目标类生成一个子类,并覆盖其中方法实现增强,但因为采用的是继承,所以不能对final修饰的类进行代理。

对于AOP的总结,到此结束,接下来总结事务的知识点。PS:深刻感觉,之前对于AOP是被吓退了!

【spring 5】AOP:spring中对于AOP的的实现的更多相关文章

  1. Spring框架中的aop操作之二 通过配置文件实现增强

    aop表达式写法 配置文件代码: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...

  2. 【Java】Spring之面向方面编程(AOP)(五)

    面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP).OOP中模块化的关键单元是类,而在AOP中,模块化单元是方面.方面实现了跨越多种类型和对象的关注点(例如事务管理)的 ...

  3. Spring Aop(一)——Aop简介

    转发地址:https://www.iteye.com/blog/elim-2394629 1 Aop简介 AOP的全称是Aspect Oriented Programming,翻译成中文是面向切面编程 ...

  4. Spring基础(二)_面向切面(AOP)

    面向切面编程 面向切面编程[AOP,Aspect Oriented Programming]:通过预编译方式和运行期间动态代理实现程序功能的统一维护的技术.AOP 是 Spring 框架中的一个重要内 ...

  5. Spring学习笔记-面向切面(AOP)-04

    什么是面向切面编程 先大概了解一下部分术语 横切关注点:软件开发中,散布于多出的功能称为横切关注点(cross-cutting concern),简单的可以描述为可以影响应用多处的功能,比如日志.安全 ...

  6. 转-Spring Framework中的AOP之around通知

    Spring Framework中的AOP之around通知 http://blog.csdn.net/xiaoliang_xie/article/details/7049183 标签: spring ...

  7. Spring学习(二)——Spring中的AOP的初步理解[转]

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  8. Spring中的AOP

    什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...

  9. Spring中的AOP开发

    1.代理模式 找个人将你原本想做的事情给做了. 三个部分组成: 抽象主题角色:真实主题和代理主题的共同接口. 真实主题角色:定义了代理角色所代表的真实对象. 代理主题角色:含有对真实主题角色的引用.代 ...

随机推荐

  1. 一个SQLSERVER触发器的示例

    CREATE TRIGGER WoStateChange on T_PD_WorkOrder AFTER UPDATE AS BEGIN declare @WorkOrderID varchar(20 ...

  2. iphone Dev 开发实例10:How To Add a Slide-out Sidebar Menu in Your Apps

    Creating the Xcode Project With a basic idea about what we’ll build, let’s move on. You can create t ...

  3. jQuery插件:用于获取元素自身的HTML内容

    jQuery.fn.outerHTML = function(s) { return (s) ? this.before(s).remove() : $("<Hill_man>& ...

  4. (转)HelloWorld CMake CMake中构建静态库与动态库及其使用

    继续完善Hello World,建立它的共享库, 包括静态库和动态库. 本节的任务: 1,建立一个静态库和动态库,提供HelloFunc函数供其他程序编程使用,HelloFunc 向终端输出Hello ...

  5. AP_AP系列 - 发票管理分析(案例)

    2014-07-07 Created By BaoXinjian

  6. nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)

    启动nginx时报这个错 , 要么用root用户启动 , 要么在配置文件nginx.conf中将server下的listen端口改掉 , 因为在linux中端口号小于1024都是需要root权限的

  7. Package 'chkconfig' has no installation candidate

    Chkcofig不再适用于Ubuntu系统,可用类似的软件sysv-rc-conf进行替换: Chkconfig is no longer available in Ubuntu. Chkconfig ...

  8. [ActionScript 3.0] as3.0加载as2.0的swf时获取as2.0的实际舞台尺寸

    var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler) ...

  9. maven项目导入报错

    极大可能是仓库设置问题

  10. java中的xml与实体类之间的映射

    实体类: package xml; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class User ...