首页 > 其他分享 >spring-ImportSelector接口原理

spring-ImportSelector接口原理

时间:2023-02-16 19:34:15浏览次数:34  
标签:return spring AnnotationMetadata springframework ImportSelector link 接口 org clas

ImportSelector接口源码

package org.springframework.context.annotation;

import org.springframework.core.type.AnnotationMetadata;

/**
 * Interface to be implemented by types that determine which @{@link Configuration}
 * class(es) should be imported based on a given selection criteria, usually one or
 * more annotation attributes.
 *
 * <p>An {@link ImportSelector} may implement any of the following
 * {@link org.springframework.beans.factory.Aware Aware} interfaces,
 * and their respective methods will be called prior to {@link #selectImports}:
 * <ul>
 * <li>{@link org.springframework.context.EnvironmentAware EnvironmentAware}</li>
 * <li>{@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware}</li>
 * <li>{@link org.springframework.beans.factory.BeanClassLoaderAware BeanClassLoaderAware}</li>
 * <li>{@link org.springframework.context.ResourceLoaderAware ResourceLoaderAware}</li>
 * </ul>
 *
 * <p>{@code ImportSelector} implementations are usually processed in the same way
 * as regular {@code @Import} annotations, however, it is also possible to defer
 * selection of imports until all {@code @Configuration} classes have been processed
 * (see {@link DeferredImportSelector} for details).
 *
 * @author Chris Beams
 * @since 3.1
 * @see DeferredImportSelector
 * @see Import
 * @see ImportBeanDefinitionRegistrar
 * @see Configuration
 */
public interface ImportSelector {

    /**
     * Select and return the names of which class(es) should be imported based on
     * the {@link AnnotationMetadata} of the importing @{@link Configuration} class.
     */
    String[] selectImports(AnnotationMetadata importingClassMetadata);

}

主要作用:

收集需要导入的配置类。如果配置类也实现了Aware接口,先执行Aware的方法

 

DeferredImportSelector接口源码解析

/*
 * Copyright 2002-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.context.annotation;

import java.util.Objects;

import org.springframework.core.type.AnnotationMetadata;
import org.springframework.lang.Nullable;

/**
 * A variation of {@link ImportSelector} that runs after all {@code @Configuration} beans
 * have been processed. This type of selector can be particularly useful when the selected
 * imports are {@code @Conditional}.
 *
 * <p>Implementations can also extend the {@link org.springframework.core.Ordered}
 * interface or use the {@link org.springframework.core.annotation.Order} annotation to
 * indicate a precedence against other {@link DeferredImportSelector}s.
 *
 * <p>Implementations may also provide an {@link #getImportGroup() import group} which
 * can provide additional sorting and filtering logic across different selectors.
 *
 * @author Phillip Webb
 * @author Stephane Nicoll
 * @since 4.0
 */
public interface DeferredImportSelector extends ImportSelector {

    /**
     * Return a specific import group.
     * <p>The default implementations return {@code null} for no grouping required.
     * @return the import group class, or {@code null} if none
     * @since 5.0
     */
    @Nullable
    default Class<? extends Group> getImportGroup() {
        return null;
    }


    /**
     * Interface used to group results from different import selectors.
     */
    interface Group {

        /**
         * Process the {@link AnnotationMetadata} of the importing @{@link Configuration}
         * class using the specified {@link DeferredImportSelector}.
         */
        void process(AnnotationMetadata metadata, DeferredImportSelector selector);

        /**
         * Return the {@link Entry entries} of which class(es) should be imported
         * for this group.
         */
        Iterable<Entry> selectImports();


        /**
         * An entry that holds the {@link AnnotationMetadata} of the importing
         * {@link Configuration} class and the class name to import.
         */
        class Entry {

            private final AnnotationMetadata metadata;

            private final String importClassName;

            public Entry(AnnotationMetadata metadata, String importClassName) {
                this.metadata = metadata;
                this.importClassName = importClassName;
            }

            /**
             * Return the {@link AnnotationMetadata} of the importing
             * {@link Configuration} class.
             */
            public AnnotationMetadata getMetadata() {
                return this.metadata;
            }

            /**
             * Return the fully qualified name of the class to import.
             */
            public String getImportClassName() {
                return this.importClassName;
            }

            @Override
            public boolean equals(Object other) {
                if (this == other) {
                    return true;
                }
                if (other == null || getClass() != other.getClass()) {
                    return false;
                }
                Entry entry = (Entry) other;
                return (Objects.equals(this.metadata, entry.metadata) &&
                        Objects.equals(this.importClassName, entry.importClassName));
            }

            @Override
            public int hashCode() {
                return Objects.hash(this.metadata, this.importClassName);
            }
        }
    }

}

 

标签:return,spring,AnnotationMetadata,springframework,ImportSelector,link,接口,org,clas
From: https://www.cnblogs.com/hongyedeboke/p/17128020.html

相关文章

  • Springboot项目中注入bean失败的问题排查
    Springboot项目中注入bean失败的问题排查这是一个Spring常见的问题,下面我们从测试方法和普通方法出问题两个角度来下如何解决测试方法先查看目录是否有误测试类的包名......
  • Spring的配置、依赖注入、Bean的自动装配及注解开发
    Spring配置1.别名alias设置别名,为bean设置别名,可以设置多个别名<!--设置别名:在获取Bean的时候可以使用别名获取--><aliasname="userT"alias="userNew"/>......
  • springboot基础配置yml
    yaml语法规则大小写敏感属性层级关系使用多行描述,每行结尾使用冒号结束使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)属性值前面添加空格(属性名与属......
  • springboot自定义校验工具类
    参考:https://betheme.net/news/txtlist_i120686v.html?action=onClickhttps://www.ngui.cc/el/2571188.html?action=onClick一、原生注解在springboot中,我们可以使用ja......
  • 申报发布的项目是用SpringSecurity-OAuth2模式登录认证的
    SpringSecurity-OAuth2万文详解https://blog.csdn.net/weixin_68320784/article/details/124103484清晰搞懂SpringSecurity的登录认证https://blog.csdn.net/Aqting/ar......
  • 如何利用iphone自带的弱网络进行弱网或接口timeout测试
    前提条件:1.设置中调出:开发者选项。(方法一:找开发帮忙真机链接编译器xcode,重启手机后开发者选项消失.开发者选项在设置的一级子目录下;方法二:在设置隐私与安全性中,打开开发......
  • Hystrix + OpenFeign+ SpringCloud +Nacos
      注意:2023年2月 springcloud最新版本不支持nacos2.2和hystrix,测试发现以下这个版本还支持<groupId>org.springframework.cloud</groupI......
  • SpringBoot
    一.SpringBoot 1.1SpringBoot概述1.1.1什么是SpringBootSpringBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework同属于spring的产品:其最主......
  • Spring-设计模式
    1.1开闭原则开闭原则(open-closedprinciple,OCP)是指一个软件实体(如类,模块和函数)应该对扩展开放,对修改关闭。所谓的开闭,也正是对扩展和修改两个行为的一个原则。强调用抽......
  • springboot 使用微信支付
    参考:Java实现微信支付_被编程征服的秃发女子的博客-CSDN博客一.微信支付流程支付流程:用户点击支付按钮调用接口[/deposit]=>返回给小程序payInfo和订单编号orderNum......