当前位置: 移动技术网 > IT编程>开发语言>Java > Springboot源码分析之TypeFilter魔力

Springboot源码分析之TypeFilter魔力

2019年09月13日  | 移动技术网IT编程  | 我要评论

摘要:

在平常的开发中,不知道大家有没有想过这样一个问题,为什么我们自定义注解的时候要使用spring的原生注解(这里指的是类似@component@service........),要么就是 随便弄个注解,搭配自己的切面编程来实现某些业务逻辑。这篇文章主要给大家分享一下,如何脱离spring原生注解自定义注解注入ioc

springbootapplication注解分析

file
从源代码很容易看出来,它的作用就是自动装配和扫描我们的包,并将符合的类进行注册到容器。自动装配非常简单,这里不做过多分析,接下来分析一下什么叫做符合规则的类。在@componentscan注解上面的过滤器类型的定义

public enum filtertype {
    annotation, //注解类型
    assignable_type, //指定的类型
    aspectj, //按照aspectj的表达式,基本上不会用到
    regex, //按照正则表达式
    custom; //自定义

    private filtertype() {
    }
}

excludefilters排除过滤器

这个是给我们排除符合的类,不让他注册到ioc的时候使用的, springboot默认使用两个排除过滤器,很简单的,网上随便搜都可以找到相关说明,在这儿我举个特舒列子就行了.

package com.github.dqqzj.springboot.filter;

import java.lang.annotation.elementtype;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;

/**
 * @author qinzhongjian
 * @date created in 2019-07-30 19:14
 * @description: todo
 * @since jdk 1.8.0_212-b10
 */
@target(elementtype.type)
@retention(retentionpolicy.runtime)
public @interface dqqzj {
    string value();
}
package com.github.dqqzj.springboot.filter;

import org.springframework.stereotype.component;

/**
 * @author qinzhongjian
 * @date created in 2019-07-29 22:30
 * @description: todo
 * @since jdk 1.8.0_212-b10
 */
@dqqzj(value = "dqqzj")
@component
public class tt {
}
package com.github.dqqzj.springboot.filter;

import org.springframework.core.type.classreading.metadatareader;
import org.springframework.core.type.classreading.metadatareaderfactory;
import org.springframework.core.type.filter.typefilter;

import java.io.ioexception;

/**
 * @author qinzhongjian
 * @date created in 2019-07-30 19:13
 * @description: todo
 * @since jdk 1.8.0_212-b10
 */
public class mytypefilter implements typefilter {
    @override
    public boolean match(metadatareader metadatareader, metadatareaderfactory metadatareaderfactory) throws ioexception {
        if (metadatareader.getannotationmetadata().isannotated(dqqzj.class.getname())) {
            return true;
        }
        return false;
    }
}

file
以上代码是正常逻辑,反过来这样想,如果将tt类的@component注解去掉是不是也行的,所以这种排除注解一般都用在正常可以注入到容器的时候进行添加的,那么我们上面说过,脱离spring也可以注入到容器,该怎么实现呢?

includefilters包含过滤器

脱离spring原生注解,将将tt类的@component注解去掉

package com.github.dqqzj.springboot.filter;

import org.springframework.stereotype.component;

/**
 * @author qinzhongjian
 * @date created in 2019-07-29 22:30
 * @description: todo
 * @since jdk 1.8.0_212-b10
 */
@dqqzj(value = "dqqzj")
//@component
public class tt {
}

file

透过现象看本质

流程进行梳理一下,注解驱动在注入容器的关键扫描类(注意这里是指的扫描,而不是什么@bean,@import等其余注解都是建立在这个基础之上的)

  • componentscanannotationparser
  • classpathbeandefinitionscanner
  • classpathscanningcandidatecomponentprovider
classpathscanningcandidatecomponentprovider#registerdefaultfilters
protected void registerdefaultfilters() {
        this.includefilters.add(new annotationtypefilter(component.class));
        classloader cl = classpathscanningcandidatecomponentprovider.class.getclassloader();

        try {
            this.includefilters.add(new annotationtypefilter(classutils.forname("javax.annotation.managedbean", cl), false));
            this.logger.trace("jsr-250 'javax.annotation.managedbean' found and supported for component scanning");
        } catch (classnotfoundexception var4) {
        }

        try {
            this.includefilters.add(new annotationtypefilter(classutils.forname("javax.inject.named", cl), false));
            this.logger.trace("jsr-330 'javax.inject.named' annotation found and supported for component scanning");
        } catch (classnotfoundexception var3) {
        }

    }

此处会将@component,jsr-250 'javax.annotation.managedbean',jsr-330 'javax.inject.named'的注解进行注册,所以难怪我们的自定义注解必须要有这些派生注解,换一个角度来思考,它们这个地方进行类annotationtypefilter的添加,我们也可以自定义annotationtypefilter来将自己的定义规则的注解进行注入容器。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网