AOP

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

三种实现方式

  1. 使用原生spring API接口
    beforelog.java
public class Log implements MethodBeforeAdvice {
    //method:要执行的目标对象的方法
    //objects:参数(args)
    //object: 目标对象
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println (target.getClass ().getName ()+"的"+method.getName ()+"被执行了");
    }
}

afterlog.java

public class AfterLog implements AfterReturningAdvice {
    //returnValue执行后返回值
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println ("被执行了"+target.getClass ().getName ()+"的"+method.getName ()+"结果为"+returnValue);
    }
}

spring配置文件

<bean id="userservice" class="com.ji.service.UserServiceImpl"/>
<bean id="log" class="com.ji.log.Log"/>
<bean id="afterlog" class="com.ji.log.AfterLog"/>
           使用原生的api接口
<aop:config>
               切入点:在哪个地方执行     execution(要执行的位置)
          <aop:pointcut id="pointcut" expression="execution(* com.ji.service.UserServiceImpl.*(..))"/>
                执行环绕增强
          <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
          <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
</aop:config>
  1. 自定义类来说实现aop(主要是切面定义)
    自定义实现类
public class DiyPointCut {
    public void before(){
        System.out.println ("方法执行前");
    }
    public void after(){
        System.out.println ("方法执行后");
    }
}

spring配置文件

<bean id="diy" class="com.ji.diy.DiyPointCut"/>
<aop:config>
                <aop:aspect ref="diy"> 自定义切面要引入的类;
                   切入点
                    <aop:pointcut id="pointCut" expression="execution(* com.ji.service.UserServiceImpl.*(..))"/>
                   通知
                    <aop:before method="before" pointcut-ref="pointCut"/>
                    <aop:after method="after" pointcut-ref="pointCut"/>
                </aop:aspect>
        </aop:config>
  1. 使用注解实现
    注解实现类
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

//使用注解方式实现aop
@Aspect  //标注这个类是一个切面
public class AnnotationPointCut {
    @Before ("execution(* com.ji.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println ("方法执行前");
    }
    @After ("execution(* com.ji.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println ("方法执行后");
    }
    //在环绕增强中可以给定一个参数代表我们要获取处理切入的点
    @Around ("execution(* com.ji.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp){

        System.out.println ("环绕前");
        //执行方法
        try {
            System.out.println (jp.proceed ());
            System.out.println (jp.getSignature ());
        } catch (Throwable throwable) {
            throwable.printStackTrace ();
        }
        System.out.println ("环绕后");
    }
}

spring配置文件

 <!--开启注解支持-->
    <aop:aspectj-autoproxy/>
    <bean id="userservice" class="com.ji.service.UserServiceImpl"/>
    <bean id="annotation" class="com.ji.diy.AnnotationPointCut"/>

总结
注解实现编写的时候最方便,但后期维护可能会造成困扰,第一种方法需要再写两个类,总体个人觉得第二种方法比较好。

posted @ 2021-11-27 18:54  一刹流云散  阅读(417)  评论(0编辑  收藏  举报