001/*
002 * Copyright 2002-2020 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.context.annotation;
018
019import java.util.function.Predicate;
020
021import org.springframework.core.type.AnnotationMetadata;
022import org.springframework.lang.Nullable;
023
024/**
025 * Interface to be implemented by types that determine which @{@link Configuration}
026 * class(es) should be imported based on a given selection criteria, usually one or
027 * more annotation attributes.
028 *
029 * <p>An {@link ImportSelector} may implement any of the following
030 * {@link org.springframework.beans.factory.Aware Aware} interfaces,
031 * and their respective methods will be called prior to {@link #selectImports}:
032 * <ul>
033 * <li>{@link org.springframework.context.EnvironmentAware EnvironmentAware}</li>
034 * <li>{@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware}</li>
035 * <li>{@link org.springframework.beans.factory.BeanClassLoaderAware BeanClassLoaderAware}</li>
036 * <li>{@link org.springframework.context.ResourceLoaderAware ResourceLoaderAware}</li>
037 * </ul>
038 *
039 * <p>Alternatively, the class may provide a single constructor with one or more of
040 * the following supported parameter types:
041 * <ul>
042 * <li>{@link org.springframework.core.env.Environment Environment}</li>
043 * <li>{@link org.springframework.beans.factory.BeanFactory BeanFactory}</li>
044 * <li>{@link java.lang.ClassLoader ClassLoader}</li>
045 * <li>{@link org.springframework.core.io.ResourceLoader ResourceLoader}</li>
046 * </ul>
047 *
048 * <p>{@code ImportSelector} implementations are usually processed in the same way
049 * as regular {@code @Import} annotations, however, it is also possible to defer
050 * selection of imports until all {@code @Configuration} classes have been processed
051 * (see {@link DeferredImportSelector} for details).
052 *
053 * @author Chris Beams
054 * @author Juergen Hoeller
055 * @since 3.1
056 * @see DeferredImportSelector
057 * @see Import
058 * @see ImportBeanDefinitionRegistrar
059 * @see Configuration
060 */
061public interface ImportSelector {
062
063        /**
064         * Select and return the names of which class(es) should be imported based on
065         * the {@link AnnotationMetadata} of the importing @{@link Configuration} class.
066         * @return the class names, or an empty array if none
067         */
068        String[] selectImports(AnnotationMetadata importingClassMetadata);
069
070        /**
071         * Return a predicate for excluding classes from the import candidates, to be
072         * transitively applied to all classes found through this selector's imports.
073         * <p>If this predicate returns {@code true} for a given fully-qualified
074         * class name, said class will not be considered as an imported configuration
075         * class, bypassing class file loading as well as metadata introspection.
076         * @return the filter predicate for fully-qualified candidate class names
077         * of transitively imported configuration classes, or {@code null} if none
078         * @since 5.2.4
079         */
080        @Nullable
081        default Predicate<String> getExclusionFilter() {
082                return null;
083        }
084
085}