• 基于注解的AOP开发


    目录

     基于注解的AOP开发

    编写测试 

     注解配置AOP详解

    注解通知的类型

     切点表达式的抽取


     基于注解的AOP开发

    快速入门,基于注解的aop开发步骤

    ①创建目标接口和目标类(内部有切点)

    ②创建切面类(内部有增强方法)

    ③将目标类和切面类的对象创建权交给spring

    ④在切面类中使用注解配置织入关系

    ⑤在配置文件中开启组件扫描和AOP的自动代理

    ⑥测试

    编写测试 

    其中Target类下

    1. package anno;
    2. import org.springframework.stereotype.Component;
    3. //交给spring容器,起个名为target
    4. @Component("target")
    5. public class Target implements TargetInterface {
    6. public void save() {
    7. System.out.println("save running。。。");
    8. }
    9. }

     Interface类下

    1. package anno;
    2. public interface TargetInterface {
    3. public void save();
    4. }

     MyAspect切面类下

    1. package anno;
    2. import org.aspectj.lang.annotation.Aspect;
    3. import org.aspectj.lang.annotation.Before;
    4. import org.springframework.stereotype.Component;
    5. //交给spring容器
    6. @Component("myAspect")
    7. @Aspect //标注当前MyAspect是一个切面类
    8. public class MyAspect {
    9. @Before("execution(* anno.*.*(..))")
    10. public void before(){
    11. System.out.println("前置增强....");
    12. }
    13. }

     AnnoTest测试类下

    1. package anno;
    2. import org.junit.Test;
    3. import org.junit.runner.RunWith;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.test.context.ContextConfiguration;
    6. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    7. @RunWith(SpringJUnit4ClassRunner.class)
    8. @ContextConfiguration("classpath:applicationContext_anno.xml")
    9. public class AnnoTest {
    10. @Autowired
    11. private TargetInterface target;
    12. @Test
    13. public void test1(){
    14. target.save();
    15. }
    16. }

    applicationContext_anno.xml配置文件

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:aop="http://www.springframework.org/schema/aop"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    8. ">
    9. <context:component-scan base-package="anno"/>
    10. <aop:aspectj-autoproxy/>
    11. beans>

    运行结果 

     注解配置AOP详解

    注解通知的类型

    通知的配置语法:@通知注解("切点表达式")

     切点表达式的抽取

    同xml配置aop一样。我们可以将切点表达式抽取,抽取方式是在切面内定义方法,早该方法上使用@Pointcut注解定义切点表达式,然后在在增强注解中进行引用。

     切面类中

    1. package anno;
    2. import org.aspectj.lang.annotation.AfterReturning;
    3. import org.aspectj.lang.annotation.Aspect;
    4. import org.aspectj.lang.annotation.Before;
    5. import org.aspectj.lang.annotation.Pointcut;
    6. import org.springframework.stereotype.Component;
    7. //交给spring容器
    8. @Component("myAspect")
    9. @Aspect //标注当前MyAspect是一个切面类
    10. public class MyAspect {
    11. @Before("execution(* anno.*.*(..))")
    12. public void before(){
    13. System.out.println("前置增强....");
    14. }
    15. //引入切点表达式方法
    16. @AfterReturning("pointcut()")//或者写MyAspect.pointcut()
    17. public void afterReturn(){
    18. System.out.println("后置增强....");
    19. }
    20. //定义切点表达式方法
    21. @Pointcut("execution(* anno.*.*(..))")
    22. public void pointcut(){ }
    23. }

    运行结果 

  • 相关阅读:
    BGP联盟实验
    Leetcode646. 最长数对链
    基于springboot小区物业管理系统
    zabbix添加微信报警
    微信小程序中应用van-calendar时加载时间过长,以及设置min-data无效的问题解决
    ArgoCD技术总结待续
    RDPCrystal EDI SDK 10.0.4.X Crack
    【Vue】二:Vue核心处理---事件处理
    9.4-9.6 章读书笔记
    mysql explain extra值枚举
  • 原文地址:https://blog.csdn.net/weixin_60719453/article/details/126318961