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 org.springframework.beans.factory.config.BeanDefinition;
020import org.springframework.util.Assert;
021
022/**
023 * An extension of {@code AnnotationBeanNameGenerator} that uses the fully qualified
024 * class name as the default bean name if an explicit bean name is not supplied via
025 * a supported type-level annotation such as {@code @Component} (see
026 * {@link AnnotationBeanNameGenerator} for details on supported annotations).
027 *
028 * <p>Favor this bean naming strategy over {@code AnnotationBeanNameGenerator} if
029 * you run into naming conflicts due to multiple autodetected components having the
030 * same non-qualified class name (i.e., classes with identical names but residing in
031 * different packages).
032 *
033 * <p>Note that an instance of this class is used by default for configuration-level
034 * import purposes; whereas, the default for component scanning purposes is a plain
035 * {@code AnnotationBeanNameGenerator}.
036 *
037 * @author Juergen Hoeller
038 * @author Sam Brannen
039 * @since 5.2.3
040 * @see org.springframework.beans.factory.support.DefaultBeanNameGenerator
041 * @see AnnotationBeanNameGenerator
042 * @see ConfigurationClassPostProcessor#IMPORT_BEAN_NAME_GENERATOR
043 */
044public class FullyQualifiedAnnotationBeanNameGenerator extends AnnotationBeanNameGenerator {
045
046        /**
047         * A convenient constant for a default {@code FullyQualifiedAnnotationBeanNameGenerator}
048         * instance, as used for configuration-level import purposes.
049         * @since 5.2.11
050         */
051        public static final FullyQualifiedAnnotationBeanNameGenerator INSTANCE =
052                        new FullyQualifiedAnnotationBeanNameGenerator();
053
054
055        @Override
056        protected String buildDefaultBeanName(BeanDefinition definition) {
057                String beanClassName = definition.getBeanClassName();
058                Assert.state(beanClassName != null, "No bean class name set");
059                return beanClassName;
060        }
061
062}