10、AOP

10、AOP

10.1 什么是aop

aop(aspect oriented programming):面向切面编。通过预编译的方式和运行动态代理实现程序功能的统一维护的一种技术

在这里插入图片描述

10.2 AOP在Spring中的作用

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

横切关注点:跨越应用程序多个模块的方法或功能。即:与我们逻辑无关的,但是我们需要专注的部分,就是横切关注点。如:日志,安全,缓存,事务等等 切面(aspect):横切关注点被 模块化 的特殊对象。即:它是一个类 通知(advice):切面必须要完成的工作。即:它是类中的一个方法 目标(target):被通知的对象 代理(proxy):向目标对象应用通知之后创建的对象 切入点(PointCut):切面通知 执行的 “地点” 的定义 连接点(joinPoint):与切入点匹配的执行点

10.3 使用spring实现AOP

使用AOP织入,需要导入一个依赖包

<dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.9.1</version>
            <scope>runtime</scope>
</dependency>

10.3.1 方式一:使用spring的API接口

动态代理代理的是接口,静态代理代理的是实体类

  • 1.配置了 前置日志和后置日志

  • 2.准备了UserService和UserServiceImpl实体类,实现增删改查

配置:

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 id="userService" class="com.kuang.service.UserServiceImpl"/>
    <bean id="log" class="com.kuang.log.Log"/>
    <bean id="afterLog" class="com.kuang.log.AfterLog"/>

    <!--配置方法一:使用原生spring API接口-->
    <aop:config>
        <!--切入点:expression:表达式 execution(要执行的位置! * * * * ) 第一个*表示方法类型-->
        <aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>

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

</beans>

Log

public class Log implements MethodBeforeAdvice {
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(method.getClass().getName()+"类,执行了"+method.getName()+"方法");
    }
}

AfterLog

public class AfterLog implements AfterReturningAdvice {
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法"+returnValue);
    }
}

测试:

@Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是UserService接口
        UserService userService = (UserService) context.getBean("userService");

        userService.delete();

    }

10.3.2 方式二:使用自定义类实现AOP

自定义Log类

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

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

配置:

<bean id="userService" class="com.kuang.service.UserServiceImpl"/>

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

    <aop:config>
        <aop:aspect id="point" ref="diy">
            <!--切入点-->
            <aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
            <!--切面-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

测试:

public class MyTest {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是UserService接口
        UserService userservice = (UserService) context.getBean("userService");

        userservice.query();
    }
}

10.3.3 方式三:使用注解实现AOP

自定义一个类充当切面:

//方式三:使用注解方式实现AOP
@Aspect // //标注这个类是一个切面
public class AnnotationPointCut {

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

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

        System.out.println("======环绕前======");

        Signature signature = pj.getSignature();//获取签名
//        System.out.println("signature="+signature);

        //执行方法
        Object proceed = pj.proceed();

        System.out.println("======环绕后======");

    }
}

配置:

    <!--注册bean    -->
    <bean id="userService" class="com.hardy.service.UserServiceImpl"/>

    <!--方式三:  使用注解实现   -->
    <bean id="annotationPointCut" class="com.hardy.diy.AnnotationPointCut"/>
    <!--开启注解支持    -->
    <aop:aspectj-autoproxy/>

测试:

public class MyTest {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是UserService接口
        UserService userservice = (UserService) context.getBean("userService");

        userservice.query();
    }
}

结果:环绕在最外层最早和最晚执行

========环绕前=========
signature:void com.hardy.service.UserService.query()
========方法执行前=========
查询了一条数据
========方法执行后=========
========环绕后=========
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值