一、AOP是什么?

AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。
AOP核心概念

  • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等…
  • 切面(ASPECT):横切关注点被模块化的特殊对象。即,它是一个类。
  • 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
  • 目标(Target):被通知对象。
  • 代理(Proxy) :向目标对象应用通知之后创建的对象。
  • 切入点(Pointcut) :切面通知执行的“地点"的定义
  • 连接点(JointPoint):与切入点匹配的执行点。

二、代码

1.需要的类

业务的接口

package com.shan.demo04;public interface UserService {void add();void delete();void update();void query();}

业务的实现类

package com.shan.demo02;
//改变业务代码在公司是大忌,因为你改动代码可能让整个项目崩溃
public class UserServiceImpl implements UserService{@Overridepublic void add() {System.out.println("增加了一个用户");}@Overridepublic void delete() {System.out.println("删除了一个用户");}@Overridepublic void update() {System.out.println("修改了一个用户");}@Overridepublic void query() {System.out.println("查询了一个用户");}
}

前置日志类

package com.shan.log;import org.springframework.aop.MethodBeforeAdvice;import java.lang.reflect.Method;public class BeforeLog implements MethodBeforeAdvice {//method:要执行的目标对象的方法// Object[]:参数// target:目标对象public void before(Method method, Object[] args, Object target) throws Throwable {System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了!");}
}

后置日志类

package com.shan.log;import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;public class AfterLog implements AfterReturningAdvice {//returnValue:返回值//method:要执行的目标对象的方法// Object[]:参数// target:目标对象public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {System.out.println("执行了!"+target.getClass().getName()+"的"+method.getName()+"方法.返回结果为:"+returnValue);}
}

自定义切入点

package com.shan.diy;public class DiyPointCut {public void before(){System.out.println("=================方法执行前==================");}public void after(){System.out.println("=================方法执行后==================");}
}

注解实现AOP

package com.shan.diy;import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;@Aspect  //标注这个类是一个切面
public class AnnotationPointCut {@Before("execution(* com.shan.service.UserServiceImpl.*(..))")public void before(){System.out.println("=====方法执行前=====");}@After("execution(* com.shan.service.UserServiceImpl.*(..))")public void after(){System.out.println("=====方法执行后=====");}}

Spring xml配置文件

<?xml version="1.0" encoding="UTF8" ?>
<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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--注册bean--><bean id="userService" class="com.shan.service.UserServiceImpl"/><bean id="beforeLog" class="com.shan.log.BeforeLog"/><bean id="afterLog" class="com.shan.log.AfterLog"/><!--配置aop方式2,自定义类--><bean id="diy" class="com.shan.diy.DiyPointCut"/><!--3,注解--><bean id="annotation" class="com.shan.diy.AnnotationPointCut"/><!--方式3,开启注解支持--><aop:aspectj-autoproxy/><!--配置aop方式2,自定义类--><aop:config><!--自定义切面,ref:要引用的类--><aop:aspect ref="diy"><!--切入点--><aop:pointcut id="point" expression="execution(* com.shan.service.UserServiceImpl.*(..))"/><aop:before method="before" pointcut-ref="point"/><aop:after method="after" pointcut-ref="point"/></aop:aspect></aop:config><aop:config><!--切入点--><aop:pointcut id="pointcut" expression="execution(* com.shan.service.UserServiceImpl.*(..))"/><!--执行环绕增加--><aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/><aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/></aop:config></beans>

2.测试

