SpringAOP_设置注入实现


AOP_面向切面编程初步了解

让我们先想象一个场景,你正在编写一个项目,在开发过程中的多个模块都有某段重复的代码,于是你选择将其抽象成一个方法,然后在需要的地方调用这个方法,当需要修改这段代码时只需要修改这个方法就行。有一天,你的Boss给了新的需求,需要再抽象出一个方法,然后再在各个需要这个方法的模块调用这个方法,这可能就让你头疼了,需要修改大量的代码,于是会想,能不能不修改源代码为系统业务添加某种功能呢?幸运的是,AOP可以很好的解决这个问题。

简单介绍

AOP:保证开发者不修改源代码的前提下,去为系统中的业务组件添加某种通用功能,本质是由AOP框架修改业务组件的多个方法的源代码,我们将其分为两类:

  • 静态AOP

    AOP 框架在编译阶段对程序源代码进行修改,生成了静态的 AOP 代理类(生成的*.class文件已经被改掉了,需要使用特定的编译器),比如 AspectJ。
  • 动态AOP:

    AOP 框架在运行阶段对动态生成代理对象(在内存中以 JDK 动态代理,或 CGlib 动态地生成 AOP 代理类),如 SpringAOP。

详细说明

Spring 的通知类型

名称 标签 说明
前置通知 < aop:before > 用于配置前置通知。指定增强的方法在切入点方法之前执行
后置通知 < aop:after-returning > 用于配置后置通知。指定增强的方法在切入点方法之后执行
环绕通知 < aop:around > 用于配置环绕通知。指定增强的方法在切入点方法之前和之后都执行
异常通知 < aop:throwing > 用于配置异常抛出通知。指定增强的方法在出现异常时执行
最终通知 < aop:after > 用于配置最终通知。无论增强方式执行是否有异常都会执行

实战演练

导入依赖包

    <dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<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("环绕通知");
}
}

创建一个切面类

public class VisitAspect {
//前置通知
public void visitBefore(JoinPoint joinPoint){
System.out.println("口令:");
}
//最终通知,无论是否报错,都执行
public void visitAfter(JoinPoint joinPoint){
System.out.println("输入完成");
}
//后置通知报错不执行
public void visitSuccess(JoinPoint joinPoint){
System.out.println("请求成功,欢迎");
}
//异常通知,报错后执行
public void visitThrow(JoinPoint joinPoint, Throwable ex){
System.out.println("请求失败,拒绝");
}
//环绕通知,如果报错只执行前一句
public Object visitAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("-------环绕-------");
Object obj = proceedingJoinPoint.proceed();
System.out.println("-------环绕-------");
return obj;
}
}

配置xml文件,需要添加第三方约束

    <bean id="userDao" class="Spring_AOP.service.impl.VisitServiceImpl"></bean>
<bean id="aspect" class="Spring_AOP.service.VisitAspect"></bean> <aop:config>
<aop:pointcut id="pointcut" expression="execution(* Spring_AOP.service.impl.VisitServiceImpl.visit(..))"/>
<aop:pointcut id="pointcut1" expression="execution(* Spring_AOP.service.impl.VisitServiceImpl.around())"/>
<aop:aspect ref="aspect">
<aop:before method="visitBefore" pointcut-ref="pointcut"></aop:before>
<aop:after method="visitAfter" pointcut-ref="pointcut"></aop:after>
<aop:after-returning method="visitSuccess" pointcut-ref="pointcut"></aop:after-returning>
<aop:around method="visitAround" pointcut-ref="pointcut1"></aop:around>
<!-- 报错后执行aop:after-throwing -->
<aop:after-throwing method="visitThrow" pointcut-ref="pointcut" throwing="ex"></aop:after-throwing>
</aop:aspect>
</aop:config>

注,对于execution()

1、execution(): 表达式主体 (必须加上execution)。

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

3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,cn.smd.service.impl包、子孙包下所有类的方法。

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

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

书写的注意事项:execution(* cn.smd.service.impl..(..))


创建一个测试类

 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

请求失败,拒绝

输入完成

-------环绕-------

环绕通知

-------环绕-------


总结

  • SpringAOP进一步降低组件的耦合,实现解耦合
  • 可以更好的监控程序,进行权限拦截
  • 注:学习AOP设置注入时需要注意出现报错时各个通知的状态

以上就是以注解实现SpringAOP框架设置注入的实现,如有错误,麻烦指出,感谢耐心到现在的朋友ᕕ( ᐛ )ᕗ ---By 不断努力的Yang

最新文章

  1. [转]runtime 消息机制
  2. Mongodb无法访问28107的问题
  3. c语言检测文件是否存在int __cdecl access(const char *, int);
  4. Nginx+Tomcat动静态资源分离
  5. 手把手教你写电商爬虫-第三课 实战尚妆网AJAX请求处理和内容提取
  6. 【转】win7与VMware ubuntu虚拟机实现文件共享(最后一定要装open-vm-dkms插件)
  7. Java基础知识强化之IO流笔记13:递归之不死神兔问题(斐波那契数列)
  8. 在windows下安装mysql
  9. 关于iPhone
  10. ZooKeeper场景实践:(2)集中式配置管理
  11. 设计模式 单例模式(Singleton) [ 转载2 ]
  12. mysql之delete语法
  13. Docker Toolbox替换默认docker machine的存储位置
  14. blender 3d模型软件介绍(开源,免费,易用,强大)
  15. Python模块——configparser
  16. Tomcat启动失败
  17. SQL注入之Sqli-labs系列第五关和第六关(基于GET型的报错注入)
  18. 微信小程序组件
  19. ajax 方法的使用以及方法中各参数的含义
  20. css3画图那些事(三角形、圆形、梯形等)

热门文章

  1. go map嵌套 map的value可以是任意类型
  2. 永恒之蓝(MS17-010)检测与利用
  3. Java开发不懂Docker,学尽Java也枉然,阿里P8架构师手把手带你玩转Docker实战
  4. Vue.js 可排序列表 (Sortable &amp; Searchable Tables) 组件
  5. 解决新版谷歌浏览器在http请求下无法开启麦克风问题
  6. MySQL入门(5)——运算符
  7. 20个最有用的Python数据科学库
  8. HTML5中window.postMessage,在两个页面之间的数据传递
  9. 从键盘读入学生成绩,找出最高分, 并输出学生成绩等级(Java)
  10. 使用命令行编译Qt程序