前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >简述SpringAOP的实现原理_spring AOP

简述SpringAOP的实现原理_spring AOP

作者头像
全栈程序员站长
发布2022-09-21 08:40:24
3260
发布2022-09-21 08:40:24
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

AOP

用Spring需要导入包

代码语言:javascript
复制
<dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.4</version>  
</dependency>
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

方式一:使用Spring接口

  1. 编写java
代码语言:javascript
复制
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() + "被执行了");
    }
}
代码语言:javascript
复制
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文件
代码语言:javascript
复制
<?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>

编写测试类

代码语言:javascript
复制
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

代码语言:javascript
复制
package com.kuang.diy;

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

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

}

xml配置文件

代码语言:javascript
复制
<?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>

测试

代码语言:javascript
复制
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配置

代码语言:javascript
复制
<?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>
代码语言:javascript
复制
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("环绕后");

    }

}

测试

代码语言:javascript
复制
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.cn/168806.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • AOP
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档