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.beans.factory.annotation; 018 019import java.lang.annotation.Documented; 020import java.lang.annotation.ElementType; 021import java.lang.annotation.Retention; 022import java.lang.annotation.RetentionPolicy; 023import java.lang.annotation.Target; 024 025/** 026 * An annotation that indicates 'lookup' methods, to be overridden by the container 027 * to redirect them back to the {@link org.springframework.beans.factory.BeanFactory} 028 * for a {@code getBean} call. This is essentially an annotation-based version of the 029 * XML {@code lookup-method} attribute, resulting in the same runtime arrangement. 030 * 031 * <p>The resolution of the target bean can either be based on the return type 032 * ({@code getBean(Class)}) or on a suggested bean name ({@code getBean(String)}), 033 * in both cases passing the method's arguments to the {@code getBean} call 034 * for applying them as target factory method arguments or constructor arguments. 035 * 036 * <p>Such lookup methods can have default (stub) implementations that will simply 037 * get replaced by the container, or they can be declared as abstract - for the 038 * container to fill them in at runtime. In both cases, the container will generate 039 * runtime subclasses of the method's containing class via CGLIB, which is why such 040 * lookup methods can only work on beans that the container instantiates through 041 * regular constructors: i.e. lookup methods cannot get replaced on beans returned 042 * from factory methods where we cannot dynamically provide a subclass for them. 043 * 044 * <p><b>Recommendations for typical Spring configuration scenarios:</b> 045 * When a concrete class may be needed in certain scenarios, consider providing stub 046 * implementations of your lookup methods. And please remember that lookup methods 047 * won't work on beans returned from {@code @Bean} methods in configuration classes; 048 * you'll have to resort to {@code @Inject Provider<TargetBean>} or the like instead. 049 * 050 * @author Juergen Hoeller 051 * @since 4.1 052 * @see org.springframework.beans.factory.BeanFactory#getBean(Class, Object...) 053 * @see org.springframework.beans.factory.BeanFactory#getBean(String, Object...) 054 */ 055@Target(ElementType.METHOD) 056@Retention(RetentionPolicy.RUNTIME) 057@Documented 058public @interface Lookup { 059 060 /** 061 * This annotation attribute may suggest a target bean name to look up. 062 * If not specified, the target bean will be resolved based on the 063 * annotated method's return type declaration. 064 */ 065 String value() default ""; 066 067}