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来实现. 本文使用 ...
随机推荐
- 关于mysql使用utf8编码在cmd窗口无法添加中文数据的问题以及解决 方法二
如果非要用cmd窗口的话,那么可以加这句话,set names gbk:
- vue行内动态添加样式或者动态添加类名
还是记录一下吧(๑•ᴗ•๑) <li :style="{backgroundImage:`url(${item.pic})`}" @click="chooseVip ...
- spring-xml实现aop-通知的种类
如果本代码有疑问,请访问spring-aop快速入门或者spring-aop动态代理技术(底层分析) 1.导入aop的相关坐标 <dependency> <groupId>or ...
- intel 82599网卡(ixgbe系列)术语表
Intel® 82599 10 GbE Controller Datasheet 15.0 Glossary and Acronyms 术语表 缩写 英文解释 中文解释 1 KB A value of ...
- linux中LNMP架构和location用法
location 使用Nginx Location可以控制访问网站的路径,但一个server可以有多个location配置, 多个location的优先级该如何区分 location匹配符号 匹配符 ...
- Windows下FFMEPG编译
FFMPEG在Windows下编译(MIMO431) /************************************************************************ ...
- Python学习报告及后续学习计划
第一次有学习Python的想法是源于寒假在家的时候,高中同学问我是否学了Python(用于深度学习),当时就到b站收藏了黑马最新的教学视频,但是"收藏过等于我看了",后续就是过完年 ...
- 网络协议自动化逆向工具开山鼻祖discoverer 分析
本文系原创,转载请说明出处:信安科研人 也可关注微信公众号:信安科研人 原论文发表在2007年的USENIX上,链接如下:https://www.usenix.org/legacy/event/sec ...
- 从零搭建Pytorch模型教程(三)搭建Transformer网络
前言 本文介绍了Transformer的基本流程,分块的两种实现方式,Position Emebdding的几种实现方式,Encoder的实现方式,最后分类的两种方式,以及最重要的数据格式的介绍. ...
- 【GPLT】 2018年天梯赛全国总决赛 L2-2 小字辈(c++)
题目: 这一题并不是很难,属于常规的图论遍历题,这里我是用的bfs(dfs应该也可以,但明显bfs简单一些). 本人写的时候写了很多没必要头文件,自己可以根据内容删去,必要的我会写上注释 如有错误,请 ...