Spring【AOP】

AOP-面向切面编程

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

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

导入依赖

XML 复制代码
<dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>

applicationContext.xml

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类

java 复制代码
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类

java 复制代码
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配置文件

XML 复制代码
    <!--方式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

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

配置spring配置文件

XML 复制代码
 <!--方式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

编写类

java 复制代码
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("方法之前执行");
    }
}

编写配置文件

XML 复制代码
    <!--方式3-->
    <bean id="annotationPointCut" class="com.study.diy.AnnotationPointCut"/>
    <!--开启注解支持!-->
    <aop:aspectj-autoproxy/>
相关推荐
夏日听雨眠8 分钟前
Linux(信号,管道,共享内存)
java·服务器·网络
TANGLONG2228 分钟前
【C++】继承详解——基类/派生类、作用域、默认函数、菱形继承(超详细)
java·c语言·c++·经验分享·笔记·ajax
未若君雅裁9 分钟前
MySQL慢SQL排查实战-从定位到EXPLAIN优化闭环
sql·mysql
zmsofts16 分钟前
IntelliJ IDEA)因为内存不足而崩溃
java·ide·intellij-idea
Dlrb121123 分钟前
C语言-字符串指针与函数指针
java·c语言·前端
萝卜白菜。27 分钟前
通过cmdline-jmxclient.jar采集TongWeb8.0监控值
java·jar
最后一支迷迭香33 分钟前
苹果的MacOS系统适合做Java开发吗
java·开发语言·macos
m0_7390300034 分钟前
[特殊字符] Java 高频面试题汇总
java·开发语言·面试
白宇横流学长35 分钟前
基于Spring Boot的校园考勤管理系统的设计与实现
java·spring boot·后端
书语时36 分钟前
单体 MySQL 支撑业务的上限一般从哪里先触顶?如何论证瓶颈在 DB?
数据库·mysql