AOP概念:

1 、什么是 AOP
(1)面向切面编程(方面),利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
(2)通俗描述:不通过修改源代码方式,在主干功能里面添加新功能。
(3)使用登录例子说明 AOP。

AOP底层原理:

1 、AOP 底层使用动态代理
(1)有两种情况动态代理
第一种 第一种 有接口情况,使用 JDK 动态代理
⚫ 创建接口实现类代理对象,增强类的方法

第二种 没有接口情况,使用 CGLIB 动态代理
⚫ 创建子类的代理对象,增强类的方法

AOP(JDK动态代理)

1、使用 JDK 动态代理,使用 Proxy 类里面的方法创建代理对象;

(1)调用 newProxyInstance 方法

方法有三个参数:
第一参数,类加载器;
第二参数,增强方法所在的类,这个类实现的接口,支持多个接口;
第三参数,实现这个接口 InvocationHandler,创建代理对象,写增强的部分;
2 、编写 JDK 动态代理代码
(1)创建接口,定义方法

package com.atguigu.spring5;/*** @author GGBond* @create 2021-04-01-13:40*/
public interface UserDao {public int add(int a,int b);public String update(String id);
}

(2)创建接口实现类,实现方法

package com.atguigu.spring5;/*** @author GGBond* @create 2021-04-01-13:42*/
public class UserDaoImpl implements UserDao{@Overridepublic int add(int a, int b) {return a+b;}@Overridepublic String update(String id) {return id;}
}

(3)使用 Proxy 类创建接口代理对象

package com.atguigu.spring5;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;/*** @author GGBond* @create 2021-04-01-13:43*/
public class JDKProxy {public static void main(String[] args) {//创建接口实现类代理对象Class[] interfaces = {UserDao.class};
//        Proxy.newProxyInstance(JDKProxy.class.getClassLoader()
//                , interfaces, new InvocationHandler() {//                    @Override
//                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {//                        return null;
//                    }
//                });UserDaoImpl userDao = new UserDaoImpl();UserDao dao = (UserDao)Proxy.newProxyInstance(JDKProxy.class.getClassLoader(),interfaces,new UserDaoProxy(userDao));int result = dao.add(1,2);System.out.println("result:"+result);}
}
//创建代理对象代码
class UserDaoProxy implements InvocationHandler{//把创建的是谁的代理对象,把谁传递过来//有参数构造传递private Object obj;public UserDaoProxy(Object obj){this.obj = obj;}@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {//方法之前System.out.println("方法之前执行。。。"+method.getName()+"传递的参数。。。"+Arrays.toString(args));Object res = method.invoke(obj,args);return res;}
}

AOP(术语)

1.连接点:
类里面哪些方法可以被增强,这些方法称为连接点;
2.切入点:
实际被真正增强的方法,成为切入点;
3.通知:
1.实际增强的逻辑部分称为通知(增强);
2.通知有多种类型:
* 前置通知
* 后置通知
* 环绕通知
* 异常通知
*最终通知
4.切面:
是动作:把通知应用到切入点过程;

AOP操作(准备工作)

1 、Spring 框架一般都是基于 AspectJ 实现 AOP 操作
(1)AspectJ 不是 Spring 组成部分,独立 AOP 框架,一般把 AspectJ 和 Spirng 框架一起使
用,进行 AOP 操作
2 、基于 AspectJ 实现 AOP 操作
(1)基于 xml 配置文件实现
(2)基于注解方式实现(使用)
3 、在项目工程里面引入 AOP

4 、切入点表达式
(1)切入点表达式作用:知道对哪个类里面的哪个方法进行增强
(2)语法结构: execution([权限修饰符] [返回类型] [类全路径] 方法名称 )
举例 1:对 com.atguigu.dao.BookDao 类里面的 add 进行增强
execution(* com.atguigu.dao.BookDao.add(…))
举例 2:对 com.atguigu.dao.BookDao 类里面的所有的方法进行增强
execution(* com.atguigu.dao.BookDao.* (…))
举例 3:对 com.atguigu.dao 包里面所有类,类里面所有方法进行增强
execution(* com.atguigu.dao.. (…))

AOP操作(AspectJ注解)

1.创建类,在类里面定义方法

public class User {public void add(){System.out.println("add....");}
}

2 、创建增强类(编写增强逻辑)
(1)在增强类里面,创建方法,让不同方法代表不同通知类型

//被增强的类
public class UserProxy {//前置通知public void before(){System.out.println("before...");}
}

3 、进行通知的配置
(1)在 spring 配置文件中,开启注解扫描

