底层动态代理

程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式

导入aop的相关模块

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>

动态感知业务类运行的状态

通知: 前置 @Before  后置 @After  环绕 @Around 异常@AfterThrowing  返回 @AfterReturning

补充环绕通知: 动态代理,手动推进目标方法运行(joinPoint.proceed() )

步骤:

编写切面类

在切面类里面  把每个方法 何时何地切入都指定好

将切面类和业务逻辑类 都添加到容器中

告诉容器 哪个是切面类@Aspect

在 xml中  <aop: aspectj-autoproxy> </aop:aspectj-autoproxy>    开启基于注解版的切面功能

注解中: 在当前的配置类加入  @EnableAspectJAutoProxy 启用基于注解的aop模式

正常代码:

@Component
public class Mathcalculator {
public int div(int i , int j) {
System.out.println("Mathcalculator--div被调用");
return i/j;
}
}

切面类:

@EnableAspectJAutoProxy
@Configuration
public class AOPConfig {
@Bean //业务逻辑类注册到容器
public Mathcalculator mathcalculator() {
return new Mathcalculator();
} @Bean // 切面类注册到容器中
public LogAspects logAspects() {
return new LogAspects();
}
}

测试:

public class test {
@Test
public void test01(){
//使用无参构造器 创建applicationContext (详情自己研究下有参构造器的方法)
AnnotationConfigApplicationContext applicationContext=
new AnnotationConfigApplicationContext(AOPConfig.class);
Mathcalculator bean = applicationContext.getBean(Mathcalculator.class);
bean.div(1, 1);
//设置需要激活的环境
applicationContext.getEnvironment().setActiveProfiles("dev","test"); //可以设置多个
//注册主配置类
applicationContext.register(Profile.class);
//启动刷新容器
applicationContext.refresh();
}
}

结果:

升级:

切面类

@Aspect
public class LogAspects {
//抽取公共的切入点表达式 本类的 如果是别的类的 写全名就OK了 @Pointcut("execution(public int com.toov5.config.Mathcalculator .*(..))" )
public void pointCut() { }
// @Before("public int com.toov5.config.Mathcalculator.div(int , int)")
@Before( "pointCut()" )
public void logStart(JoinPoint joinpoint) { //获取签名 获取方法
Object[] args = joinpoint.getArgs(); //目标方法 运行时候需要的参数表
System.out.println("除法运行@Before"+joinpoint.getSignature().getName()+"运行时参数表"+args );
}
@After( "pointCut()" ) //运行异常与否 都要执行的
public void logEnd(JoinPoint joinPoint) {
System.out.println(joinPoint.getSignature().getName()+"除法结束@After");
}
@AfterReturning(value="pointCut()", returning="result")
public void logReturn(JoinPoint joinpoint,Object result ) { //result 接受返回值
System.out.println(joinpoint.getSignature().getName()+"除法正常运行@AfterReturning,结果:"+result);
}
@AfterThrowing(value = "pointCut()", throwing="e")
public void logException(JoinPoint joinpoint, Exception e) {
System.out.println( joinpoint.getSignature().getName()+"除法异常@AfterThrowing"+e.getMessage());
}
}

配置:

@EnableAspectJAutoProxy
@Configuration
public class AOPConfig {
@Bean //业务逻辑类注册到容器
public Mathcalculator mathcalculator() {
return new Mathcalculator();
} @Bean // 切面类注册到容器中
public LogAspects logAspects() {
return new LogAspects();
}
}

测试类:

public class test {
@Test
public void test01(){
//使用无参构造器 创建applicationContext (详情自己研究下有参构造器的方法)
AnnotationConfigApplicationContext applicationContext=
new AnnotationConfigApplicationContext(AOPConfig.class);
Mathcalculator bean = applicationContext.getBean(Mathcalculator.class);
bean.div(1, 1);
//设置需要激活的环境
applicationContext.getEnvironment().setActiveProfiles("dev","test"); //可以设置多个
//注册主配置类
applicationContext.register(Profile.class);
//启动刷新容器
applicationContext.refresh();
}
}

结果:

JointPoint 要写一定要写在 参数的首位!

三步走:

1  将业务逻辑组件和切面类都加入到容器,告诉Spring哪个是切面类(@Aspect)

2 在切面类上的每一个通知方法上标注 通知注解,告诉Spring合适何地运行(切入点表达式)

3 开启基于注解的aop模式 与config上

总结:

aop的原理


加入@EnableAspectJAutoProxy  才有AOP

继续点开看

利用AspectJAutoProxyRegistrar  给自定义容器中注册bean

给容器注册一个AnnotationAwareAspectJAutoProxyCreator   自动代理创建器

可以一直追踪其父类:

实现了 SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware

后置处理器

关注后置处理器的工作  Bean初始化完成前后做的事情

BeanFactory                自动注入

AbstractAutoProxyCreator :

@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}

然后是与后置处理器有关的:

@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName)

有后置处理器的逻辑

AbstractAutoProxyCreator   类

AnnotationAwareAspectJAutoProxyCreator 类  initBeanFactory

启动后

1、  传入主配置类  创建 IOC容器

2、  注册配置类 调用refresh() 刷新容器

3、 registerBeanPostProcessors(beanFactory)  注册bean的后置处理器 方便拦截bean的创建

1先获取ioc容器中已经定义了需要创建对象的所有BeanPostProcessor

2给容器加别的BeanPostProcessor

