有多少人用過(guò)Spring的@Lookup注解?
環(huán)境:Spring5.3.23
1. 簡(jiǎn)介
Lookup方法注入能夠根據(jù)@Lookup注解的value屬性值或被注解該方法的返回值,從容器中查找bean作為方法的返回值對(duì)象使用。Spring容器會(huì)通過(guò)CGLIB生成當(dāng)前類的代理,然后重寫(xiě)被@Lookup注解的方法。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lookup {
/**
* 根據(jù)你設(shè)置的value值,從容器中查找對(duì)應(yīng)的bean,如果沒(méi)有指定value值
* 那么會(huì)根據(jù)被注解方法的返回值類型從容器中查找相應(yīng)的bean
*/
String value() default "";
}
2. 應(yīng)用案例
準(zhǔn)備基礎(chǔ)類
static interface HttpSecurity {
void http() ;
}
static class HtmlHttpSecurity implements HttpSecurity {
@Override
public void http() {
System.out.println("Html HttpSecurity...") ;
}
}
定義一個(gè)抽象類
該類中有一個(gè)抽象方法被@Lookup注解標(biāo)注
static abstract class SecurityManager {
public void execute() {
HttpSecurity httpSecurity = httpSecurity() ;
System.out.println(httpSecurity.getClass()) ;
httpSecurity.http() ;
}
@Lookup("html")
protected abstract HttpSecurity httpSecurity() ;
}
注冊(cè)Bean
將上面的類注冊(cè)到容器中
public static void main(String[] args) {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
context.registerBean("html", HtmlHttpSecurity.class) ;
context.register(SecurityManager.class) ;
context.refresh() ;
SecurityManager sm = context.getBean(SecurityManager.class);
sm.execute() ;
System.out.println(sm.getClass()) ;
}
}
執(zhí)行結(jié)果
class com.pack.main.lookup.MethodLookupInjectMain2$HtmlHttpSecurity
Html HttpSecurity...
class com.pack.main.lookup.MethodLookupInjectMain2$SecurityManager$$EnhancerBySpringCGLIB$$ae697832
SecurityManager通過(guò)CGLIB被創(chuàng)建為了代理類。同時(shí)execute方法中HttpSecurity對(duì)象就是HtmlHttpSecurity類,也就是容器通過(guò)查找注入的。
去掉@Lookup注解的value屬性
@Lookup
protected abstract HttpSecurity httpSecurity() ;
繼續(xù)執(zhí)行上面的測(cè)試代碼,程序也能正常的輸出。
在添加一個(gè)HttpSecurity的實(shí)現(xiàn)
static class CssHttpSecurity implements HttpSecurity {
@Override
public void http() {
System.out.println("Css HttpSecurity...") ;
}
}
將上面的類也注冊(cè)到容器中,如果這時(shí)候你的@Lookup注解沒(méi)有value屬性將會(huì)報(bào)錯(cuò)
Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.pack.main.lookup.MethodLookupInjectMain2$HttpSecurity' available: expected single matching bean but found 2: html,css
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1273)
expected single matching bean but found 2: html,css;錯(cuò)誤提示你有2個(gè)bean,期望的是一個(gè),所以這里我們必須添加value屬性,指明要查找的bean對(duì)象。
繼續(xù)測(cè)試,被@Lookup注解的方法是不是必須是抽象的?
修改代碼如下:
@Lookup("html")
protected HttpSecurity httpSecurity() {
return null ;
};
方法返回null,執(zhí)行結(jié)果
class com.pack.main.lookup.MethodLookupInjectMain2$HtmlHttpSecurity
Html HttpSecurity...
class com.pack.main.lookup.MethodLookupInjectMain2$SecurityManager$$EnhancerBySpringCGLIB$$ae697832
程序正常,沒(méi)有問(wèn)題。這也說(shuō)明了,這里和具體的返回值是沒(méi)有關(guān)系的。
3. 實(shí)現(xiàn)原理
容器在創(chuàng)建一個(gè)Bean對(duì)象時(shí)會(huì)執(zhí)行如下步驟
public abstract class AbstractAutowireCapableBeanFactory {
protected Object doCreateBean() {
BeanWrapper instanceWrapper = null;
// ...
if (instanceWrapper == null) {
// 創(chuàng)建實(shí)例
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
// ...
}
protected BeanWrapper createBeanInstance() {
// 查找構(gòu)造方法
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
// 實(shí)例化bean對(duì)象;上面一步對(duì)@Lookup注解的方法進(jìn)行了初始化查找
return instantiateBean(beanName, mbd);
}
protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(@Nullable Class<?> beanClass, String beanName)
throws BeansException {
if (beanClass != null && hasInstantiationAwareBeanPostProcessors()) {
// 調(diào)用BeanPostProcessor
for (SmartInstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().smartInstantiationAware) {
// 這里通過(guò)了AutowiredAnnotationBeanPostProcessor處理器
// 這里其實(shí)最終返回了null,關(guān)鍵就是執(zhí)行了下面的動(dòng)作,查找了@Lookup注解的方法進(jìn)行解析
// 保存到BeanDefinition中
Constructor<?>[] ctors = bp.determineCandidateConstructors(beanClass, beanName);
if (ctors != null) {
return ctors;
}
}
}
return null;
}
}
AutowiredAnnotationBeanPostProcessor處理器
public class AutowiredAnnotationBeanPostProcessor {
public Constructor<?>[] determineCandidateConstructors() {
do {
// 遍歷當(dāng)前類的所有方法
ReflectionUtils.doWithLocalMethods(targetClass, method -> {
// 獲取方法是的@Lookup注解
Lookup lookup = method.getAnnotation(Lookup.class);
if (lookup != null) {
// 如果存在則構(gòu)造LookupOverride對(duì)象,將當(dāng)前的Method及注解的value值傳入
LookupOverride override = new LookupOverride(method, lookup.value());
try {
RootBeanDefinition mbd = (RootBeanDefinition)this.beanFactory.getMergedBeanDefinition(beanName);
// 最后將其保存到BeanDefinition中
mbd.getMethodOverrides().addOverride(override);
}
}
});
targetClass = targetClass.getSuperclass();
}
while (targetClass != null && targetClass != Object.class);
}
}
繼續(xù)上面執(zhí)行到instantiateBean方法
public abstract class AbstractAutowireCapableBeanFactory {
protected BeanWrapper instantiateBean() {
// getInstancetiationStrategy方法返回了CglibSubclassingInstantiationStrategy
beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
}
}
進(jìn)入SimpleInstantiationStrategy#instanceiate方法
public class SimpleInstantiationStrategy implements InstantiationStrategy {
public Object instantiate(RootBeanDefinition bd) {
// 判斷當(dāng)前的BeanDefinition中是否有LookupOverride
// 上面查找到后已經(jīng)將其保存到了BeanDefinition中。
if (!bd.hasMethodOverrides()) {
// ...
}
else {
// 存在則通過(guò)CGLIB進(jìn)行創(chuàng)建代理
// 進(jìn)入CglibSubclassingInstantiationStrategy
return instantiateWithMethodInjection(bd, beanName, owner);
}
}
}
CglibSubclassingInstantiationStrategy
public class CglibSubclassingInstantiationStrategy extends SimpleInstantiationStrategy {
protected Object instantiateWithMethodInjection(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
return instantiateWithMethodInjection(bd, beanName, owner, null);
}
protected Object instantiateWithMethodInjection(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,
@Nullable Constructor<?> ctor, Object... args)
return new CglibSubclassCreator(bd, owner).instantiate(ctor, args);
}
}
CglibSubclassCreator
private static class CglibSubclassCreator {
private static final Class<?>[] CALLBACK_TYPES = new Class<?>[]
{NoOp.class, LookupOverrideMethodInterceptor.class, ReplaceOverrideMethodInterceptor.class};
public Object instantiate(@Nullable Constructor<?> ctor, Object... args) {
// 創(chuàng)建代理,當(dāng)前類的子類
Class<?> subclass = createEnhancedSubclass(this.beanDefinition);
// ...
}
private Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(beanDefinition.getBeanClass());
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
if (this.owner instanceof ConfigurableBeanFactory) {
ClassLoader cl = ((ConfigurableBeanFactory) this.owner).getBeanClassLoader();
enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(cl));
}
enhancer.setCallbackFilter(new MethodOverrideCallbackFilter(beanDefinition));
// 我們主要關(guān)注這里,這些都是CGLIB相關(guān)的知識(shí)了。
// 關(guān)注LookupOverrideMethodInterceptor攔截器
enhancer.setCallbackTypes(CALLBACK_TYPES);
return enhancer.createClass();
}
}
LookupOverrideMethodInterceptor攔截器
private static class LookupOverrideMethodInterceptor extends CglibIdentitySupport implements MethodInterceptor {
public Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable {
LookupOverride lo = (LookupOverride) getBeanDefinition().getMethodOverrides().getOverride(method);
Object[] argsToUse = (args.length > 0 ? args : null); // if no-arg, don't insist on args at all
// 判斷是否設(shè)置了@Lookup value值
if (StringUtils.hasText(lo.getBeanName())) {
// 根據(jù)value指定的值查找bean對(duì)象
Object bean = (argsToUse != null ? this.owner.getBean(lo.getBeanName(), argsToUse) :
this.owner.getBean(lo.getBeanName()));
return (bean.equals(null) ? null : bean);
}
// 如果沒(méi)有設(shè)置value,那么會(huì)根據(jù)方法的返回值類型在容器中查找bean
else {
ResolvableType genericReturnType = ResolvableType.forMethodReturnType(method);
return (argsToUse != null ? this.owner.getBeanProvider(genericReturnType).getObject(argsToUse) :
this.owner.getBeanProvider(genericReturnType).getObject());
}
}
}
以上就是@Lookup注解的原理
總結(jié):Spring的@Lookup注解提供了一種靈活的機(jī)制,用于在運(yùn)行時(shí)動(dòng)態(tài)地創(chuàng)建和初始化beans。通過(guò)@Lookup,開(kāi)發(fā)者可以在配置中指定一個(gè)方法,該方法會(huì)在運(yùn)行時(shí)被調(diào)用以獲取相應(yīng)的bean實(shí)例。這使得在某些特定條件下或在運(yùn)行時(shí)配置變更時(shí),能夠動(dòng)態(tài)地選擇和創(chuàng)建不同的bean實(shí)現(xiàn)。
完畢?。?!