Spring【AOP】

简介: Spring【AOP】

AOP-面向切面编程

AOP:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。

 

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

导入依赖

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

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

Spring 实现AOP的3种方式

1、使用Spring API

编写两个扩展功能的类Log、和AfterLog,分别将添加到旧业务的前面和后面

Log类

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()+"方法被执行了");
    }
}

AfterLog类

import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object result, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法,返回结果为"+result);
    }
}

配置spring配置文件

    <!--方式1-->
    <!--配置aop:需要导入aop的xsi信息-->
    <aop:config>
        <!--切入点 execution(要执行的位置!)-->
        <aop:pointcut id="pointcut" expression="execution(* com.study.service.UserServiceImpl.*(..))"/>
        <!--执行环绕-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>

2、自定义类实现

编写一个自定义切面类 DiyPointCut

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

配置spring配置文件

 <!--方式2-->
    <bean id="diy" class="com.study.diy.DiyPointCut"/>
    <aop:config>
        <!--自定义切面-->
        <aop:aspect ref="diy">
            <aop:pointcut id="point" expression="execution(* com.study.service.UserServiceImpl.*(..))"/>
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

3、使用注解实现AOP

编写类

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//使用注解实现AOP 标注这个类为一个切面
@Aspect
public class AnnotationPointCut {
    @Before("execution(* com.study.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("方法之前执行");
    }
}

编写配置文件

    <!--方式3-->
    <bean id="annotationPointCut" class="com.study.diy.AnnotationPointCut"/>
    <!--开启注解支持!-->
    <aop:aspectj-autoproxy/>


相关文章
|
9天前
|
XML Java API
Spring AOP切点和通知机制的深度解析
Spring AOP切点和通知机制的深度解析
31 4
|
14天前
|
Java Maven 数据安全/隐私保护
详解 Java AOP:面向方面编程的核心概念与 Spring 实现
详解 Java AOP:面向方面编程的核心概念与 Spring 实现
21 1
|
12天前
|
XML 安全 Java
Spring高手之路19——Spring AOP注解指南
在本文中,我们将深入探索Spring AOP(面向切面编程)的核心概念及其在现代Spring应用中的实际应用。从基本的注解使用到复杂的切面配置,本文将一步步指导你如何利用Spring AOP提升代码的模块化,帮助你在Spring开发路上更进一步。
25 3
Spring高手之路19——Spring AOP注解指南
|
9天前
|
缓存 监控 安全
在 Spring Boot 中使用 AOP(Aspect-Oriented Programming)实现日志记录功能
在 Spring Boot 中使用 AOP(Aspect-Oriented Programming)实现日志记录功能
19 1
|
12天前
|
设计模式 网络安全 开发工具
|
14天前
|
缓存 Java uml
Spring压轴题:当循环依赖遇上Spring AOP
Spring压轴题:当循环依赖遇上Spring AOP
17 1
|
16天前
|
XML 监控 Java
Java一分钟之-Spring AOP:基于Spring的AOP
【6月更文挑战第13天】Spring框架集成AOP支持,便于实现如日志、监控和事务管理等关注点的集中管理。本文探讨Spring AOP的核心概念(切面、切入点、通知和代理),常见问题(代理对象理解不清、切入点表达式错误、通知类型混淆和事务管理配置不当)及其对策,并提供注解式日志记录代码示例。通过学习和实践,开发者能更好地运用Spring AOP提升代码质量。
27 2
|
15小时前
|
Java 开发者 Spring
使用Spring Boot AOP实现日志记录
使用Spring Boot AOP实现日志记录
|
1天前
|
前端开发 Java 数据库
浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
|
1天前
|
XML Java API
经验大分享:Spring实现AOP的三种方式
经验大分享:Spring实现AOP的三种方式