浅谈Spring的IOC与AOP

1、IOC

IOC:思想
DI:手段

​ 传统编程模式下,通常采用new关键字来创建对象,这种方式会增加调用者与被调用者之间的耦合性,不利于代码维护。而IOC是指,由Spring容器负责对象的创建,从而负责控制程序之间的关系,这样控制权就由调用者转移至Spring容器,即控制反转。

2、AOP

​ 采用横向抽取机制,即:将分散在各个方法中的重复代码提取出来,然后在程序编译或运行时阶段再将其应用到需要执行的地方。需要注意的是,AOP不是OOP的替代品,而是OOP的补充,两者相辅相成

术语:

  • Aspect(切面): A modularization of a concern that cuts across multiple classes. Transaction management is a good example of a crosscutting concern in enterprise Java applications. In Spring AOP, aspects are implemented by using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style). 本质:类,即要横切到系统功能的类,是切入点和通知(引介)的结合

  • Join point(连接点): A point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution. 本质:时间点,如方法调用或异常的抛出

  • Advice(通知,可用于增强处理): Action taken by an aspect at a particular join point. Different types of advice include “around”, “before” and “after” advice. (Advice types are discussed later.) Many AOP frameworks, including Spring, model an advice as an interceptor and maintain a chain of interceptors around the join point. 可以理解为,切面开启后切面的方法

  • Pointcut(切入点): A predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default. 本质:需要处理的连接点

  • Introduction(引入): Declaring additional methods or fields on behalf of a type. Spring AOP lets you introduce new interfaces (and a corresponding implementation) to any advised object. For example, you could use an introduction to make a bean implement an IsModified interface, to simplify caching. (An introduction is known as an inter-type declaration in the AspectJ community.) 在现有的实现类中添加自定义的方法和属性

  • Target Object(目标对象): An object being advised by one or more aspects. Also referred to as the “advised object”. Since Spring AOP is implemented by using runtime proxies, this object is always a proxied object.所有被通知的对象,若AOP使用运行时代理方式,则通知对象总是一个代理对象

  • AOP proxy(代理): An object created by the AOP framework in order to implement the aspect contracts (advise method executions and so on). In the Spring Framework, an AOP proxy is a JDK dynamic proxy or a CGLIB proxy.通知应用到代理后,被动态创建的对象

  • Weaving(织入): linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.

    ***将切面代码插入目标对象上,从而生成代理对象的过程。***织入的三种方式:1、编译器织入 2、类装载织入 3、动态代理织入。AspectJ采用编译器织入和类装载织入。Spring采用动态代理织入。

  • 动态代理技术:
    • 1、JDK代理:基于接口的动态代理技术
    • 2、cglib代理:基于父类的动态代理技术
  • AOP开发注意事项
    • 1、需要编写的内容
      • 编写核心业务代码(目标类的目标方法)
      • 编写切面类,切面类中有通知(增强功能方法)
      • 在配置文件中,配置织入关系,即:将哪些通知与哪些连接点进行结合
    • 2、AOP技术实现的内容
      • Spring框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。
    • 在Spring中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式。

参考:https://spring.io/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值