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. 视觉十四讲:第十二讲_RGB-D稠密点云

    1.点云地图 所谓点云,就是由一组离散的点表示的地图,最基本的点包含x,y,z三维坐标,也可以带有r,g,b的彩色信息. #include <iostream> #include < ...

  2. JZOJ 4744.同余

    \(\text{Problem}\) \(\text{Solution}\) 考虑 \(60\) 分 设 \(f_{i,j,k}\) 表示前 \(i\) 个数,模 \(j\) 同余 \(k\) 的个数 ...

  3. CF1250C Trip to Saint Petersburg

    题目传送门 思路 线段树入门题. 不妨固定一个右端点 \(r\),把所有右端点小于 \(r\) 的区间都在 \(1\) 至此区间的左端点处 update 一个 \(p\),然后每次都给区间 \(1\) ...

  4. 🤗 PEFT: 在低资源硬件上对十亿规模模型进行参数高效微调

    动机 基于 Transformers 架构的大型语言模型 (LLM),如 GPT.T5 和 BERT,已经在各种自然语言处理 (NLP) 任务中取得了最先进的结果.此外,还开始涉足其他领域,例如计算机 ...

  5. linux系统下,新加硬盘并把现有的/home目录扩容。

    我的机器是centos7.3的系统.使用了一段时间后,我的/home 目录的可用空间没有了.于是我新加了一张硬盘.(如果是加了多张硬盘,就先做好raid,使用起来是一样的).我需要把新加的硬盘空间都划 ...

  6. pdf.js打开后的pdf文件

    可用pdf.js在h5打开pdf文件.注意,在本地打不开,一定要在部署环境. 方法:<a href="../../pdf/web/viewer.html?file=../../pdf/ ...

  7. js中各种函数

    //设置不可使用 $("#sydcpihao_"+sysTime).attr('disabled',true); //设置标签中的text $("#sydcdel_&qu ...

  8. Spring随意总结

    Spring框架的优点 1.使用Spring的IOC容器,将对象之间的依赖关系交给Spring,降低组件之间的耦合性,让我们更专注于应用逻辑 2.可以提供众多服务,事务管理,WS等. 3.AOP的很好 ...

  9. 找出一组数中出现次数最多的数(csp201312-1)

    问题描述:找出一组数字出现次数最多的数,如果有多个这样的数,输出其中最小的一个. 算法:sort排序,遍历数组,每遍历一个数,查出它已经出现的次数. 代码: #include<bits/stdc ...

  10. 如何使用源码编译安装Nginx服务器

    安装 PCRE : 网站:http://pcre.org/ 下载: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ ftp://ftp.c ...