美文网首页
spring-aop

spring-aop

作者: modou1618 | 来源:发表于2019-01-19 21:35 被阅读0次

一 类结构

类结构图.png
  • AbstractAutoProxyCreator模版方法查找aop配置bean,并创建代理,实现BeanPostProcessor接口,插入到spring 容器bean初始化流程中
  • AbstractAdvisorAutoProxyCreator缓存aop相关配置bean,并提供查找和目标bean代理匹配处理
  • AspectJAwareAdvisorAutoProxyCreator支持aop代理排序
  • AnnotationAwareAspectJAutoProxyCreator支持注解代理处理

二 aop创建流程

aop创建.png

2.1 注解的aop创建

  • AnnotationAwareAspectJAutoProxyCreator通过实现接口findCandidateAdvisors对aspectj的注解进行处理
protected List<Advisor> findCandidateAdvisors() {
    //获取xml标签定义的aop相关配置bean
    List<Advisor> advisors = super.findCandidateAdvisors();
//获取注解标记的aop相关配置bean
    advisors.addAll(this.aspectJAdvisorsBuilder.buildAspectJAdvisors());
    return advisors;
}
  • 获取包含Aspect注解或者是ajc编译器编译的类
public List<Advisor> buildAspectJAdvisors() {
    List<String> aspectNames = null;

    synchronized (this) {
        aspectNames = this.aspectBeanNames;
        if (aspectNames == null) {
            List<Advisor> advisors = new LinkedList<Advisor>();
            aspectNames = new LinkedList<String>();
            String[] beanNames =
                    BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory, Object.class, true, false);//获取所有bean
            for (String beanName : beanNames) {
                if (!isEligibleBean(beanName)) {
                    continue;
                }
                // 获取bean类型
                Class<?> beanType = this.beanFactory.getType(beanName);
                if (beanType == null) {
                    continue;
                }
//包含Aspect注解或者是ajc编译器编译的类
                if (this.advisorFactory.isAspect(beanType)) {
                    aspectNames.add(beanName);
                    AspectMetadata amd = new AspectMetadata(beanType, beanName);
                    if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) {
                        MetadataAwareAspectInstanceFactory factory =
                                new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName);
                        List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory);
                        if (this.beanFactory.isSingleton(beanName)) {
                            this.advisorsCache.put(beanName, classAdvisors);
                        }
                        else {
                            this.aspectFactoryCache.put(beanName, factory);
                        }
                        advisors.addAll(classAdvisors);
                    }
                    else {
                        // Per target or per this.
                        if (this.beanFactory.isSingleton(beanName)) {
                            throw new IllegalArgumentException("Bean with name '" + beanName +
                                    "' is a singleton, but aspect instantiation model is not singleton");
                        }
                        MetadataAwareAspectInstanceFactory factory =
                                new PrototypeAspectInstanceFactory(this.beanFactory, beanName);
                        this.aspectFactoryCache.put(beanName, factory);
                        advisors.addAll(this.advisorFactory.getAdvisors(factory));
                    }
                }
            }
            this.aspectBeanNames = aspectNames;
            return advisors;
        }
    }

    if (aspectNames.isEmpty()) {
        return Collections.emptyList();
    }
    List<Advisor> advisors = new LinkedList<Advisor>();
    for (String aspectName : aspectNames) {
        List<Advisor> cachedAdvisors = this.advisorsCache.get(aspectName);
        if (cachedAdvisors != null) {
            advisors.addAll(cachedAdvisors);
        }
        else {
            MetadataAwareAspectInstanceFactory factory = this.aspectFactoryCache.get(aspectName);
            advisors.addAll(this.advisorFactory.getAdvisors(factory));
        }
    }
    return advisors;
}

相关文章

  • 自定义注解,aop+redis,实现controller接口频率

    1,环境配置 引入aop的jar包compile 'org.springframework:spring-aop:...

  • spring-4.3.4.RELEASE集成AOP 实战

    一、依赖引入 (包括 spring-aop 以及 aspectj) 二、切面配置代码 (使用 javaConfig...

  • spring-AOP

    Aspects,切面 spring-aop 是spring-aspects的上层建筑 targetClass Me...

  • spring-aop

    aop概念aop概念aop术语AOP实现方式1、spring-aop(使用xml文件实现AOP)2、AspectJ...

  • spring-aop

    Aop-面向切面编程,在程序中主要用来解决一些系统层面上的问题,比如:日志,事务,权限等。 aop的一些基本概念:...

  • spring-aop

    1, aop的两种实现机制动态代理:利用jdk/cglib动态代理,性能弱一丢丢 jdk动态代理:所有的方法调用被...

  • spring-aop

  • Spring-AOP

    AOP的概念 面向切面的编程,切面用于描述分散在对象、类或者函数中的横向关注点,通过分离这些关注点,把解决特定领域...

  • spring-aop

    aop概念 aop术语 joinpoint连接点:类中可以被增强的方法(其实就是类中的方法) pointcut切入...

  • spring-aop

    aop 概念:面向切面编程作用:不用修改原来的类就可以添加新功能 专业术语 joinpoint 连接点:类中可以被...

网友评论

      本文标题:spring-aop

      本文链接:https://www.haomeiwen.com/subject/wsjgdqtx.html