001/*
002 * Copyright 2002-2019 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 org.springframework.beans.factory.support.BeanDefinitionRegistry;
020import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
021import org.springframework.beans.factory.support.BeanNameGenerator;
022import org.springframework.core.type.AnnotationMetadata;
023
024/**
025 * Interface to be implemented by types that register additional bean definitions when
026 * processing @{@link Configuration} classes. Useful when operating at the bean definition
027 * level (as opposed to {@code @Bean} method/instance level) is desired or necessary.
028 *
029 * <p>Along with {@code @Configuration} and {@link ImportSelector}, classes of this type
030 * may be provided to the @{@link Import} annotation (or may also be returned from an
031 * {@code ImportSelector}).
032 *
033 * <p>An {@link ImportBeanDefinitionRegistrar} may implement any of the following
034 * {@link org.springframework.beans.factory.Aware Aware} interfaces, and their respective
035 * methods will be called prior to {@link #registerBeanDefinitions}:
036 * <ul>
037 * <li>{@link org.springframework.context.EnvironmentAware EnvironmentAware}</li>
038 * <li>{@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware}
039 * <li>{@link org.springframework.beans.factory.BeanClassLoaderAware BeanClassLoaderAware}
040 * <li>{@link org.springframework.context.ResourceLoaderAware ResourceLoaderAware}
041 * </ul>
042 *
043 * <p>Alternatively, the class may provide a single constructor with one or more of
044 * the following supported parameter types:
045 * <ul>
046 * <li>{@link org.springframework.core.env.Environment Environment}</li>
047 * <li>{@link org.springframework.beans.factory.BeanFactory BeanFactory}</li>
048 * <li>{@link java.lang.ClassLoader ClassLoader}</li>
049 * <li>{@link org.springframework.core.io.ResourceLoader ResourceLoader}</li>
050 * </ul>
051 *
052 * <p>See implementations and associated unit tests for usage examples.
053 *
054 * @author Chris Beams
055 * @author Juergen Hoeller
056 * @since 3.1
057 * @see Import
058 * @see ImportSelector
059 * @see Configuration
060 */
061public interface ImportBeanDefinitionRegistrar {
062
063        /**
064         * Register bean definitions as necessary based on the given annotation metadata of
065         * the importing {@code @Configuration} class.
066         * <p>Note that {@link BeanDefinitionRegistryPostProcessor} types may <em>not</em> be
067         * registered here, due to lifecycle constraints related to {@code @Configuration}
068         * class processing.
069         * <p>The default implementation delegates to
070         * {@link #registerBeanDefinitions(AnnotationMetadata, BeanDefinitionRegistry)}.
071         * @param importingClassMetadata annotation metadata of the importing class
072         * @param registry current bean definition registry
073         * @param importBeanNameGenerator the bean name generator strategy for imported beans:
074         * {@link ConfigurationClassPostProcessor#IMPORT_BEAN_NAME_GENERATOR} by default, or a
075         * user-provided one if {@link ConfigurationClassPostProcessor#setBeanNameGenerator}
076         * has been set. In the latter case, the passed-in strategy will be the same used for
077         * component scanning in the containing application context (otherwise, the default
078         * component-scan naming strategy is {@link AnnotationBeanNameGenerator#INSTANCE}).
079         * @since 5.2
080         * @see ConfigurationClassPostProcessor#IMPORT_BEAN_NAME_GENERATOR
081         * @see ConfigurationClassPostProcessor#setBeanNameGenerator
082         */
083        default void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry,
084                        BeanNameGenerator importBeanNameGenerator) {
085
086                registerBeanDefinitions(importingClassMetadata, registry);
087        }
088
089        /**
090         * Register bean definitions as necessary based on the given annotation metadata of
091         * the importing {@code @Configuration} class.
092         * <p>Note that {@link BeanDefinitionRegistryPostProcessor} types may <em>not</em> be
093         * registered here, due to lifecycle constraints related to {@code @Configuration}
094         * class processing.
095         * <p>The default implementation is empty.
096         * @param importingClassMetadata annotation metadata of the importing class
097         * @param registry current bean definition registry
098         */
099        default void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
100        }
101
102}