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