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.一篇讲解不完,所以 ... 
随机推荐
- 学习ConcurrentHashMap1.7分段锁原理
			1. 概述 接上一篇 学习 ConcurrentHashMap1.8 并发写机制, 本文主要学习 Segment分段锁 的实现原理. 虽然 JDK1.7 在生产环境已逐渐被 JDK1.8 替代,然而一 ... 
- Nginx 入门及基本命令行操作
			Nginx 介绍 Nginx 是一个高性能的 Web 服务器,从 2001 年发展至今,由于 Nginx 对硬件和操作系统内核特性的深度挖掘,使得在保持高并发的同时还能够保持高吞吐量.Nginx 还采 ... 
- Linux命令进阶篇-文件查看与查找
			上一篇的博客对于Linux如何在不同目录下跳转和查看目录下内容做出了总结,主要靠cd和ls,很常见也很实用.但是你看到目录下面那么多不同花花绿绿的文件,心里是不是痒痒,是不是想进去一探究竟,有办法! ... 
- 如何查看QQ坦白说来自谁
			近两天QQ新功能的坦白说开始席卷朋友圈,一个醒目的小窗就这样明晃晃出现在QQ对话列表"有人对你说:--"下面我们就来整理一下怎么看到是谁给你发送的坦白说呢? 方法一: 此方法仅限于 ... 
- Mol Cell Proteomics. | MARMoSET – Extracting Publication-ready Mass Spectrometry Metadata from RAW Files
			本文是马克思普朗克心肺研究所的三名研究者Marina Kiweler.Mario Looso和Johannes Graumann发表在8月刊的MCP的一篇文章. 由于Omics实验经常涉及数百个数据文 ... 
- 物联网 软硬件系统  树莓派  单片机  esp32  小程序  网页 开发  欢迎相互交流学习~
			物联网软硬件开发 知识分享 多年学生项目开发经验 物联网 软硬件系统 树莓派 单片机 esp32 小程序 网页 开发 欢迎相互交流学习~ http://39.105.218.125:9000/ 
- (2)Windows PowerShell使用
			什么是PowerShell: Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework 的强大功能.PowerShell是命 ... 
- BIT-Reverse Pairs
			2019-12-17 11:07:02 问题描述: 问题求解: 本题可以看作是逆序数问题的强化版本,需要注意的是num[i] > 2 * num[j],这里有0和负数的情况. public in ... 
- Layui-admin-iframe通过页面链接直接在iframe内打开一个新的页面,实现单页面的效果
			前言: 使用Layui-admin做后台管理框架有很长的一段时间了,但是一直没有对框架内iframe菜单栏切换跳转做深入的了解.今天有一个这样的需求就是通过获取超链接中传递过来的跳转地址和对应的tab ... 
- Oracle数据库开机自启动的配置
			如果服务器断电重启或计划内重启,在服务器的操作系统启动后,需要手工启动数据库实例和监听,本文介绍如何把Oracle数据库的启动和关闭配置成系统服务,在操作系统启动/关闭时,自动启动/关闭Oracle实 ... 
 
			
		