简述SpringAOP的实现原理_spring AOP

简述SpringAOP的实现原理_spring AOPAOP用Spring需要导入包<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency>方式一:使用Spring接口编写javapackage com.kuang.log;


Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

AOP

用Spring需要导入包

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

在这里插入图片描述
在这里插入图片描述

方式一:使用Spring接口

  1. 编写java
package com.kuang.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

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

package com.kuang.log;

import org.aspectj.lang.annotation.AfterReturning;
import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice { 
   

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

  1. 编写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:context="http://www.springframework.org/schema/context" 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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--注册bean-->
    <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接口-->
    <!--配置opp:需要导入opp的约束-->
    <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>

编写测试类

import com.kuang.pojo.User;
import com.kuang.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest  { 
   
    public static void main(String[] args) { 
   
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        UserService userService = (UserService)context.getBean("userService");
        userService.add();
    }
}

在这里插入图片描述
方式二:使用自定义类来实现AOP

package com.kuang.diy;

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

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

}

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

    <!--方式二:自定义类-->
    <bean id="diy" class="com.kuang.diy.DiyPointCut"/>
    <aop:config>
        <!--自定义切面,ref要引用的类-->
        <aop:aspect 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>
</beans>

测试

import com.kuang.pojo.User;
import com.kuang.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest  { 
   
    public static void main(String[] args) { 
   
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        UserService userService = (UserService)context.getBean("userService");
        userService.add();
    }
}

在这里插入图片描述

方式三:使用注解实现
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:context="http://www.springframework.org/schema/context" 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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--注册bean-->
    <bean id="userService" class="com.kuang.service.UserServiceImpl"/>

    <bean id="diy" class="com.kuang.diy.DiyPointCut"/>
    <!--方式三:自定义类-->
    <!--开启注解支持-->
    <aop:aspectj-autoproxy/>
</beans>
package com.kuang.diy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class DiyPointCut { 
   
    @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 jp) throws Throwable { 
   
        System.out.println("环绕前");

        Signature signature = jp.getSignature();//获得签名
        System.out.println("signature:" + signature);

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

    }

}

测试

import com.kuang.pojo.User;
import com.kuang.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest  { 
   
    public static void main(String[] args) { 
   
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        UserService userService = (UserService)context.getBean("userService");
        userService.add();
    }
}

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/168806.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • CAP理论详解

    CAP理论详解CAP理论在互联网界有着广泛的知名度,知识稍微宽泛一点的工程师都会把其作为衡量系统设计的准则。大家都非常清楚地理解了CAP:任何分布式系统在可用性、一致性、分区容错性方面,不能兼得,最多只能得其二,因此,任何分布式系统的设计只是在三者中的不同取舍而已。事实上,让人吃惊的是,CAP在国外的响力完全不如所想,相反还伴随着诸多的争论。下面我们系统地阐述一下CAP的来龙去脉。1.CAP的历史1985年Ly…

    2022年6月23日
  • 笔记本卡顿不流畅怎么解决_win10开机慢是什么原因

    笔记本卡顿不流畅怎么解决_win10开机慢是什么原因展开全部一般来说,引起电脑卡顿不流畅的无非就是软件和硬件问题,软件主要包括系统优化不佳,比如同时打e69da5e6ba903231313335323631343130323136353331333363396439开很多软件,造成电脑卡顿不流畅。硬件主要为电脑配置过低或者硬件故障,比如硬盘坏道、内存不足、散热不佳等造成的卡顿不流畅。卡顿不流畅原因一:电脑可能感染木马病毒解决方法:升级杀毒软件并且杀…

    2022年9月11日
  • 迭代器Python_python进阶路线

    迭代器Python_python进阶路线迭代器迭代是访问集合元素的一种方式。迭代器是一个可以记住遍历的位置的对象。迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。迭代器只能往前不会后退。可迭代对象我们已经知道可以对l

    2022年7月28日
  • PyPDF2 | 利用 Python 实现 PDF 分割

    PyPDF2 | 利用 Python 实现 PDF 分割1.PDF分割由于疫情影响被迫在家上网课,因此教材也只能用电子版。但有一门教材是对开的扫描版,导致在iPad上阅读很不友好,因此决定寻找一个工具将PDF对半分开。图1分割前的PDF在百度了一番后,发现大多都是使用AdobeAcrobat软件进行剪裁,这完全不Pythonic,因此又找了用Python处理PDF文件的方法,最后发现了PyPDF2这个库,本…

    2022年6月23日
  • 极光漏洞,”极光”ie漏洞,微软发布2010年第一个IE 0day漏洞“极光”警告、最新官方补丁和解决办法

    极光漏洞,”极光”ie漏洞,微软发布2010年第一个IE 0day漏洞“极光”警告、最新官方补丁和解决办法微软2010年1月14日晚发布公告称,黑客在最近的针对Google、Adobe以及其他公司的攻击中利用了IE零日漏洞。远程代码执行漏洞影响到各Windows版本上运行的近乎全部的IE版本。关键字:极光

    2022年7月2日
  • one step closer intersection_cut a striking figure

    one step closer intersection_cut a striking figure教程:1.下载软件压缩包文件,首先解压安装程序2.选择第一个选项,下一步3.设置软件安装路径以及其他设置4.运行主程序,安装完成,资源地址:FranzisCutOut9Professional中文版新功能1.色度键-消光现在每个人都可以成为抠图专家。使用色度键隔离方法可以轻松地将对象与均匀彩色背景分开2.分段掩蔽要完美地剪切图片的图案,将照片组合到理想的位置,并通过单击鼠标取出烦人的物体3.专业背景管理。背景管理是CutOut的新功能。并非每个摄影师(包括专业人士)

    2022年9月15日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号