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. ROS多机通讯

    1.主机: sudo /etc/hosts 写入从机的地址和用户名: 192.168.31.14    raspi2 2.从机 sudo /etc/hosts 写入主机的地址和用户名 192.168. ...

  2. NOIP2021游记总结

    \(\text{Day-1}\) 惨遭遣返······ 这真是伟大的啊!! \(\text{Day1}\) \(day\) 几好像没有意义,反正只有一天 \(\text{T1}\) 极致 \(H_2O ...

  3. JZOJ 5343. 【NOIP2017模拟9.3A组】健美猫

    题面 其中 \(1 \leq n \leq 2 \times 10^6\) 分析 考虑每次移动,发现负数对答案贡献少 \(1\),非负数多 \(1\) 每次移动都加了 \(1\) 负数变非负数关键点在 ...

  4. Hbase一:Hbase介绍及特点

    转载请注明出处: 1.Google的三篇论文 2003年,Google发布Google File System论文,(GFS)这是一个可扩展的分布 式文件系统,用于大型的.分布式的.对大量数据进行访问 ...

  5. sqlserver生成指定的序列

    WITH numbers AS ( SELECT 1 AS num UNION ALL SELECT num + 1 FROM numbers WHERE num < 30 ) SELECT C ...

  6. ASP动态网页(网站)设计教程

    ASP动态网页(网站)设计教程 文件名 大小 ASP获取时间函数大全 35KB 项目6 ASP数据表数据操作功能设计.pptx 3.34 MB 项目5 WEB数据库与数据库管理.pptx 3.34 M ...

  7. HashMap排序题

    HashMap排序题 题目 已知一个 HashMap<Integer,User>集合, User 有 name(String)和 age(int)属性. 请写一个方法实现对HashMap ...

  8. scrapy 爬取酷狗热门歌手音乐

    目录 声明 Hello,酷狗! 创建一个Scrapy项目 spider模块 分析前端界面 注意 items模块 pipeline模块 处理音频文件自定义下载路径 自定义下载图片路径 异步存入到数据库 ...

  9. go对数组对象排序

    1.根据时间对数组对象排序 package main import (     "fmt"     "time"     "github.com/ah ...

  10. DotNetCore2.1使用GitLab通过阿里云自动构建镜像上传阿里云仓库在Docker运行

    操作步骤: 1.安装GitLab并添加项目(此处省略安装过程) 2.获取GitLab的Access Tokens 3.创建空的DotNetCore2.1 Api项目 4.项目添加Docker支持,文件 ...