3优先注册实现了PriorityOrdered接口的BeanPostProcessor

4在给容器中注册实现了Ordered接口的BeanPostProcessor

5注册没实现优先级接口的BeanPostProcessor

创建internalAutoProxyCreator的BeanPostProcessor

创建Bean的实例

populateBean 初始化Bean

1、 invokeAwareMethods() 处理Aware接口的方法回调

2、 applyBeanPostProcessorsBeforeInitialization() 应有后置处理器的BeforeInitialization( )

3、invokeInitMethods() 自定义的初始化方法

4、applyBeanPostProcessorsAfterInitialization ()  执行后置处理器的 postProcessAfterInitialization

4、BeanPostProcessor(AnnotationAwareAspectJAutoProxyCreator)创建成功; aspectJAdvisorBuilder

6 把beanPostProcessor注册到BeanFactory中 调用addBeanPostProcessor(postProcessor)

Spring注解(AOP)的更多相关文章

  1. Spring注解 - AOP 面向切面编程

    基本概念: AOP:Aspect Oriented Programming,即面向切面编程 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式 前置通知(@Before):在目标 ...

  2. 基于spring注解AOP的异常处理

    一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...

  3. Spring注解AOP及单元测试junit(6)

    2019-03-10/20:19:56 演示:将xml配置方式改为注解方式 静态以及动态代理推荐博客:https://blog.csdn.net/javazejian/article/details/ ...

  4. spring 注解AOP

     aspectAnnotation的切面信息,加到了AnnotationAwareAspectJAutoProxyCreator的advisorsCache属性里面去了. 解析annotationSe ...

  5. 重新学习Spring注解——AOP

    面向切面编程——思想:在一个地方定义通用功能,但是可以通过声明的方式定义这个功能要以何种方式在何处运用,而无须修改受影响的类. 切面:横切关注点可以被模块化为特殊的类. 优点: 1.每个关注点都集中在 ...

  6. spring注解 aop

    @Resource(name="as")  为空按类型装配 @autowired 按类型 @quafiler (name="xx") 按名称 spring继承关 ...

  7. spring 注解aop调用invoke()

    public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlAp ...

  8. 一步一步深入spring(5)--使用基于注解的spring实现 AOP

    1.要利用spring aop,至少需要添加以下jar包 使用spring需要的jarspring.jar .commons-logging.jar 使用切面编程(AOP)需要的jar aspectj ...

  9. Java框架spring 学习笔记(十四):注解aop操作

    回见Java框架spring Boot学习笔记(十三):aop实例操作,这里介绍注解aop操作 首先编写一个切入点HelloWorld.java package com.example.spring; ...

  10. Spring的AOP基于AspectJ的注解方式开发3

    上上偏博客介绍了@Aspect,@Before 上篇博客介绍了spring的AOP开发的注解通知类型:@Before,@AfterThrowing,@After,@AfterReturning,@Ar ...

随机推荐

  1. python之list去重

    问题就是对一个list中的新闻id进行去重,去重之后要保证顺序不变. 直观方法 最简单的思路就是: 复制代码代码如下: ids = [1,2,3,3,4,2,3,4,5,6,1]news_ids = ...

  2. VS 忽略文件 Git 向远程添加问题

    背景介绍: 我团队有个叫李恕华的技术大牛,最近在研究redis 然后向一个叫做MTS的项目中引入了redis,感觉很吊 问题: vs2017+git管理代码 其他人分支里一直提示找不到redis文件 ...

  3. 前端代码tomcat下简单部署

    软件 filezilla [ftp]  +  visionapp Remote Desktop[远程桌面] (前提:前后端代码分离,如前端angular实现) ftp上传到机器{软件 filezill ...

  4. [Navicat] 常用快捷键及注意事项

    近来需要同时管理多种类型.多台server的数据库,在经历了各种查询客户端(SSMS, SQLyog, PL/SQL等)的不断切换,ip/user/pwd的反复输入的这种自虐式体验后,实在无法忍受,就 ...

  5. 【谷歌浏览器】在任意页面运行JS

    1.使用谷歌浏览器的调试功能: 在任何页面上运行代码片段 · Chrome 开发者工具中文文档 注:比较简单,直接,不过只能本地执行,只能自己使用.且需自行保存JS文件: 2.使用油猴插件: Tamp ...

  6. FineReport----单元格元素(数据列、公式、斜线)

    一.绑定.插入数据列 数据集字段绑定 二.公式 1.单元 格计算 1.两个单元格(C7,I7)和 2.等于某单元格的值 D7等C7单元格的值 3.C7/8(除) 4.除数被除数为零.空的处理 http ...

  7. 《挑战程序设计竞赛》2.3 动态规划-优化递推 POJ1742 3046 3181

    POJ1742 http://poj.org/problem?id=1742 题意 有n种面额的硬币,面额个数分别为Ai.Ci,求最多能搭配出几种不超过m的金额? 思路 据说这是传说中的男人8题呢,对 ...

  8. Chomsky_hierarchy

      Grammar Languages Automaton Production rules (constraints) Type-0 Recursively enumerable Turing ma ...

  9. dev EditMask 设置方法

    官方帮助地址: https://documentation.devexpress.com/WindowsForms/583/Controls-and-Libraries/Editors-and-Sim ...

  10. python基础之类的特性(property)

    一 什么是特性propertyproperty是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值. import math class Circle: def __init__(self,ra ...