Spring AOP快速使用教程
Spring是方法级别的AOP框架,我们主要也是以某个类的某个方法作为连接点,用动态代理的理论来说,就是要拦截哪个方法织入对应的AOP通知。为了更方便的测试我们首先创建一个接口
public interface RoleService {
public void printRole(Role role);
}
然后创建一个实现类
@Component
public class RoleServiceImpl implements RoleService {
public void printRole(Role role) {
System.out.println(role.toString());
}
}
这个类没啥特别,这个时候把printRole作为AOP的连接点,那么用动态代理的语言就是要为类RoleServiceImpl生成代理对象,然后拦截printRole方法,可以用于产生各种AOP通知方法。
接着我们来进行切面的创建,他如同一个拦截器,在Spring中只要使用@Aspect注解一个类,那么Spring IOC 容器就会认为这是一个切面了。
@Aspect
public class RoleAspect {
//在被代理对象的方法前调用 ,使用args来传递参数
@Before("execution(* com.aop.RoleServiceImpl.printRole(..)) && args(role,sort)")
public void before(Role role, int sort) {
System.out.println("before...");
}
//在被代理对象的方法后调用
@After("execution(* com.aop.RoleServiceImpl.printRole(..))")
public void after() {
System.out.println("after...");
}
//在被代理对象方法正常返回后调用
@AfterReturning("execution(* com.aop.RoleServiceImpl.printRole(..))")
public void afterRunning() {
System.out.println("afterRunning");
}
//在被代理对象方法抛出异常后使用
@AfterThrowing("execution(* com.aop.RoleServiceImpl.printRole(..))")
public void afterThrowing() {
System.out.println("afterThrowing");
}
//将验证角色对象是否为空的类加入到切面中
//value=""表示对某类进行增强,defaultImpl表示默认的实现类
@DeclareParents(value = "com.aop.RoleServiceImpl+", defaultImpl = RoleVerifierImpl.class)
public RoleVerifier roleVerifier;
}
此时连接点和切面我们都创建完成了,这个时候可以编写代码来测试AOP的内容,首先要对Spring的Bean进行配置,采用注解Java配置。
@Configuration
@EnableAspectJAutoProxy
@ComponentScan("com")
public class AppConfig {
@Bean
public RoleAspect getRoleAspect() {
return new RoleAspect();
}
}
测试AOP流程
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
RoleService roleService = (RoleService) ctx.getBean(RoleService.class);
//使用刚才创建的RoleVerifier来进行检测role对象是否为空
RoleVerifier verifier = (RoleVerifier) ctx.getBean(RoleVerifier.class);
Role role = new Role();
role.setId(1L);
role.setRoleName("张三");
role.setRoleNote("张三的备注信息");
System.out.println("##测试结果");
roleService.printRole(role);
System.out.println("#####################");
//测试异常通知
role = null;
roleService.printRole(role, 2);
}
}
##测试结果
before...
Role{id=1, roleName='张三', roleNote='张三的备注信息'}
1
afterRunning
after...
#####################
before...
afterThrowing ##异常通知
after...
Exception in thread "main" java.lang.NullPointerException
at com.aop.RoleServiceImpl.printRole(RoleServiceImpl.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
很显然切面的通知已经通过AOP织入约定的流程当中了,这时我们可以使用AOP来处理一些需要切面的场景了。
Spring AOP快速使用教程的更多相关文章
- 化繁就简,如何利用Spring AOP快速实现系统日志
1.引言 有关Spring AOP的概念就不细讲了,网上这样的文章一大堆,要讲我也不会比别人讲得更好,所以就不啰嗦了. 为什么要用Spring AOP呢?少写代码.专注自身业务逻辑实现(关注本身的业务 ...
- Spring Boot2 快速入门教程-到上手
Spring Boot2 教程合集 入门 纯 Java 代码搭建 SSM 环境 创建一个 Spring Boot 项目的三种方法 理解 Spring Boot 项目中的 parent 基础配置 配置文 ...
- spring boot入门教程——Spring Boot快速入门指南
Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...
- Spring Boot:快速入门教程
什么是Spring Boot? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...
- Spring系列22:Spring AOP 概念与快速入门篇
本文内容 Spring AOP含义和目标 AOP相关概念 声明式AOP快速入门 编程式创建代理对象 Spring AOP含义和目标 OOP: Object-oriented Programming 面 ...
- Spring Aop详尽教程
一.概念 AOP(Aspect Oriented Programming):面向切面编程. 面向切面编程(也叫面向方面编程),是目前软件开发中的一个热点,也是Spring框架中的一个重要内容.利用AO ...
- spring boot快速入门 7: 使用aop处理请求
样例:登陆拦截(aop简单样例) 第一步:在pom 文件中加入aop依赖 <!-- spring aop --> <dependency> <groupId>org ...
- Spring学习总结(4)——Spring AOP教程
一.概念 AOP(Aspect Oriented Programming):面向切面编程. 面向切面编程(也叫面向方面编程),是目前软件开发中的一个热点,也是Spring框架中的一个重要内容.利用AO ...
- 使用Spring AOP 实现日志管理(简单教程)
有时候,我们在做项目时会遇到这样的需求: 给XXX.java中的所有方法加上指定格式的日志输出. 针对这种指定类.或者指定方法进行共性操作的功能,我们完全可以使用Spring AOP来实现. 本文使用 ...
随机推荐
- Hadoop 3.1.2报错:xception in thread "main" org.apache.hadoop.fs.UnsupportedFileSystemException: No FileSystem for scheme "hdfs"
报错内容如下: Exception in thread "main" org.apache.hadoop.fs.UnsupportedFileSystemException: No ...
- 安卓xml布局中 android:paddingBottom="@dimen/activity_vertical_margin"是什么意思?
@dimen/activity_vertical_margin 是什么意思? @dimen/activity_vertical_margin这个的意思就是在你的values文件夹下面的dimens文件 ...
- numpy教程06---ndarray的进阶操作
欢迎关注公众号[Python开发实战], 获取更多内容! 工具-numpy numpy是使用Python进行数据科学的基础库.numpy以一个强大的N维数组对象为中心,它还包含有用的线性代数,傅里叶变 ...
- 从数据库中获取图片编号,然后通过request获取图片下载
import pandas as pd from pandas.core.dtypes.dtypes import register_extension_dtype from sqlalchemy i ...
- LC-54
给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素. 示例 1: 输入:matrix = [[1,2,3],[4,5,6],[7,8,9]] 输出:[1,2 ...
- mysql_install_db 一次修复密码
我用mysql 社区版进行的安装,在linux centos 操作系统下, yum install 方式系统默认安装时没有密码的,需要你及时设置,但是我操作多次后,并没有修改密码,启动和关闭多次以后就 ...
- 简单的js提示框,仅仅用jq和css就可以
首先定义一个盒子 1 .pop { 2 position: fixed; 3 top: 20%; 4 left: 50%; 5 transform: translate(-50%); 6 width: ...
- petite-vue源码剖析-沙箱模型
在解析v-if和v-for等指令时我们会看到通过evaluate执行指令值中的JavaScript表达式,而且能够读取当前作用域上的属性.而evaluate的实现如下: const evalCache ...
- 苹果手机Safri浏览器 js 解析问题
低系统版本的苹果手机的浏览器存在很多JS问题 一 date 问题 1.new Date() {至少10.3版本已下存在这个问题} 苹果手机只能识别 new Date('2017/04/12') 这 ...
- 分享一个为Linux创建的任务管理器,看起来就像Windows的任务管理器
关注「开源Linux」,选择"设为星标" 回复「学习」,有我为您特别筛选的学习资料~ 链接:https://www.linuxmi.com/sysmontask-linux-tas ...