首页 > 资讯 > 产业政策

Java AOP实践指南:切面编程详解

人阅读 2024-06-05 12:01:35物联网

哈喽,大家好,我是了不起。

AOP就是面向切面编程,或者叫面向方面编程,或者开玩笑的说叫面向方便面编程,如果粗俗的理解,就是可以自定义注解,然后通过自己定义的方式定义注解的作用。

什么是SpringAOP

SpringAOP的全称是(Aspect Oriented Programming)中文翻译过来是面向切面编程,AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

图片

AOP体系

图片

SpringAOP的应用场景

  • 日志记录
  • 权限验证(SpringSecurity有使用)
  • 事务控制(调用方法前开启事务, 调用方法后提交关闭事务 )
  • 效率检查(检测方法运行时间)
  • 数据源代理(seata里面,获取到数据源连接执行的sql)
  • 缓存优化 (第一次调用查询数据库,将查询结果放入内存对象, 第二次调用, 直接从内存对象返回,不需要查询数据库 )

Aop在 Spring 中的作用

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

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

图片

AOP的实现方式

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

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.4</version>
</dependency>

使用方式

applicationContext.xml:

<?xml versinotallow="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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
  ">
  <!--注册bean-->
    <bean id="userService" class="service.UserServiceImpl"/>
    <bean id="log" class="log.Log"/>
    <bean id="afterLog" class="log.AfterLog"/>
    <!--配置aop:需要导入aop的约束-->
    <aop:config>
        <!--切入点:expression:表达式,execution(要执行的位置! * * * *)-->

        <aop:pointcut id="pointcut" expression="execution(* service.UserServiceImpl.*(..))"></aop:pointcut>
        <!--执行环绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

UserService接口:

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

UserServiceImpl实现类(切入点):

public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("增加了一个用户");
    }

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

    }

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

    }

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

    }
}

前置通知:

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {

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

后置通知:

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {

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

测试类:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
import service.UserServiceImpl;

import java.lang.annotation.Annotation;

public class MyTest {
    public static void main(String[]  args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理的是接口
        UserService userService = (UserService) context.getBean("userService");
        userService.select();

    }
}

结语

通过本文的讲解,我们深入了解了切面编程的核心概念、动态代理的实现原理,并通过一个实际的例子展示了使用Java AOP的完整过程。AOP可以帮助我们将横切关注点(例如日志记录、事务管理等)从核心业务逻辑中解耦出来,提高代码的可维护性和重用性。同时,AOP也是实现设计模式和架构思想的重要手段之一,我们在开发中可以灵活运用AOP来优化代码结构并提高系统的整体性能。

LOT物联网

iot产品 iot技术 iot应用 iot工程

Powered By LOT物联网  闽ICP备2024036174号-1

联系邮箱:support1012@126.com