AOP

AOP 在 Spring 中的作用

由上节了解到 AOP 底层是通过动态代理实现的,想使用 AOP 进行织入,需要导入依赖。
AOP作用很多提供声明式事务,允许用户自定义切面

  • 橫切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等….
  • 切面(ASPECT) :横切关注点被模块化的特殊对象。即,它是一个类。
  • 通知(Advice) :切面必须要完成的工作。即,它是类中的一个方法。
  • 目标(Target):被通知对象。
  • 代理(Proxy):向目标对象应用通知之后创建的对象。
  • 切入点(PointCut) :切面通知执行的“地点”的定义。
  • 连接点(UointPoint) :与切入点匹配的执行点。
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.4</version>
    </dependency>
    AOP实现的方法有三种:
  • 方法一:使用Spring的API接口 [主要是Spring接口实现]
  • 方法二:自定义类实现 [主要是切面定义]
  • 方法三:使用注解实现

准备

首先 Service 层有几个方法,UserService 接口有几个方法

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void search();
}

具体实现类继承接口,实现方法

public class UserServiceImpl implements UserService {

    public void add() {
        System.out.println("增加了一个用户");
    }

    public void delete() {
        System.out.println("删除了一个用户");

    }

    public void update() {
        System.out.println("更新了一个用户");

    }

    public void search() {
        System.out.println("查询了一个用户");

    }
}

方法一:使用Spring的API接口[主要是Spring接口实现]

在所有的方法前后添加日志打印,首先是方法执行前添加日志打印,继承 MethodBeforeAdvice 接口,实现 before 方法。

public class Log implements MethodBeforeAdvice {

    //method:要执行的目标对象的方法
    //objects:参数
    //o:目标对象
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(o.getClass().getName()+"的"+method.getName()+"被执行了");
    }
}

在方法后添加日志打印

public class AfterLog implements AfterReturningAdvice {

    //returnValue;返回值
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
    }
}

然后是配置 applicationContext.xml 文件。

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

<!--注册bean-->
<bean id="userService" class="com.hudu.service.UserServiceImpl"/>
<bean id="log" class="com.hudu.log.Log"/>
<bean id="afterLog" class="com.hudu.log.AfterLog"/>

<!--方式一:使用原生Spring API接口-->
<!--配置aop:需要导入aop的约束-->
<aop:config>
  <!--切入点:expression:表达式execution(要执行的位置, * (返回类型,*表示所有类型) 类名 方法名 参数)-->
  <aop:pointcut id="pointcut" expression="execution(* com.hudu.service.UserServiceImpl.*(..))"/>

  <!--执行环绕增强-->
  <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
 <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>

</beans>

自定义类实现 [主要是切面定义]

首先自定义一个类 DiyPointCut

public class DiyPointCut {

    public void before() {
        System.out.println("======方法执行前======");
    }

    public void after() {
        System.out.println("======方法执行后======");
    }
}

配置 XML 文件

<!--方式二:自定义类-->
    <bean id="diy" class="com.hudu.diy.DiyPointCut"/>

    <aop:config>
        <!--自定义切面,ref要引用的类-->
        <aop:aspect ref="diy">
            <!--切入点-->
            <aop:pointcut id="point" expression="execution(* com.hudu.service.UserServiceImpl.*(..))"/>
            <!--通知-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

使用注解

定义一个类

@Aspect //标注这个类是一个切面
public class AnnotationPointCut {

    @Before("execution(* com.hudu.service.UserServiceImpl.*(..))")
    public void before() {
        System.out.println("******方法执行前******");
    }

    @After("execution(* com.hudu.service.UserServiceImpl.*(..))")
    public void after() {
        System.out.println("======方法执行后======");
    }

    //在环绕增强中,我们可以给定一个参数,代表我们要获取处理的切入点
    @Around("execution(* com.hudu.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前");

        Signature signature = joinPoint.getSignature();//获得签名
        System.out.println("signature:"+signature);
        //执行方法
        Object proceed = joinPoint.proceed();

        System.out.println("环绕后");
        System.out.println(proceed);
    }
}

配置XML文件

<!--方式三:使用注解-->
    <bean id="annotationPointCut" class="com.hudu.diy.AnnotationPointCut"/>
    <!--开启注解支持 JDK(默认) cglib(proxy-target-class="true")-->
    <aop:aspectj-autoproxy proxy-target-class="false"/>
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
247
粉丝
18
喜欢
217
收藏
62
排名:731
访问:9753
私信
所有博文
社区赞助商