 <context:component-scan base-package="com.atguigu.spring5"></context:component-scan>

(2)使用注解创建 User 和 UserProxy 对象

@Component
public class User {@Component
public class UserProxy {

(3)在增强类上面添加注解 @Aspect

//被增强的类
@Component
@Aspect
public class UserProxy {

(4)在 spring 配置文件中开启生成代理对象

 <!--开启Aspect生成代理对象--><aop:aspectj-autoproxy></aop:aspectj-autoproxy>

测试:

 @Testpublic void testAop(){ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");User user = context.getBean("user", User.class);user.add();}

4 、配置不同类型的通知
(1)在增强类的里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置

//被增强的类
@Component
@Aspect
public class UserProxy {//前置通知@Before(value = "execution(* com.atguigu.spring5.User.add(..))")public void before(){System.out.println("before...");}//后置通知(返回通知)@AfterReturning(value = "execution(* com.atguigu.spring5.User.add(..))")public void AfterReturning(){System.out.println("AfterReturning...");}//最终通知@After(value = "execution(* com.atguigu.spring5.User.add(..))")public void after(){System.out.println("after...");}//异常通知@AfterThrowing(value = "execution(* com.atguigu.spring5.User.add(..))")public void AfterThrowing(){System.out.println("AfterThrowing...");}//环绕通知@Around(value = "execution(* com.atguigu.spring5.User.add(..))")public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println("环绕之前...");//被增强的方法执行了proceedingJoinPoint.proceed();System.out.println("环绕之后...");}
}

5.相同的接入点抽取

 //相同的切入点抽取@Pointcut(value = "execution(* com.atguigu.spring5.User.add(..))")public void pointdemo(){}//前置通知@Before(value = "pointdemo()")public void before(){System.out.println("before...");}

6 、有多个增强类多同一个方法进行增强,设置增强类优先级
(1)在增强类上面添加注解 @Order(数字类型值),数字类型值越小优先级越高

@Component
@Aspect
@Order(1)
public class PersonProxy {

7 ,完全使用注解开发
(1)创建配置类,不需要创建xml

@Configuration
@ComponentScan(basePackages = {"com.atguigu"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ConfigAop {}
@Testpublic void test() {ApplicationContext context = new AnnotationConfigApplicationContext(ConfigAop.class);User user = context.getBean("user", User.class);user.add();}

AOP操作(AspectJ配置文件)

1 、创建两个类,增强类和被增强类,创建方法

package com.atguigu.spring5;/*** @author GGBond* @create 2021-04-01-15:48*/
public class Book {public void buy(){System.out.println("buy..........");}
}

2 、在 spring 配置文件中创建两个类对象

package com.atguigu.spring5;/*** @author GGBond* @create 2021-04-01-15:49*/
public class BookProxy {public void before(){System.out.println("before........");}
}

3 、在 spring 配置文件中配置切入点

<!--创建对象--><bean id="book" class="com.atguigu.spring5.Book"></bean><bean id="bookProxy" class="com.atguigu.spring5.BookProxy"></bean><!--配置aop增强--><aop:config><!--切入点--><aop:pointcut id="p" expression="execution(* com.atguigu.spring5.Book.buy(..))"/><!--配置切面--><aop:aspect ref="bookProxy"><!--增强作用在具体的方法上--><aop:before method="before" pointcut-ref="p"></aop:before></aop:aspect></aop:config>
@Testpublic void test2() {ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");Book book = context.getBean("book", Book.class);book.buy();}

Spring框架之AOP相关推荐

  1. 详解Spring框架的AOP机制

    AOP是Spring框架面向切面的编程思想,AOP采用一种称为"横切"的技术,将涉及多业务流程的通用功能抽取并单独封装,形成独立的切面,在合适的时机将这些切面横向切入到业务流程指定 ...

  2. Spring 框架之 AOP 原理深度剖析!|CSDN 博文精选

    作者 | GitChat 责编 | 郭芮 出品 | CSDN 博客 AOP(Aspect Oriented Programming)面向切面编程是 Spring 框架最核心的组件之一,它通过对程序结构 ...

  3. Spring 框架的AOP之注解的方式

    1. 环境搭建 1.1 导入 jar 包 Spring 框架的基本开发包(6个); Spring 的传统AOP的开发包 spring-aop-4.3.10.RELEASE org.aopallianc ...

  4. spring框架aop_使用Spring框架和AOP进行动态路由

    spring框架aop 本文的总体思路是展示业务交易如何动态触发业务事件以进行子系统处理. 本文显示的示例有效地使用了Spring框架2.0和Spring AOP来将业务服务与子系统处理功能分离. 现 ...

  5. java 路由框架_使用Spring框架和AOP实现动态路由

    本文的大体思路是展示了一次业务交易如何动态地为子系统处理过程触发业务事件.本文所示的例子使用Spring框架和Spring AOP有效地解耦业务服务和子系统处理功能.现在让我们仔细看看业务需求. 业务 ...

  6. spring框架复习--aop,事务

    AOP概述 1.AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 2. 利用AOP可以对业务逻 ...

  7. Spring框架之AOP详解(带实战详细步骤)

    Spring之AOP AOP简介: 解决的问题:解决了需求的改变,造成了原有没必要改变的代码,需要去改变它: 比如:书籍的增删改,本身只需要完成增删改的功能即可,这是如果需要添加日志功能,那么需要在原 ...

  8. Spring框架四AOP

    AOP 面向切面编程 搭建环境 需要 9个包 链接: https://pan.baidu.com/s/1M0QQtX0aiYjmFoWayFHoHA 密码: kapc 切面  切点save*()  连 ...

  9. Spring框架AOP源码剖析

    今天我要和大家分享的是 AOP(Aspect-Oriented Programming)这个东西的源码剖析,作为多年的开发者,想必大家在面试的时候都被问过,你知道Spring框架AOP的底层实现机制吗 ...

最新文章

  1. .NET中获取电脑名、IP及用户名方法
  2. 无锡初一计算机试题,2015年无锡市初中信息技术考查选择题.doc
  3. python3 问题 No module named _sqlite3 解决方案
  4. Ajax请求中的async:false/true的作用
  5. php论坛怎么架设,论坛架设有诀窍 phpWind配置技巧三则
  6. Sahi ---实现 Web 自动化测试
  7. html js倒计时不准确,js倒计时代码:第2个倒计时为什么会不正常?
  8. 就地过年的年轻人都去搜索“年夜饭”外卖了
  9. 吴恩达机器学习练习4:神经网络学习(反向传播)
  10. Java内存中的堆和栈
  11. 3 天开发物联网应用!腾讯云 IoT 超级小程序来了
  12. 0day影响 Chrome和 Safari,谷歌不修复
  13. 运行VS2012出现“未找到与约束....”的解决方法
  14. 字典 选取前100_100道 Python 经典练习题004
  15. JSP项目实战视频教程
  16. MATLAB 信号处理仿真入门实验
  17. XtraReport报表控件
  18. BSD协议和FreeBSD
  19. 微信分享等配置,微信授权失败讲解
  20. 苹果移动设备(iPhone/iPad)分辨率汇总

热门文章

  1. MapReduce论文精读
  2. MapXtreme 2005 鹰眼源代码
  3. 相控阵天线(二):非规则直线阵列天线(稀布阵列、稀疏阵列、平方率分布阵列、含python代码)
  4. 论文阅读笔记(一)《Sequence to Sequence Learning with Neural Networks》
  5. 8个最吸引眼球的广告宣传
  6. 驾驭组织结构——生意格局和配置优势
  7. 虚拟服务器没有操作系统么,虚拟服务器自带操作系统吗
  8. 使用 GROW 模型
  9. SVN版本库的异地备份
  10. 如何使用Arduino摇杆模块(Joystick Shield)