001/*
002 * Copyright 2002-2018 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.beans.factory.annotation;
018
019import org.springframework.beans.factory.wiring.BeanWiringInfo;
020import org.springframework.beans.factory.wiring.BeanWiringInfoResolver;
021import org.springframework.lang.Nullable;
022import org.springframework.util.Assert;
023import org.springframework.util.ClassUtils;
024
025/**
026 * {@link org.springframework.beans.factory.wiring.BeanWiringInfoResolver} that
027 * uses the Configurable annotation to identify which classes need autowiring.
028 * The bean name to look up will be taken from the {@link Configurable} annotation
029 * if specified; otherwise the default will be the fully-qualified name of the
030 * class being configured.
031 *
032 * @author Rod Johnson
033 * @author Juergen Hoeller
034 * @since 2.0
035 * @see Configurable
036 * @see org.springframework.beans.factory.wiring.ClassNameBeanWiringInfoResolver
037 */
038public class AnnotationBeanWiringInfoResolver implements BeanWiringInfoResolver {
039
040        @Override
041        @Nullable
042        public BeanWiringInfo resolveWiringInfo(Object beanInstance) {
043                Assert.notNull(beanInstance, "Bean instance must not be null");
044                Configurable annotation = beanInstance.getClass().getAnnotation(Configurable.class);
045                return (annotation != null ? buildWiringInfo(beanInstance, annotation) : null);
046        }
047
048        /**
049         * Build the {@link BeanWiringInfo} for the given {@link Configurable} annotation.
050         * @param beanInstance the bean instance
051         * @param annotation the Configurable annotation found on the bean class
052         * @return the resolved BeanWiringInfo
053         */
054        protected BeanWiringInfo buildWiringInfo(Object beanInstance, Configurable annotation) {
055                if (!Autowire.NO.equals(annotation.autowire())) {
056                        // Autowiring by name or by type
057                        return new BeanWiringInfo(annotation.autowire().value(), annotation.dependencyCheck());
058                }
059                else if (!annotation.value().isEmpty()) {
060                        // Explicitly specified bean name for bean definition to take property values from
061                        return new BeanWiringInfo(annotation.value(), false);
062                }
063                else {
064                        // Default bean name for bean definition to take property values from
065                        return new BeanWiringInfo(getDefaultBeanName(beanInstance), true);
066                }
067        }
068
069        /**
070         * Determine the default bean name for the specified bean instance.
071         * <p>The default implementation returns the superclass name for a CGLIB
072         * proxy and the name of the plain bean class else.
073         * @param beanInstance the bean instance to build a default name for
074         * @return the default bean name to use
075         * @see org.springframework.util.ClassUtils#getUserClass(Class)
076         */
077        protected String getDefaultBeanName(Object beanInstance) {
078                return ClassUtils.getUserClass(beanInstance).getName();
079        }
080
081}