关于spring:SpringAOP构造注入实现技巧分享

8次阅读

共计 3610 个字符,预计需要花费 10 分钟才能阅读完成。

AOP_面向切面编程初步理解

让咱们先设想一个场景,你正在编写一个我的项目,在开发过程中的多个模块都有某段反复的代码,于是你抉择将其形象成一个办法,而后在须要的中央调用这个办法,当须要批改这段代码时只须要批改这个办法就行。有一天,你的 Boss 给了新的需要,须要再形象出一个办法,而后再在各个须要这个办法的模块调用这个办法,这可能就让你头疼了,须要批改大量的代码,于是会想,能不能不批改源代码为零碎业务增加某种性能呢?侥幸的是,AOP 能够很好的解决这个问题。

简略介绍

AOP: 保障开发者不批改源代码的前提下,去为零碎中的业务组件增加某种通用性能, 实质是由 AOP 框架批改业务组件的多个办法的源代码,咱们将其分为两类:

动态 AOP
AOP 框架在编译阶段对程序源代码进行批改,生成了动态的 AOP 代理类(生成的 *.class 文件曾经被改掉了,须要应用特定的编译器),比方 AspectJ。
动静 AOP:
AOP 框架在运行阶段对动静生成代理对象(在内存中以 JDK 动静代理,或 CGlib 动静地生成 AOP 代理类),如 SpringAOP。

具体阐明
Spring 的告诉类型

名称 – 标签 – 阐明
前置告诉 –@Before– 用于配置前置告诉。指定加强的办法在切入点办法之前执行
后置告诉 –@AfterReturning– 用于配置后置告诉。指定加强的办法在切入点办法之后执行
盘绕告诉 –@Around– 用于配置盘绕告诉。指定加强的办法在切入点办法之前和之后都执行
异样告诉 –@AfterThrowing– 用于配置异样抛出告诉。指定加强的办法在出现异常时执行
最终告诉 –@After– 用于配置最终告诉。无论加强形式执行是否有异样都会执行
切面类注解 –@Aspect– 标注该以后类是一个切面类
断点注解 –@Pointcut– 应用一个返回值为 void、办法体为空的办法来命名切入点

实战演练
导入依赖包

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.5</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>5.3.5</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
    <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

创立一个加强类以及其接口

加强类接口:
public interface VisitService {

// 用于实现前置告诉,后置告诉,异样告诉,最终告诉
void visit(String str) throws Exception;

// 用于实现盘绕告诉
void around();

}

加强类:
public class VisitServiceImpl implements VisitService {

// 前置,后置,最终,异样告诉的加强类
public void visit(String str) throws Exception{System.out.println(str);
    if(!str.equalsIgnoreCase("agree")){throw new Exception("非法拜访");
    }
}
// 盘绕告诉的加强类
public void around() {System.out.println("盘绕告诉");
}

}

创立一个切面类
@Component(“VisitAspect”)
@Aspect // 标注以后 myAspect 是一个切面类
public class VisitAspect_anno {

// 定义切入点表达式
// 应用一个返回值为 void、办法体为空的办法来命名切入点
@Pointcut("execution(* Spring_AOP.service.impl.VisitServiceImpl.visit(..))")
private void v1() {}

// 前置告诉
@Before("v1()")
public void visitBefore(JoinPoint joinPoint) {System.out.println("口令:");
}

@After("v1()")
// 最终告诉,无论是否报错,都执行
public void visitAfter(JoinPoint joinPoint) {System.out.println("输出实现");
}

@AfterReturning("v1()")
// 后置告诉报错不执行
public void visitSuccess(JoinPoint joinPoint) {System.out.println("申请胜利,欢送");
}

@AfterThrowing(value = "v1()",throwing = "ex")
// 异样告诉,报错后执行
public void visitThrow(JoinPoint joinPoint, Throwable ex) {System.out.println("申请失败,回绝");
}

@Around("execution(* Spring_AOP.service.impl.VisitServiceImpl.around())")
// 盘绕告诉,如果报错只执行前一句
public Object visitAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println("------- 盘绕 -------");
    Object obj = proceedingJoinPoint.proceed();
    System.out.println("------------------");
    return obj;
}

}

配置 xml 文件

<!-- 基于注解的申明式 AspectJ -->
<context:component-scan base-package="Spring_AOP" />
<!-- 启动基于注解的申明式 AspectJ 反对一 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

创立一个测试类

public class visitTest {

@Test
public void VisitTest(){ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext_AOP.xml");
    VisitService visitService = app.getBean(VisitService.class);
    try {visitService.visit("agree");
    } catch (Exception e) {e.printStackTrace();
    }
    try {visitService.visit("ok");
    } catch (Exception e) {e.printStackTrace();
    }
    visitService.around();}

}

测试运行

口令:
agree
申请胜利,欢送
输出实现
口令:
ok
申请失败,回绝
输出实现
——- 盘绕 ——-
盘绕告诉
——- 盘绕 ——-


总结

应用结构注入能够更不便的实现 AOP 模式,然而同样与设置注入相比各有千秋。
以上就是以注解实现 SpringAOP 框架结构注入的实现。

正文完
 0