
infra.beans.factory.annotation.Lookup Maven / Gradle / Ivy
/*
* Copyright 2017 - 2024 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see [https://www.gnu.org/licenses/]
*/
package infra.beans.factory.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import infra.beans.factory.BeanFactory;
/**
* An annotation that indicates 'lookup' methods, to be overridden by the container
* to redirect them back to the {@link BeanFactory}
* for a {@code getBean} call. This is essentially an annotation-based version of the
* XML {@code lookup-method} attribute, resulting in the same runtime arrangement.
*
* The resolution of the target bean can either be based on the return type
* ({@code getBean(Class)}) or on a suggested bean name ({@code getBean(String)}),
* in both cases passing the method's arguments to the {@code getBean} call
* for applying them as target factory method arguments or constructor arguments.
*
*
Such lookup methods can have default (stub) implementations that will simply
* get replaced by the container, or they can be declared as abstract - for the
* container to fill them in at runtime. In both cases, the container will generate
* runtime subclasses of the method's containing class via CGLIB, which is why such
* lookup methods can only work on beans that the container instantiates through
* regular constructors: i.e. lookup methods cannot get replaced on beans returned
* from factory methods where we cannot dynamically provide a subclass for them.
*
*
Recommendations for typical Framework configuration scenarios:
* When a concrete class may be needed in certain scenarios, consider providing stub
* implementations of your lookup methods. And please remember that lookup methods
* won't work on beans returned from {@code @Bean} methods in configuration classes;
* you'll have to resort to {@code @Inject Provider} or the like instead.
*
* @author Juergen Hoeller
* @author Harry Yang
* @see BeanFactory#getBean(Class, Object...)
* @see BeanFactory#getBean(String, Object...)
* @since 4.0 2022/3/8 13:44
*/
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Lookup {
/**
* This annotation attribute may suggest a target bean name to look up.
* If not specified, the target bean will be resolved based on the
* annotated method's return type declaration.
*/
String value() default "";
}