[Spring Boot] Aspect Intro
Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects. (Such concerns are often termed crosscutting concerns in AOP literature.)
One of the key components of Spring is the AOP framework. While the Spring IoC container does not depend on AOP, meaning you do not need to use AOP if you don't want to, AOP complements Spring IoC to provide a very capable middleware solution. [1]
It is powerful tool, it allow you to automaticlly watching Classes execution, we can preform some opreation before it or after it. It is a little bit similar to Angular Router Guards, before we enter a router, we can use Guards to check Auth.
package com.in28minutes.spring.aop.springaop.aspect; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration; //AOP
//Configuration
@Aspect
@Configuration
public class UseAccessAspect { private Logger logger = LoggerFactory.getLogger(this.getClass()); //What kind of method calls I would intercept
//execution(* PACKAGE.*.*(..)) @Before("execution(* com.in28minutes.spring.aop.springaop.business.*.*(..))")
public void before(JoinPoint joinPoint){
logger.info(" Check for user access ");
logger.info(" Allowed execution for {}", joinPoint);
}
}
package com.in28minutes.spring.aop.springaop.business; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.in28minutes.spring.aop.springaop.data.Dao1; @Service
public class Business1 { @Autowired
private Dao1 dao1; public String calculateSomething(){
//Business Logic
return dao1.retrieveSomething();
}
}
@AfterReturning(value = "com.in28minutes.spring.aop.springaop.aspect.CommonJoinPointConfig.businessLayerExecution()",
returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
logger.info("{} returned with value {}", joinPoint, result);
} @After(value = "com.in28minutes.spring.aop.springaop.aspect.CommonJoinPointConfig.businessLayerExecution()")
public void after(JoinPoint joinPoint) {
logger.info("after execution of {}", joinPoint);
}
Anytime 'Business1' is running, the Aspect will kick in.
[Spring Boot] Aspect Intro的更多相关文章
- 玩转spring boot——AOP与表单验证
AOP在大多数的情况下的应用场景是:日志和验证.至于AOP的理论知识我就不做赘述.而AOP的通知类型有好几种,今天的例子我只选一个有代表意义的“环绕通知”来演示. 一.AOP入门 修改“pom.xml ...
- Spring Boot 乐观锁加锁失败 - 集成AOP
Spring Boot with AOP 手头上的项目使用了Spring Boot, 在高并发的情况下,经常出现乐观锁加锁失败的情况(OptimisticLockingFailureException ...
- 一键式Spring集成工具 Spring Boot
最近公司使用Spring boot进行开发,稍微了解一下,不过自我感觉把集中式配置applicate.properties搞明白,注解用过Spring MVC的boot绝对没问题的 比如拦截器:@As ...
- Spring Boot Admin的使用
http://www.jianshu.com/p/e20a5f42a395 ******************************* 上一篇文章中了解了Spring Boot提供的监控接口,例如 ...
- Manage Spring Boot Logs with Elasticsearch, Logstash and Kibana
下载地址:https://www.elastic.co/downloads When time comes to deploy a new project, one often overlooked ...
- Spring boot实现数据库读写分离
背景 数据库配置主从之后,如何在代码层面实现读写分离? 用户自定义设置数据库路由 Spring boot提供了AbstractRoutingDataSource根据用户定义的规则选择当前的数据库,这样 ...
- spring boot 中 Mybatis plus 多数据源的配置方法
最近在学习spring boot,发现在jar包依赖方面做很少的工作量就可以了,对于数据库操作,我用的比较多的是mybatis plus,在中央仓库已经有mybatis-plus的插件了,对于单数据源 ...
- 四、Spring Boot 多数据源 自动切换
实现案例场景: 某系统除了需要从自己的主要数据库上读取和管理数据外,还有一部分业务涉及到其他多个数据库,要求可以在任何方法上可以灵活指定具体要操作的数据库.为了在开发中以最简单的方法使用,本文基于注解 ...
- Spring Boot实战:拦截器与过滤器
一.拦截器与过滤器 在讲Spring boot之前,我们先了解一下过滤器和拦截器.这两者在功能方面很类似,但是在具体技术实现方面,差距还是比较大的.在分析两者的区别之前,我们先理解一下AOP的概念,A ...
随机推荐
- 【环境搭建】Angular (含Hello World)
一.环境安装 1.安装node.js 下载路径:https://nodejs.org/en/download/ 命令行验证: 2.安装ts.cli ts: npm install -g typescr ...
- Linux系列(6):入门之文件与目录管理
你知道常见的目录操作吗? 知道如何查询文件内容吗? 了解 umask 指令吗,知道如何查看和设置文件的默认权限吗? 知道文件的隐藏属性吗,了解如何设置(chattr指令)并查看(lsattr指令)吗? ...
- Codeforces 1240B. Sequence Sorting
传送门 分析题目发现如果把某个数 $x$ 往左移,那么之后所有小于 $x$ 的数也都要往左移 如果把 $x$ 往右移,那么之后所有大于 $x$ 的数也都要往右移 考虑我们首先一定有一个操作 $n$ 次 ...
- 快速上手小程序的mpvue框架
一.什么是mpvue框架? mpvue 是一个使用 Vue.js 开发小程序的前端框架.框架基于 Vue.js 核心(所以建议熟练掌握vue再使用mpvue框架,否则还是建议去使用原生框架去写小程序) ...
- lua加载DLL
.cpp //若没有在项目属性--库文件.依赖文件.包含添加.则添加一下路径 #pragma comment (lib,"lua5.1.lib") #include " ...
- C++内存分配和分区
1.分配方式 静态分配: 静态区(全局区),分配全局变量.静态变量优先于main函数. 动态分配: (1)自动分配:栈区(2)手动分配:堆区,(malloc,free) (new,delete) 2. ...
- Guava动态调用方法
前言 大家在Coding的时候,经常会遇到这样一个情况,根据不同的条件去执行对应的代码.我们通常的处理方式是利用if-else判断,或者直接switch-case,特别是jdk1.6之后,swith开 ...
- 帝国cms 加载更多的实现(父栏目以及子栏目都可以实现)
1. <div class="pagelist"> <span id="loadmore" class="btn" sty ...
- Struts简介
一.简介 Apache Struts 2最初被称为WebWork 2,它是一个简洁的.可扩展的框架,可用于创建企业级Java web应用程序.设计这个框架是为了从构建.部署.到应用程序维护方面来简化整 ...
- python3中SYS模块
sys.argv 命令行参数List,第一个元素是程序本身路径sys.modules 返回系统导入的模块字段,key是模块名,value是模块sys.exit ...