import com.shan.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {@Testpublic void diyPointCutTest(){ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//动态代理 代理的是接口UserService userService = context.getBean("userService", UserService.class);userService.add();}@Testpublic void annotationPointCutTest(){ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//动态代理 代理的是接口UserService userService = context.getBean("userService",UserService.class);userService.add();}
}
> =================方法执行前==================
> com.shan.service.UserServiceImpl的add被执行了!
> =====方法执行前=====
> 增加了一个用户
> =====方法执行后=====
> 执行了!com.shan.service.UserServiceImpl的add方法.返回结果为:null
> =================方法执行后==================

总结

AOP的实现方式:

  • 默认的执行环绕
  • 通过AOPconfig配置自定义的切入面,切入点,引用的类
  • 注解实现AOP

对比有优先级,哪个config在上面,before就最先执行,after就最后执行.这两种方式可以同时存在,个人发现对比有不同!

作者有话说

博客创作不易,希望看到这里的读者动动你的小手点个赞,如果喜欢的小伙伴可以一键三连,作者大大在这里给大家谢谢了。

Spring学习12之AOP2相关推荐

  1. spring学习12 -Spring 框架模块以及面试常见问题注解等

    以下为spring常见面试问题: 1.Spring 框架中都用到了哪些设计模式? Spring框架中使用到了大量的设计模式,下面列举了比较有代表性的: 代理模式-在AOP和remoting中被用的比较 ...

  2. Spring学习12之整合Mybatis

    前言 Spring两大核心,IOC,AOP. 一.整合Mybatis 1.编写数据源配置 2.sqlSessionFactory 3.sqlSessionTemplate 4.需要给接口加实现类 5. ...

  3. Spring学习(一)初识Spring

    本文借鉴:Spring学习(特此感谢!) 一.简介 什么是Spring 定义:Spring 是一个轻量级的 DI / IoC 和 AOP 容器的开源框架,目的为了简化java开发. DI:注入 IOC ...

  4. spring学习笔记06-spring整合junit(出现的问题,解决的思路)

    spring学习笔记06-spring整合junit(出现的问题,解决的思路) 文章目录 spring学习笔记06-spring整合junit(出现的问题,解决的思路) 3.1测试类中的问题和解决思路 ...

  5. 【spring学习】02

    [spring学习]02 spring快速入门 实例 Spring配置文件 Bean标签的基本配置 Bean标签的范围配置 Spring 依赖注入 Spring引入其他配置文件 spring配置文件总 ...

  6. Spring学习笔记之MyBatis

    系列文章目录 Spring学习笔记 之 Springhttps://blog.csdn.net/weixin_43985478/article/details/124411746?spm=1001.2 ...

  7. Spring 学习笔记----->AOP

    Spring 学习笔记----->AOP 代理模式 为什么学代理模式? 因为这就是Spring Aop的底层 代理模式的分类: 静态代理 动态代理 静态代理 生活用的例子: 房东 public ...

  8. Spring学习中使用javaConfig进行配置时出现 has not been refreshed yet错误

    Spring学习中使用javaConfig进行配置时出现 has not been refreshed yet错误 java.lang.IllegalStateException: org.sprin ...

  9. Spring——Spring学习教程(详细)(上篇)——IOC、AOP

    本文是Spring的学习上篇,主要讲IOC和AOP. Spring的JDBCTemplete以及事务的知识,请见下篇. Spring--Spring学习教程(详细)(下篇)--JDBCTemplete ...

最新文章

  1. 十、springboot注解式AOP(@Aspect)统一日志管理
  2. 2021-05-21 深入理解SLAM技术 【4】射影几何--2面中心射影
  3. html5 webview,HTML5+学习历程之webview经典案例
  4. ARM汇编Hello,World
  5. linux下的svn搭建,Ubuntu 14.04 下搭建SVN服务器 svn://
  6. AVRNET 学习笔记UDP部分
  7. WWDC20中iOS的改变
  8. adb命令刷机vivox20_求救VIVO X20的 ROOT可行的方法。
  9. gaussian窗口函数_常用窗函数的特点
  10. java 语音传输_java – 通过tcp流式传输语音
  11. 【技巧】vscode快速生成html结构
  12. java制作名片applet程序_【小程序 提取码:krua】壹佰智能名片小程序版本V1.1.45 – 持续更新 无后门...
  13. java项目笔记 - 第16章:坦克大战1.0
  14. 树莓派做网络代理_树莓派使用Proxy代理
  15. 最小均方算法二分类(基于双月数据集)
  16. Kotlin:所有的一切还是从Hello Kotlin开始
  17. 4.python 系统批量运维管理器之paramiko模块
  18. 抽象类 [Java]
  19. 什么是CCNA?(及相关概念)
  20. 【ASML】EUV光刻技术PPT

热门文章

  1. php 多用户 判断,Laravel jwt 多表(多用户端)验证隔离的实现
  2. c语言单链表_C语言笔试题—单链表逆序
  3. Plotly绘制树状热力图(treemap)【Plotly实例教程】
  4. java高级工程师开放面试题集二
  5. solr/lucence和关系数据库的混合使用
  6. 李宏毅深度学习——Why Deep?
  7. Anaconda 镜像使用帮助
  8. 深度学习、自然语言处理和表征方法
  9. 我早年在Google学到的10条经验
  10. 实战并发编程 - 02解决并发问题常用套路