Spring-面向切面(Aop)

面向切面编程

概念

AOP(Aspect Oriented Programming),即面向切面编程,利用一种称为"横切"的技术,剖开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为"Aspect",即切面。所谓"切面",简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。

AOP开发术语

  • 连接点(Joinpoint):连接点是程序类中客观存在的方法,可被Spring拦截并切入内容。

  • 切入点(Pointcut):被Spring切入连接点。

  • 通知、增强(Advice):可以为切入点添加额外功能,分为:前置通知、后置通知、异常通知、环绕通知等。

  • 目标对象(Target):代理的目标对象

  • 引介(Introduction):一种特殊的增强,可在运行期为类动态添加Field和Method。

  • 织入(Weaving):把通知应用到具体的类,进而创建新的代理类的过程。

  • 代理(Proxy):被AOP织入通知后,产生的结果类。

  • 切面(Aspect):由切点和通知组成,将横切逻辑织入切面所指定的连接点中。

作用

 

Spring的AOP编程即是通过动态代理类为原始类的方法添加辅助功能。

1.环境搭建

引入AOP相关依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.1.6.RELEASE</version>
</dependency>

2.创建spring-context.xml引入AOP命名空间

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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
       ">
</beans>

流程:

定义原始类

public interface EmpServices {
    void  show();

    int add(User user);
}
public class EmpServicesImpl implements EmpServices {

    @Override
    public void show() {
        System.out.println("获取数据展示show方法");
    }

    @Override
    public int add(User u) {
        System.out.println("新增user");
        return 0;
    }
}

定义通知类(添加额外功能)

/**
 * @author: qiuyongqi
 * @time: 16:34 2021/5/17
 * @description:  前置增强
 */
public class BeforeShow implements MethodBeforeAdvice {   //实现前置通知接口
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("我来记录日志了");
        System.out.println(new Date()+":执行了-->"+method.getName()+"方法");
    }
}

定义bean标签

    <!-- 1.配置需要增强的类目标类(业务类) -->
    <!--原始对象-->
    <bean id="empServices" class="com.cos.qf.demo5.impl.EmpServicesImpl"></bean>
   
    <!-- 2.配置增强类,交由spring管理 -->
    <!--辅助对象-->
    <bean id="beforeShow" class="com.cos.qf.demo5.advice.BeforeShow"></bean>

定义切入点(PointCut)

形成切面(Aspect)

   <aop:config>
        <!-- execution(访问修饰符 返回值 包.类 (参数列表) 抛出的异常 -->
        <!-- 无论是什么访问修饰符,有没有返回值,只要是在com.cos.demo4.impl下的所有类,里面的所有方法无论有无参数 都符合条件-->
          <!--切点-->
        <aop:pointcut id="base" expression="execution(* com.cos.qf.demo5.impl.*.*(..))"/>
        <!-- 设置增强 -->
        <!--组装切面 -->
        <aop:advisor advice-ref="beforeShow" pointcut-ref="base"/>
    </aop:config>

测试类:

public class TestAop {

    @Test
    public void t1() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("application-aop.xml");

        EmpServices es = (EmpServices) ac.getBean("empServices");
        System.out.println("*************");
        System.out.println(es.getClass());
        System.out.println("*************");
        es.show();
        System.out.println("=====================");
        System.out.println("=====================");
        System.out.println("=====================");
        es.add(null);

    }

}

总结Aop:

  • 通过AOP提供的编码流程,更便利的定制切面,更方便的定制了动态代理。

  • 进而彻底解决了辅助功能冗余的问题;

  • 业务类中职责单一性得到更好保障;

  • 辅助功能也有很好的复用性。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值