Spring深入讲解实现AOP的三种方式

 更新时间:2022年05月20日 11:17:36   作者:张志明(努力奋斗版)  
Spring的AOP就是通过动态代理实现的,使用了两个动态代理,分别是JDK的动态代理和CGLIB动态代理,本文重点给大家介绍下Spring Aop的三种实现,感兴趣的朋友一起看看吧

[重点] 使用AOP织入 需要导入一个依赖包

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

方式一:使用原生Spring API接口

配置文件

<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       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="com.kero.service.UserServiceImpl"/>
    <bean id="log" class="com.kero.log.Log"/>
    <bean id="afterlLog" class="com.kero.log.AfterLog"/>
<!--    配置aop 需要导入aop的约束-->
    <aop:config>
<!--        切入点 expression表达式 expression(要执行的位置  )-->
        <aop:pointcut id="pointcut" expression="execution(* com.kero.service.UserServiceImpl.*(..))"/>
<!--        执行环绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterlLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

Log

import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
    //objects:参数
    //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 {
    //method:要执行的目标对象的方法
    //objects:参数
    //target:目标对象
    //returnValue:返回值
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+ method.getName() + "方法,返回结果为:"+returnValue);
    }
}

Service

import org.springframework.stereotype.Service;
@Service
public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void search();
}
public class UserServiceImpl implements UserService{
    @Override
    public void add() {
    }
    @Override
    public void delete() {
    }
    @Override
    public void update() {
    }
    @Override
    public void search() {
    }
}

test动态代理 代理的是接口(代理模式是SpringAOP的底层)

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理 代理的是接口
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }
}

方式二:使用自定义类

配置文件

<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       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="com.kero.service.UserServiceImpl"/>
    <bean id="log" class="com.kero.log.Log"/>
    <bean id="afterlLog" class="com.kero.log.AfterLog"/>
    <bean id="diy" class="com.kero.diy.DiyPointCut"/>
    <aop:config>
<!--        自定义切面 ref要引用的类-->
        <aop:aspect ref="diy">
<!--            切入点-->
            <aop:pointcut id="point" expression="execution(* com.kero.service.UserServiceImpl.*(..))"/>
<!--            通知-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
</beans>

DIY类

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

其他的不变

方式三:使用注解实现

配置文件

<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--    注册bean-->
    <bean id="userService" class="com.kero.service.UserServiceImpl"/>
    <bean id="log" class="com.kero.log.Log"/>
    <bean id="afterlLog" class="com.kero.log.AfterLog"/>
<!--    开启注解支持-->
    <aop:aspectj-autoproxy/>
    <bean id="annotationPointCut" class="com.kero.diy.AnnotationPointCut"/>
</beans>

自定义类

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

其他不变

补充知识:execution表达式

execution表达式的详解

切入点表达式:execution(* 包名.*.*(..))

整个表达式可以分为五个部分:

1、execution(): 表达式主体。

2、第一个*号:方法返回类型, *号表示所有的类型。

3、包名:表示需要拦截的包名。

4、第二个*号:表示类名,*号表示所有的类。

5、*(..):最后这个星号表示方法名,*号表示所有的方法,后面( )里面表示方法的参数,两个句点表示任何参数

其中除了返回类型模式、方法名模式和参数模式外,其它项都是可选的。

举例:

execution(public * *(..)) 匹配所有的public修饰符的方法

execution(* set*(..)) 匹配所有”set”开头的方法:

execution(* com.kero.service.UserServiceImpl.*(..))) 匹配UserServiceImpl接口/类的所有方法:

到此这篇关于Spring深入讲解实现AOP的三种方式的文章就介绍到这了,更多相关Spring AOP内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java开源好用的简繁转换类库推荐

    java开源好用的简繁转换类库推荐

    这篇文章主要为大家介绍了java开源好用的简繁转换类库推荐,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Java启用Azure Linux虚拟机诊断设置

    Java启用Azure Linux虚拟机诊断设置

    这篇文章主要介绍了Java启用Azure Linux虚拟机诊断设置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • Java Builder模式构建MAP/LIST的实例讲解

    Java Builder模式构建MAP/LIST的实例讲解

    下面小编就为大家带来一篇Java Builder模式构建MAP/LIST的实例讲解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Java实现PDF文件的分割与加密功能

    Java实现PDF文件的分割与加密功能

    这篇文章主要为大家分享了如何利用Java语言实现PDF文件的分割与加密以及封面图的生成,文中的示例代码简洁易懂,感兴趣的可以了解一下
    2022-04-04
  • Docker使用Dockerfile自定义镜像方式

    Docker使用Dockerfile自定义镜像方式

    这篇文章主要介绍了Docker使用Dockerfile自定义镜像方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • Java Servlet中Response对象的使用方法

    Java Servlet中Response对象的使用方法

    本文介绍了Java Servlet中Response对象的使用方法,包括设置响应头、设置响应编码、向客户端发送数据、重定向等操作,同时介绍了常用的响应状态码和响应类型,帮助读者更好地理解和掌握Servlet中Response对象的用法
    2023-05-05
  • Java map的学习及代码示例

    Java map的学习及代码示例

    这篇文章主要介绍了Java map的学习及代码示例,简单介绍了map与collection的比较,map的相关内容,分享了map的一些简介代码示例,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • SpringAOP 如何通过JoinPoint获取参数名和值

    SpringAOP 如何通过JoinPoint获取参数名和值

    这篇文章主要介绍了SpringAOP 通过JoinPoint获取参数名和值的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Java面试题冲刺第三十天--数据库(6)

    Java面试题冲刺第三十天--数据库(6)

    这篇文章主要为大家分享了最有价值的三道关于数据库的面试题,涵盖内容全面,包括数据结构和算法相关的题目、经典面试编程题等,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • SpringBoot使用Feign调用其他服务接口

    SpringBoot使用Feign调用其他服务接口

    这篇文章主要介绍了SpringBoot使用Feign调用其他服务接口,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03

最新评论