springBoot AOP学习(一)
AOP学习(一)
1.简介
AOp:面向切面编程,相对于OOP面向对象编程。
Spring的AOP的存在目的是为了解耦。AOP可以让一切类共享相同的行为。在OOP中只能通过继承类或者实现接口,使代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类上,AOP弥补了OOP的不足。
Spring支持AspectJ的注解式切面编程。
(1)使用@Aspect声明是一个切面;
(2)使用@After、@Before、@Around定义通知(Advice),可直接将拦截规则(切点)作为参数;
(3)其中@After、@Before、@Around参数的拦截规则为切点(Pointcut),为了使切点复用,可使用@Pointcut 专门定义拦截规则,然后在@After、@Before、@Around的参数中调用;
(4)其中符合条件的每一个被拦截处为连接点(JoinPoint)。
下面示例将演示基于注解拦截和基于方法规则拦截两种方式,演示一种模拟记录操作的日志系统的实现。其中注解式拦截能够很好地控制要拦截的粒度和获得更丰富的信息,Spring本身在事务处理(@Transcational)和数据缓存(@Cacheable)等都使用此种形式的拦截。
2.示例
(1)新建springboot项目,在pom.xml文件中添加spring aop 以及支持AspectJ 依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency> (2)注解拦截,编写拦截规则的注解
/**
* 自定义拦截规则注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String name();
} (3)编写使用注解拦截的 业务类
/**
* 使用注解的被拦截类
*/
@Service
public class DemoAnnotationService { @Action(name="注解式拦截的add操作")
public void add(){}
}
(4)编写使用方法规则拦截的 业务类
/**
* 使用方法规则的被拦截类
*/
@Service
public class DemoMethodService {
public void add(){}
} (5)编写切面
/**
* 切面
*/
@Aspect//注解声明一个切面
@Component//让切面成为Spring容器管理的Bean
public class LoginAspect { //通过@Pointcut注解声明切点
@Pointcut("@annotation(com.wenhuang.springboot.aop.Action)")
public void annotationPointCut(){}; //通过@After 注解声明一个通知,并使用@Pointcut定义切点
@After("annotationPointCut()")
public void after(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
Method method =signature.getMethod();
Action action =method.getDeclaredAnnotation(Action.class);
System.out.println("注解式拦截,"+ action.name());//通过反射获取注解上的属性
} //通过@Before 注解声明一个通知,直接使用拦截器规则作为参数
@Before("execution(* com.wenhuang.springboot.aop.service.DemoMethodService.*(..))")
public void before(JoinPoint joinPoint){
MethodSignature signature=(MethodSignature)joinPoint.getSignature();
Method method =signature.getMethod();
System.out.println("方法规则式拦截,"+method.getName());
}
}
(6)配置类
@Configuration
@ComponentScan("com.wenhuang.springboot.aop")
@EnableAspectJAutoProxy //使用该注解开启Spring对AspectJ代理的支持
public class AopConfig {
}
(7)运行
public class Main {
public static void main(String[] args){
AnnotationConfigApplicationContext context
= new AnnotationConfigApplicationContext(AopConfig.class);
DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);
demoAnnotationService.add();
demoMethodService.add();
context.close();
}
}
结果如下

springBoot AOP学习(一)的更多相关文章
- SpringBoot+Shiro学习(七):Filter过滤器管理
SpringBoot+Shiro学习(七):Filter过滤器管理 Hiwayz 关注 0.5 2018.09.06 19:09* 字数 1070 阅读 5922评论 1喜欢 20 先从我们写的一个 ...
- Springboot 框架学习
Springboot 框架学习 前言 Spring Boot是Spring 官方的顶级项目之一,她的其他小伙伴还有Spring Cloud.Spring Framework.Spring Data等等 ...
- springboot+aop切点记录请求和响应信息
本篇主要分享的是springboot中结合aop方式来记录请求参数和响应的数据信息:这里主要讲解两种切入点方式,一种方法切入,一种注解切入:首先创建个springboot测试工程并通过maven添加如 ...
- SpringBoot+AOP整合
SpringBoot+AOP整合 https://blog.csdn.net/lmb55/article/details/82470388 https://www.cnblogs.com/onlyma ...
- springboot aop 不生效原因解决
最近参照资料创建Springboot AOP ,结果运行后aop死活不生效. 查明原因: 是我在创建AOP类时选择了Aspect类型,创建后才把这个文件改为Class类型,导致一直不生效, 代码配置这 ...
- springboot aop 自定义注解方式实现完善日志记录(完整源码)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 一:功能简介 本文主要记录如何使用aop切面的方式来实现日志记录功能. 主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型 ...
- springboot aop 自定义注解方式实现一套完善的日志记录(完整源码)
https://www.cnblogs.com/wenjunwei/p/9639909.html https://blog.csdn.net/tyrant_800/article/details/78 ...
- Aop学习笔记
在学习编程这段时间我想大家都是习惯了面向过程或者面向对象的思想来编程,较少或者没有接触过面向方面编程的思想. 那么什么是面向方面(Aspect)——其实就是与核心业务处理逻辑无关的切面,例如记录日志. ...
- spring 学习(三):aop 学习
spring 学习(三):aop 学习 aop 概念 1 aop:面向切面(方面)编程,扩展功能不修改源代码实现 2 AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码 3 aop底层使用动态代 ...
随机推荐
- HDU 5710 Digit Sum
Let S(N)S(N) be digit-sum of NN, i.e S(109)=10,S(6)=6S(109)=10,S(6)=6. If two positive integers a,ba ...
- 『MXNet』第三弹_Gluon模型参数
MXNet中含有init包,它包含了多种模型初始化方法. from mxnet import init, nd from mxnet.gluon import nn net = nn.Sequenti ...
- java使用线程设置定时任务
private static int a=0; public static void main( String[] args ) { timer(); } public static void tim ...
- SpringBoot项目Shiro的实现(一)
一.Shiro的简单介绍 Shiro是Apache下的一个开源项目,我们称之谓Apache Shiro,它是一个易用与Java项目的安全框架,提供了认证.授权.加密.会话管理,与Spring Secu ...
- bootstrap居中
1.页面 <div class="container"> <div class="row clearfix"> <div clas ...
- hibernate建表默认为UTF-8编码
一.问题: hibernate自动建表的编码应该是数据默认的编码格式,一般也不是utf-8.所以想要建表默认的编码是UTF-8,应该怎么做呢? 二.解决方法: 拿mysql举例: (一).修改hibe ...
- [CodeForces - 614C] C - Peter and Snow Blower
C - Peter and Snow Blower Peter got a new snow blower as a New Year present. Of course, Peter decide ...
- Django之权限管理插件
一.功能分析: 一个成熟的web应用,对权限的控制.管理是不可少的:对于一个web应用来说是什么权限? 这要从web应用的使用说起,用户在浏览器输入一个url,访问server端,server端返回这 ...
- 【内存泄漏】 C/C++内存泄漏及其检测工具
对于一个c/c++程序员来说,内存泄漏是一个常见的也是令人头疼的问题.已经有许多技术被研究出来以应对这个问题,比如 Smart Pointer,Garbage Collection等.Smart Po ...
- 我在大学毕业后学习Linux系统的心得经验
扣着手指头一算,自己已经毕业快半年了,这半年莫名其妙进外包圈子溜达了一圈,有幸退的早还是正常干一些事情吧,外包终究不是太适合刚入社会的毕业生,今天想把自己的学习和工作经验写成一篇文章,希望能够帮助到正 ...