Spring (五):AOP
本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!b站搜索狂神说或点击下面链接
https://space.bilibili.com/95256449?spm_id_from=333.788.b_765f7570696e666f.2
AOP
定义:
AOP (Aspect Oriented Programming) :面向切面编程
个人理解其实就是spring下的动态代理模式,说白了就是不影响原代码的情况下,横向增加代码扩充功能的设计思想.
通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容。是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
官方文档定义个人理解说明:
横切关注点:跨越应用程序多个模块的方法或功能。
切面(ASPECT) :横切关注点被模块化的特殊对象。
我们要自定义一个类,里面写上代理对象附加要实现的内容,这个类就是切面。
通知(Advice) :切面必须要完成的工作。
代理对象附加要实现的内容
目标(Target) :被通知对象。
真实对象。
代理(Proxy) :向目标对象应用通知之后创建的对象。
代理对象,Spring AOP封装了代理对象的生成,我们不用管怎么生成的。
切入点(PointCut) :切面通知执行的“地点”的定义。
什么地方要执行代理对象的方法,比如定义某个类,那么这个类的所有方法都会执行代理方法。
连接点UointPoint) :与切入点匹配的执行点。
导入依赖
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
方式一:使用原生Spring API接口
需要注入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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
真实对象接口
package com.rzp.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
真实对象
package com.rzp.service;
//真实对象
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加用户");
}
public void delete() {
System.out.println("删除用户");
}
public void update() {
System.out.println("更新用户");
}
public void query() {
System.out.println("查询用户");
}
}
LOG类
package com.rzp.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
//继承MethodBeforeAdvice,重写的before方法会在真实对象方法执行前会执行
public class Log implements MethodBeforeAdvice {
//method 要执行的目标对象的方法
//Object[] 参数
//target 目标对象
public void before(Method method, Object[] objects, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行了");
}
}
afterLog类
package com.rzp.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
//继承AfterReturningAdvice,重写的afterReturning会在真实对象方法执行后执行
public class AfterLog implements AfterReturningAdvice {
//returnValue 返回值
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"返回结果为"+returnValue);
}
}
xml
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.rzp.service.UserServiceImpl"/>
<bean id="log" class="com.rzp.log.Log"/>
<bean id="afterlog" class="com.rzp.log.AfterLog"/>
<!--方式一,使用原生Spring API接口-->
<!--配置aop-->
<aop:config>
<!--定义切入点,在什么地方切入 expression:表达式 executiion(要执行的位置)-->
<!--1、execution(): 表达式主体。
2、第一个*号:表示返回类型,*号表示所有的类型。
3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子孙包下所有类的方法。
4、第二个*号:表示类名,*号表示所有的类。
5、*(..):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数-->
<aop:pointcut id="pointc" expression="execution(* com.rzp.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointc"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="pointc"/>
</aop:config>
</beans>
测试
import com.rzp.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mytest {
public static void main(String[] args) {
ApplicationContext contest = new ClassPathXmlApplicationContext("beans.xml");
UserService user = contest.getBean("userService", UserService.class);
user.add();
}
}

