Spring-11-AOP

11.1、AOP概念

AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

11.2、Spring AOP 的作用与目标

提供声明式事务;允许用户自定义切面

  • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 ....

  • 切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。

  • 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。

  • 目标(Target):被通知对象。

  • 代理(Proxy):向目标对象应用通知之后创建的对象。

  • 切入点(PointCut):切面通知 执行的 “地点”的定义。

  • 连接点(JointPoint):与切入点匹配的执行点。

SpringAOP中,通过Advice定义横切逻辑,Spring中支持5种类型的Advice:

通知类型连接点实现接口用法
前置通知方法前org.springframework.aop.BeforeAdvice表示在目标方法执行前来实施增强
后置通知方法后org.springframework.aop.AfterReturningAdvice表示在目标方法执行后来实施增强
环绕通知方法前后org.aopalliance.intercept.MethodInterceptor表示在目标方法执行前后同时实施增强
异常抛出通知方法抛出异常org.springframework.aop.ThrowsAdvice表示在目标方法抛出异常后来实施增强
引介增强类中增加新的方法和属性org.springframework.aop.introductioninterceptor<!--表示在目标类中添加一些新的方法和属性-->

即 Aop 在 不改变原有代码的情况下 , 去增加新的功能

11.3、使用Spring实现AOP

【重点】使用AOP织入,需要导入一个依赖包!

 <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
 <dependency>
     <groupId>org.aspectj</groupId>
     <artifactId>aspectjweaver</artifactId>
     <version>1.9.5</version>
     <scope>runtime</scope>
 </dependency>

方法一、使用Spring API接口

  1. 注册bean

     <bean id="pointcut" class="com.mosang.DIY.DIYPointCut"/>
  2. 编写配置文件

 <!--导入aop约束-->
 <aop:config>
     <!--修饰词 返回值 类名 方法名 参数-->
     <!--切入点-->
     <aop:pointcut id="pointcut" expression="execution(* com.mosang.service.serviceImpl.*(..))"/>
     <!--执行环绕增加-->
     <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
     <aop:advisor advice-ref="Afterlog" pointcut-ref="pointcut"/>
 </aop:config>
public class Log implements MethodBeforeAdvice {
     //method:要执行的目标对象的方法
     //args:参数
     //target:目标对象
     @Override
     public void before(Method method, Object[] args, Object target) throws Throwable {
         System.out.println(this.getClass().getName()+"执行了"+method.getName()+"方法");
     }
 }
  public class Afterlog implements AfterReturningAdvice {
     @Override
     public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
         System.out.println(this.getClass().getName()+"执行了"+method.getName()+"方法");
     }
 }
  1. 测试类:

 public class Mytest {
     public static void main(String[] args) {
         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
         //动态代理的是接口
         service service = context.getBean("service",service.class);
         service.search();
     }
 }

方法二、自定义类实现

  1. 自定义切面

     public class DIYPointCut {
             public void before(){
                 System.out.println("===============方法开始前=============");
             }
             public void after(){
                 System.out.println("===============方法开始后=============");
             }
     }
  2. aop:config

     <bean id="pointcut" class="com.mosang.DIY.DIYPointCut"/>
         <aop:config>
             <!--自定义切面 ref:要引用的类-->
             <aop:aspect ref="pointcut">
                 <!--切入点-->
                 <aop:pointcut id="point" expression="execution(* com.mosang.service.serviceImpl.*(..))"/>
                 <!--通知-->
                 <aop:before method="before" pointcut-ref="point"/>
                 <aop:after method="after" pointcut-ref="point"/>
             </aop:aspect>
         </aop:config>
     </beans>

  • execution语法

    execution(* com.sample.service.impl..*.*(..))

    符号含义
    *execution()* *表达式的主体;*
    *第一个”*“符号* *表示返回值的类型任意;*
    com.sample.service.implAOP所切的服务的包名,即,我们的业务部分
    包名后面的”..“表示当前包及子包
    第二个”*“表示类名,*即所有类。此处可以自定义,下文有举例
    .*(..)表示任何方法名,括号表示参数,两个点表示任何参数类型

execution(<修饰符模式> <返回类型模式> <方法名模式> (<参数模式>) <异常模式>?)

除了返回类型模式、方法名模式和参数模式外,其它项都是可选的。

参考:白话Spring(基础篇)---AOP(execution表达式)_y-yg的博客-CSDN博客

方法三、注解实现

  1. 自定义注解切面

     @Aspect
     public class annotationpoint {
         @Before("execution(* com.mosang.service.serviceimpl.*(..))")
         public void before() {
             System.out.println("==========方法执行前========");
         }
         @After("execution(* com.mosang.service.serviceimpl.*(..))")
         public void after(){
             System.out.println("==========方法执行后========");
         }
         @Around("execution(* com.mosang.service.serviceimpl.*(..))")
         public void around(ProceedingJoinPoint jp) throws Throwable {
             System.out.println("------环绕前-----");
             jp.proceed();
             System.out.println("------环绕后-----");
         }
     }
  2. 编写配置文件,注册bean,并增加支持注解的配置

     <bean id="service" class="com.mosang.service.serviceimpl"/>
     <bean id="annotationpoint" class="com.mosang.diy.annotationpoint"/>
     <!--开启注解支持-->
     <aop:aspectj-autoproxy/>

    通过aop命名空间的<aop:aspectj-autoproxy />声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。当然,spring 在内部依旧采用AnnotationAwareAspectJAutoProxyCreator进行自动代理的创建工作,但具体实现的细节已经被<aop:aspectj-autoproxy />隐藏起来了

    <aop:aspectj-autoproxy />有一个proxy-target-class属性,默认为false,表示使用jdk动态代理织入增强,当配为<aop:aspectj-autoproxy poxy-target-class="true"/>时,表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值