本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!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的更多相关文章

  1. Spring(五)AOP简述

    一.AOP简述 AOP全称是:aspect-oriented programming,它是面向切面编号的思想核心, AOP和OOP既面向对象的编程语言,不相冲突,它们是两个相辅相成的设计模式型 AOP ...

  2. 跟着刚哥学习Spring框架--AOP(五)

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

  3. Spring框架第五篇之Spring与AOP

    一.AOP概述 AOP(Aspect Orient Programming),面向切面编程,是面向对象编程OOP的一种补充.面向对象编程是从静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运 ...

  4. Spring 学习十五 AOP

    http://www.hongyanliren.com/2014m12/22797.html 1: 通知(advice): 就是你想要的功能,也就是安全.事物.日子等.先定义好,在想用的地方用一下.包 ...

  5. Spring学习之旅(五)--AOP

    什么是 AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是 OOP(Object-Oriented Programing,面向对象编程)的补充和完善. OO ...

  6. Spring实现AOP的4种方式

    了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程序能够应用通知的一个“时机”,这些“ ...

  7. spring的AOP

    最近公司项目中需要添加一个日志记录功能,就是可以清楚的看到谁在什么时间做了什么事情,因为项目已经运行很长时间,这个最初没有开来进来,所以就用spring的面向切面编程来实现这个功能.在做的时候对spr ...

  8. Spring 实践 -AOP

    Spring 实践 标签: Java与设计模式 AOP引介 AOP(Aspect Oriented Programing)面向切面编程采用横向抽取机制,以取代传统的纵向继承体系的重复性代码(如性能监控 ...

  9. Spring实现AOP的4种方式(转)

    转自:http://blog.csdn.net/udbnny/article/details/5870076 Spring实现AOP的4种方式 先了解AOP的相关术语:1.通知(Advice):通知定 ...

  10. spring(二) AOP之AspectJ框架的使用

    前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...

随机推荐

  1. [android]从书中编码方式,看编程思想

    <Android权威编程指南第3版>今天学习到第10章,感叹经验丰富的编程理念——解耦合. 编程理念肯定是尽可能解除耦合,让代码可以复用,书中多次提到关于参数传递的解耦,这又出现一次. 本 ...

  2. 幕布,workflowy的使用技巧

    Q: 幕布免费用户导出文档为纯文本或opml: - 将文档Ctrl+C 复制到workflowy: - workflowy可以导出plain-text或opml: 注:已知这样的方法,注释的格式不会被 ...

  3. 微信小程序接入LeanCloud

    大家在做小程序或者客户端开发的时候肯定会想使得数据进行联网,但这样就必须有对应的后台服务器以及数据库,再加上linux运维等各种细节,往往会对新手比较劝退,在这里给大家推荐一种bass(后端即服务), ...

  4. js函数的三种成创建方式以及它们各自的不同

    js有三种创建函数的方式: 1.function语句(也叫函数声明) function sum(a, b) { return a + b; } sum(1, 2); // 3 2. 函数直接量,又叫函 ...

  5. juery 弹出框

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. git删除已经提交的包含敏感信息的文件(还没提交到远程仓库)

    写好的代码已经提交了(但还没push到github),发现某个文件里包含密码.如果push的话,密码可就被公开了.如果在代码里改掉密码,再commit一次,也不行,历史提交记录还是会上传到github ...

  7. QT使用信号量QSemaphore处理大量数据

    实现如下:

  8. (转)协议森林06 瑞士军刀 (ICMP协议)

    协议森林06 瑞士军刀 (ICMP协议) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 到现在为止,我们讲解了网络层中最重要的I ...

  9. [C++]那些年被虐的STL

    首先很感谢**P1135奇怪的电梯 **[2.14补充:此题已被AC!然后将被我花式虐[From语]哈哈哈哈哈哈哈哈哈哈好嗨哟感觉人生已经到达了巅峰感觉人生已经到达了高潮]这道题了!在做这道题的我大致 ...

  10. System.Text.Json 序列化对所有 JSON 属性名称使用 camel 大小写

    asp.net core3.x 新增的序列号接口System.Text.Json 序列化时,如果要对所有 JSON 属性名称使用 camel 大小写 将 JsonSerializerOptions.P ...