程序员

这篇关于Spring之SSM框架的笔记你真得看看,写的非常详细了

2020-08-10  本文已影响0人  程序员匡胤

什么是AOP

AOP是Aspect Oriented Programming的缩写,意思为面向切面编程,是通过预编译的方法和运行期动态代理实现程序的统一维护的一种技术

AOP是OOP(面向对象)的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

AOP的作用及其优势

作用

在程序运行期间,在不修改源码的情况下对方法进行功能增强

优势

减少重复代码,提高开发效率,并且便于维护

AOP的底层实现

实际上,在AOP的底层是通过Spring提供的动态代理技术实现的。在运行期间,Spring通过动态代理技术动态生成代理对象,代理对象方法执行时进行增强功能的介入,在去调用目标对象的方法,从而完成功能的增强。

AOP的动态代理技术

常用的动态代理技术:

AOP相关概念

Spring的AOP实现底层就是对动态代理的代码进行了封装,封装后我们只需要对关注的部分进行代码编写,并通过配置的方式完成指定目标的方法增强。

相关术语

AOP开发的明确事项

需要编写的内容

AOP技术实现的内容

Spring框架监控切入点方法的执行。一旦切入点方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。

AOP底层使用哪种代理方式

在spring中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式

基于XML的AOP开发

快速入门

  1. 导入AOP相关的坐标
 <dependency>
                   <groupId>org.springframework</groupId>
                   <artifactId>spring-context</artifactId>
                   <version>5.2.5.RELEASE</version>
               </dependency>
       <dependency>
                   <groupId>org.aspectj</groupId>
                   <artifactId>aspectjweaver</artifactId>
                   <version>1.8.4</version>
               </dependency>
               <dependency>
                   <groupId>junit</groupId>
                   <artifactId>junit</artifactId>
                   <version>4.12</version>
               </dependency>

  1. 创建目标接口和目标类(内部有切点)
 public interface TargetInterface {
           void save();
}

 public class Target implements TargetInterface {
           @Override
           public void save() {
               System.out.println("意大利炮准备就绪!");
           }
}

  1. 创建切面类(内部有增强方法)
 public class MyAspect {

           public void before(){
               System.out.println("我是前置方法。。。。。。。");
           }
}

  1. 将目标类和切面类的对象创建权交给spring
            <!--目标对象-->
           <bean id="target" class="zyh.com.aop.Target"></bean>

           <!--切面对象-->
           <bean id="myAspect" class="zyh.com.aop.MyAspect"></bean>

  1. 在applicationContext.xml中配置织入关系
        <!--配置织入:告诉spring框架 哪些方法(切点)需要进行哪些增强(前置,后置...)-->
           <aop:config>
               <!--声明切面-->
               <aop:aspect ref="myAspect">
                   <!--切面:切点+通知 当访问save方法时,会通过myAspect中的before方法进行前置增强-->
                   <aop:before method="before" pointcut="execution(public void zyh.com.aop.Target.save())"/>
               </aop:aspect>
           </aop:config>

  1. 测试代码
       @RunWith(SpringJUnit4ClassRunner.class)
       @ContextConfiguration("classpath:applicationContext.xml")
       public class AopTest {
           @Autowired
           @Qualifier("target")
           private TargetInterface target;

           @Test
           public void  t1(){
               target.save();
           }
       }

切点表达式的写法

表达式语法:excution([修饰符] 返回值类型 包名.类名.方法名(参数))

通知类型

基于注解的AOP开发

快速入门

  1. 创建目标接口和目标类(内部有切点)
  2. 创建切面类(内部有增强方法)
  3. 将目标类和切面类的创建权交给spring
       @Component("target")
       public class Target implements TargetInterface {
           @Override
           public void save() {
               System.out.println("意大利炮准备就绪!");
           }
       }

       @Component("myAspect")
       public class MyAspect {

           public void before(){
               System.out.println("我是前置方法。。。。。。。");
           }
       }

  1. 在切面类中使用注解配置织入关系
       @Component("myAspect")
       @Aspect//标注当前MyAspect是一个切面类
       public class MyAspect {

           @Before(value = "execution(* zyh.com.anno.*.*(..))")
           public void before(){
               System.out.println("我是前置方法。。。。。。。");
           }
       }

  1. 在配置文件中开启组件扫描和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"
              xmlns:context="http://www.springframework.org/schema/context"
              xsi:schemaLocation="
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       ">
           <!--组件扫描-->
           <context:component-scan base-package="zyh.com.anno"/>
           <!--aop自动代理-->
           <aop:aspectj-autoproxy/>

       </beans>

  1. 测试
 @RunWith(SpringJUnit4ClassRunner.class)
       @ContextConfiguration("classpath:applicationContext-anno.xml")
       public class AnnoTest {
           @Autowired
           @Qualifier("target")
           private TargetInterface target;

           @Test
           public void  t1(){
               target.save();
           }
       }

最后

感谢你看到这里,看完有什么的不懂的可以在评论区问我,觉得文章对你有帮助的话记得给我点个赞,每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章!

上一篇 下一篇

猜你喜欢

热点阅读