1、引入依赖

    <!--spring aop依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>6.0.2</version>
</dependency>
<!--spring aspects依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>6.0.2</version>
</dependency>

2、定义接口和实现类

public interface Calculator {
int add(int a,int b);
int sub(int a,int b);
int mul(int a,int b);
int div(int a,int b);
}
public class CalculatorImpl implements Calculator{
@Override
public int add(int a, int b) {
int res = a+b;
System.out.println("res:"+res);
return res;
} @Override
public int sub(int a, int b) {
int res = a-b;
System.out.println("res:"+res);
return res;
} @Override
public int mul(int a, int b) {
int res = a*b;
System.out.println("res:"+res);
return res;
} @Override
public int div(int a, int b) {
int res = a/b;
System.out.println("res:"+res);
return res;
}
}

3、配置类

@Configuration
@ComponentScan(value = "com.jixian")
@EnableAspectJAutoProxy
public class SpringConfig {
}

4、切面类

@Aspect
@Component
public class AspectLog { @Before(value = "execution(public int com.jixian.CalculatorImpl.*(..))")
public void beforeMethod(){
System.out.println("前置通知...");
}
@After(value = "execution(* com.jixian.CalculatorImpl.*(..))")
public void afterMethod(){
System.out.println("后置通知...");
}
@AfterReturning(value = "execution(* com.jixian.CalculatorImpl.*(..))")
public void afterReturn(){
System.out.println("返回通知...");
}
@AfterThrowing(value = "execution(* com.jixian.CalculatorImpl.*(..))")
public void throwMethod(){
System.out.println("异常通知...");
}
@Around(value = "execution(* com.jixian.CalculatorImpl.*(..))")
public Object aroundMethod(ProceedingJoinPoint joinPoint){
Object o = null;
try {
System.out.println("环绕通知...方法执行前");
o = joinPoint.proceed();
System.out.println("环绕通知...返回值后");
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("环绕通知...出现异常");
} finally {
System.out.println("环绕通知...方法执行后");
}
return o;
}
}

5、测试

        ApplicationContext context = new AnnotationConfigApplicationContext("com.jixian");
Calculator calculator = context.getBean(Calculator.class);
calculator.div(12,1);

基于注解的AOP的更多相关文章

  1. Spring_Spring与AOP_AspectJ基于注解的AOP实现

    一.AspectJ.Spring与AOP的关系 AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Cl ...

  2. Spring基础知识之基于注解的AOP

    背景概念: 1)横切关注点:散布在应用中多处的功能称为横切关注点 2)通知(Advice):切面完成的工作.通知定了了切面是什么及何时调用. 5中可以应用的通知: 前置通知(Before):在目标方法 ...

  3. spring中基于注解使用AOP

    本文内容:spring中如何使用注解实现面向切面编程,以及如何使用自定义注解. 一个场景 比如用户登录,每个请求发起之前都会判断用户是否登录,如果每个请求都去判断一次,那就重复地做了很多事情,只要是有 ...

  4. AspectJ框架基于注解的AOP实现

    AspectJ的AOP实现:有两种方式,一种是基于XML配置文件,一种是基于注解的,由于注解更为常用,这里 这里只针对注解来学习. ---------------------------------- ...

  5. 阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置

    复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置 ...

  6. springboot自定义jdbc操作库+基于注解切点AOP

    发布时间:2018-11-08   技术:springboot+aop   概述 springBoot集成了自定义的jdbc操作类及AOP,因为spring自带的JdbcTemplate在实际项目中并 ...

  7. Spring AspectJ基于注解的AOP实现

    对于AOP这种编程思想,很多框架都进行了实现.Spring就是其中之一,可以完成面向切面编程.然而,AspectJ也实现了AOP的功能,且实现方式更为简捷,使用更加方便,而且还支持注解式开发.所以,S ...

  8. Spring 基于注解的AOP实现

    在本文开始之前,我要引入一张图,这张图的来源 https://blog.csdn.net/chenyao1994/article/details/79708496 ,版权归原作者所有,我借鉴了原作者的 ...

  9. 基于注解的AOP配置

    配置文件 spring配置文件中的约束 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...

  10. 阶段3 2.Spring_10.Spring中事务控制_2 作业-基于注解的AOP实现事务控制及问题分析_上

    创建maven的新项目 先复制坐标的依赖. 再把代码复制进来 先改造ioc的部分 复制上面一行代码.到下面 改成context 这里也是复制的上面两行代码.到下面改成context关键字 配置扫描的包 ...

随机推荐

  1. Node版本管理工具 - Nvm的下载、安装配置与使用

    1.业务背景 不同时期的项目使用的Node版本也不一样,随着版本的更新一直在使用的Node版本也在不断升级,本文介绍一个Node版本的管理工具,可自由切换版本. 2.Nvm下载与安装 1)下载 下载地 ...

  2. uboot启动过程 2

    512M内存, 地址范围 [0x80000000, 0xA0000000)  UBOOT原先位置 0x87800000, 移动后的位置0x9FF47000,   也就是最后 700多k,  前面的位置 ...

  3. ctfshow_web入门 反序列化(254~266)

    要是没接触过的师傅们,可以先看看这个 web 254 这个题没有考什么,get方式传入payload即可,这里xxxxxx,就是6gex而已 payload: ?username=xxxxxx& ...

  4. sys.argv的用法

    一.Python sys 模块 "sys" 是 "system",是一个系统模块,该模块提供了一些接口,用户访问python解释器自身使用和维护的变量,同时模块 ...

  5. 关于Visual Studio使用头文件"stdafx.h"报错原因

    当我们需要使用"stdafx.h"该头文件时,Visual Studio会报错,提示我们无法打开源文件"stdafx.h",实际上在Visual Studio中 ...

  6. Postgresql之闪回数据库示例

    一.摘要 在Oracle中,若发生重大的误操作,那么我们可以使用flashback database命令来把数据库整体闪回到过去的误操作的时间点,当然前提是需要打开数据库的闪回功能. 在PG中,能否也 ...

  7. vue前端实现将页面显示内容生成pdf文件的几种方法,html2canvas、dom-to-image、jspdf(带分页)基本使用以及介绍

    实际开发需求:vue项目中,根据数据结构生成echarts图表组件,生成带有样式的图表以后,点击下载按钮,把图表以pdf格式的文件下载到本地 实现思路:将vue界面的echarts组件生成图片,然后使 ...

  8. LeetCode-1034 边界着色

    题目来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/coloring-a-border/ 题目描述 给你一个大小为 m x n 的整数矩阵 gri ...

  9. MySQL5.7升级版本到8.0

    升级二进制包安装的MySQL In-Place Upgrade(替代升级) 替代升级涉及到shutdown down旧版本的MySQL,用新版本的包替代旧版本的二进制包,用存在的数据文件目录重启MyS ...

  10. 让CSS flex布局最后一行列表左对齐的N种方法

    原文链接https://mp.weixin.qq.com/s?__biz=MjM5MDA2MTI1MA==&mid=2649091838&idx=1&sn=fa4e1ed1e0 ...