方式二:使用自定义类来实现AOP
自定义类
package com.rzp.diy;
public class DiyPointCut {
public void before(){
System.out.println("方法执行前");
}
public void after(){
System.out.println("方法执行后");
}
}
xml
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.rzp.service.UserServiceImpl"/>
<bean id="log" class="com.rzp.log.Log"/>
<bean id="afterlog" class="com.rzp.log.AfterLog"/>
<!--方式二,使用自定义类-->
<bean id="diy" class="com.rzp.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面,ref引用的类-->
<aop:aspect ref="diy">
<aop:pointcut id="point" expression="execution(* com.rzp.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
测试
import com.rzp.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mytest {
public static void main(String[] args) {
ApplicationContext contest = new ClassPathXmlApplicationContext("beans.xml");
UserService user = contest.getBean("userService", UserService.class);
user.add();
}
}
没有方式一好,因为方式一还能通过反射获取到真实对象的信息。
方式三:使用注解实现
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="userService" class="com.rzp.service.UserServiceImpl"/>
<bean id="log" class="com.rzp.log.Log"/>
<bean id="afterlog" class="com.rzp.log.AfterLog"/> <!--方式三:使用注解开发-->
<context:component-scan base-package="com.rzp.*"/>
<context:annotation-config/>
<!--开启注解AOP支持,JDK5 默认proxy-target-class="false" Cglib proxy-target-class="true"-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
自定义类
package com.rzp.diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect //标注这个类是一个切面
@Component
public class AnnotationPointCut {
@Before("execution(* com.rzp.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("before");
}
@After("execution(* com.rzp.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("after");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
@Around("execution(* com.rzp.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
//获得签名--其实就是执行了真实对象的什么方法
Signature signature = jp.getSignature();
System.out.println("sign"+signature);
//执行方法
Object proceed = jp.proceed();
System.out.println("环绕后");
System.out.println(proceed);
}
}
其他不变,测试结果

Spring (五):AOP的更多相关文章
- Spring(五)AOP简述
一.AOP简述 AOP全称是:aspect-oriented programming,它是面向切面编号的思想核心, AOP和OOP既面向对象的编程语言,不相冲突,它们是两个相辅相成的设计模式型 AOP ...
- 跟着刚哥学习Spring框架--AOP(五)
AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...
- Spring框架第五篇之Spring与AOP
一.AOP概述 AOP(Aspect Orient Programming),面向切面编程,是面向对象编程OOP的一种补充.面向对象编程是从静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运 ...
- Spring 学习十五 AOP
http://www.hongyanliren.com/2014m12/22797.html 1: 通知(advice): 就是你想要的功能,也就是安全.事物.日子等.先定义好,在想用的地方用一下.包 ...
- Spring学习之旅(五)--AOP
什么是 AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是 OOP(Object-Oriented Programing,面向对象编程)的补充和完善. OO ...
- Spring实现AOP的4种方式
了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程序能够应用通知的一个“时机”,这些“ ...
- spring的AOP
最近公司项目中需要添加一个日志记录功能,就是可以清楚的看到谁在什么时间做了什么事情,因为项目已经运行很长时间,这个最初没有开来进来,所以就用spring的面向切面编程来实现这个功能.在做的时候对spr ...
- Spring 实践 -AOP
Spring 实践 标签: Java与设计模式 AOP引介 AOP(Aspect Oriented Programing)面向切面编程采用横向抽取机制,以取代传统的纵向继承体系的重复性代码(如性能监控 ...
- Spring实现AOP的4种方式(转)
转自:http://blog.csdn.net/udbnny/article/details/5870076 Spring实现AOP的4种方式 先了解AOP的相关术语:1.通知(Advice):通知定 ...
- spring(二) AOP之AspectJ框架的使用
前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...
随机推荐
- Drf(DjangoRestFramewok)
第一部分 问题 1.前后端分离? vue.js 后端给前段返回json数据 2.移动端盛行. app 后端给app返回json数据 3.PC端应用? crm项目,前段后端一起写,运行在浏览器上. 一般 ...
- Vue2.0 【第三季】第2节 computed Option 计算选项
目录 Vue2.0 [第三季]第2节 computed Option 计算选项 第2节 computed Option 计算选项 一.格式化输出结果 二.用计算属性反转数组 Vue2.0 [第三季]第 ...
- 原来rollup这么简单之 rollup.rollup篇
大家好,我是小雨小雨,致力于分享有趣的.实用的技术文章. 内容分为翻译和原创,如果有问题,欢迎随时评论或私信,希望和大家一起进步. 分享不易,希望能够得到大家的支持和关注. 计划 rollup系列打算 ...
- html5 cavans的小应用
1.canvas鼠标画线,canvas小方块移动,canvas小方块旋转并缩放 <!doctype html> <html> <head> <meta cha ...
- CSS的SVG学习
SVG 意为可缩放矢量图形(Scalable Vector Graphics). HTML三种方法导入svg文件: <html xmlns:svg="http://www.w3.org ...
- 数据挖掘入门系列教程(五)之Apriori算法Python实现
数据挖掘入门系列教程(五)之Apriori算法Python实现 加载数据集 获得训练集 频繁项的生成 生成规则 获得support 获得confidence 获得Lift 进行验证 总结 参考 数据挖 ...
- 远程调试docker构建的weblogic
环境信息 OSType: CentOS Linux 7 (Core) x86_64 3.10.0-957.21.3.el7.x86_64 DockerVersion: 19.03.8 Mirrors: ...
- [转载] 全局键盘钩子(WH_KEYBOARD)
为了显示效果,在钩子的DLL中我们会获取挂钩函数的窗体句柄,这里的主程序窗体名为"TestMain",通过FindWindow查找. KeyBoardHook.dll代码 libr ...
- ASP.NET Core 3.x 中间件流程与路由体系
中间件分类 ASP.NET Core 中间件的配置方法可以分为以上三种,对应的Helper方法分别是:Run(), Use(), Map(). Run(),使用Run调用中间件的时候,会直接返回一个响 ...
- django 之 Ajax and so on
MTV与MVC(了解)
