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的更多相关文章

  1. 玩转spring boot——AOP与表单验证

    AOP在大多数的情况下的应用场景是:日志和验证.至于AOP的理论知识我就不做赘述.而AOP的通知类型有好几种,今天的例子我只选一个有代表意义的“环绕通知”来演示. 一.AOP入门 修改“pom.xml ...

  2. Spring Boot 乐观锁加锁失败 - 集成AOP

    Spring Boot with AOP 手头上的项目使用了Spring Boot, 在高并发的情况下,经常出现乐观锁加锁失败的情况(OptimisticLockingFailureException ...

  3. 一键式Spring集成工具 Spring Boot

    最近公司使用Spring boot进行开发,稍微了解一下,不过自我感觉把集中式配置applicate.properties搞明白,注解用过Spring MVC的boot绝对没问题的 比如拦截器:@As ...

  4. Spring Boot Admin的使用

    http://www.jianshu.com/p/e20a5f42a395 ******************************* 上一篇文章中了解了Spring Boot提供的监控接口,例如 ...

  5. 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 ...

  6. Spring boot实现数据库读写分离

    背景 数据库配置主从之后,如何在代码层面实现读写分离? 用户自定义设置数据库路由 Spring boot提供了AbstractRoutingDataSource根据用户定义的规则选择当前的数据库,这样 ...

  7. spring boot 中 Mybatis plus 多数据源的配置方法

    最近在学习spring boot,发现在jar包依赖方面做很少的工作量就可以了,对于数据库操作,我用的比较多的是mybatis plus,在中央仓库已经有mybatis-plus的插件了,对于单数据源 ...

  8. 四、Spring Boot 多数据源 自动切换

    实现案例场景: 某系统除了需要从自己的主要数据库上读取和管理数据外,还有一部分业务涉及到其他多个数据库,要求可以在任何方法上可以灵活指定具体要操作的数据库.为了在开发中以最简单的方法使用,本文基于注解 ...

  9. Spring Boot实战:拦截器与过滤器

    一.拦截器与过滤器 在讲Spring boot之前,我们先了解一下过滤器和拦截器.这两者在功能方面很类似,但是在具体技术实现方面,差距还是比较大的.在分析两者的区别之前,我们先理解一下AOP的概念,A ...

随机推荐

  1. centos git clone 报错 fatal: HTTP request failed 解决办法

    git clone报错提示 git clone https://github.com/xxxx.git Initialized empty Git repository in /root/xxxx/. ...

  2. git合并时冲突<<<<<<< HEAD

    <<<<<<< HEAD 本地代码 ======= 拉下来的代码 >>>>>>>

  3. 一文搞懂 Java 中的枚举,写得非常好!

    知识点 概念 enum的全称为 enumeration, 是 JDK 1.5 中引入的新特性. 在Java中,被 enum关键字修饰的类型就是枚举类型.形式如下: enum Color { RED, ...

  4. day05-06

    day05 上传下载 下载工具软件: lrzsz 安装方法: yum install lrzsz -y 下载命令就是sz 上传命令就是rz 下载到widow实列 sz 文件名 上传到linux 执行r ...

  5. C#解决界面闪烁

    添加缓冲: private void SetDoubleBuffer() { base.SetStyle( ControlStyles.OptimizedDoubleBuffer | ControlS ...

  6. MVC中的Action过滤器

    Action过滤器可以用在调用动作方法之前或之后,执行一些特殊的逻辑,比如用登录验证: Action过滤器实现IActionFilter接口,该接口有两个方法: public interface IA ...

  7. 你不知道的css各类布局(一)之固定布局、静态布局

    前言 当为大量用户设计网站时,设计人员必须考虑到访问者的差异: 屏幕分辨率, 浏览器的选择, 是否在浏览器最大化状态, 浏览器的额外工具栏的开启(历史记录,书签等), 操作系统和硬件. 我们知道css ...

  8. vue 项目中如何在页面刷新的状态下保留数据

    1.问题:在vue项目中,刷新页面之后,我当前打开的所有菜单,都消失,我如何实现刷新之后页面仍然是刷新之前的状态 效果图: 解决方法: 使用vuex作状态管理: 将vuex里面的数据同步更新到loca ...

  9. 创建LEANGOO项目

    转自:https://www.leangoo.com/leangoo_guide/leangoo_guide_create_project.html#toggle-id-3 一个项目包括多个看板和多名 ...

  10. 卡片拖拽(vue拖拽事件)

    <template> <div class="wrapper wrapper-content" id="main" v-cloak> & ...