欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring @Lookup深入分析實現(xiàn)原理

 更新時間:2023年01月03日 09:38:48   作者:程序員小潘  
這篇文章主要介紹了Spring @Lookup實現(xiàn)原理,我們知道在spring容器中單獨的一個抽象類是不能成為一個bean的,那么有沒有辦法呢?這個時候我們可以使用Lookup注解

1. 前言

在使用Spring的時候,往單例bean注入原型bean時,原型bean可能會失效,如下:

@Component
public class Person {
    @Autowired
    Car car;
    public Car getCar() {
        return car;
    }
}
@Component
@Scope("prototype")
public class Car {
}

調(diào)用Person#getCar()方法返回的總是同一個Car對象,這也很好理解,因為Person是單例的,Spring在創(chuàng)建Person時只會注入一次Car對象,以后Car都不會再改變了。

怎么解決這個問題呢?Spring提供了多種方式來獲取原型bean。

2. 解決方案

解決方案有很多,本文重點分析@Lookup注解的方式。

1、每次從ApplicationContext重新獲取bean。

@Component
public class Person implements ApplicationContextAware {
    private ApplicationContext applicationContext;
    @Lookup
    public Car getCar() {
        return applicationContext.getBean(Car.class);
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

2、通過CGLIB生成Car子類代理對象,每次都從容器內(nèi)獲取bean執(zhí)行。

@Component
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class Car {
}

3、通過**@Lookup**注解的方式,方法體已經(jīng)不重要了,代理對象每次都會從容器中重新獲取bean。

@Component
public class Person {
    @Lookup
    public Car getCar() {
        return null;
    }
}

3. 源碼分析

為什么方法上加了@Lookup注解,調(diào)用該方法就能拿到原型bean了呢?其實縱觀上述三種方式,要想拿到原型bean,底層原理都是一樣的,那就是每次都通過ApplicationContext#getBean()方法從容器中重新獲取,只要Car本身是原型的,Spring就會保證每次拿到的都是新創(chuàng)建的Car實例。

**@Lookup**注解也是通過生成代理類的方式,重寫被標記的方法,每次都從ApplicationContext獲取bean。

1、Spring加載Person的時候,容器內(nèi)不存在該bean,那首先就是要實例化Person對象。Spring會通過SimpleInstantiationStrategy#instantiate()方法去實例化Person。

public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
    /**
     * 如果bean沒有要重寫的方法,直接反射調(diào)用構(gòu)造函數(shù)創(chuàng)建對象
     * 反之,需要通過CGLIB創(chuàng)建增強子類代理對象
     */
    if (!bd.hasMethodOverrides()) {
        Constructor<?> constructorToUse;
        synchronized (bd.constructorArgumentLock) {
            constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
            if (constructorToUse == null) {
                final Class<?> clazz = bd.getBeanClass();
                if (clazz.isInterface()) {
                    throw new BeanInstantiationException(clazz, "Specified class is an interface");
                }
                try {
                    if (System.getSecurityManager() != null) {
                        constructorToUse = AccessController.doPrivileged(
                                (PrivilegedExceptionAction<Constructor<?>>) clazz::getDeclaredConstructor);
                    } else {
                        constructorToUse = clazz.getDeclaredConstructor();
                    }
                    bd.resolvedConstructorOrFactoryMethod = constructorToUse;
                } catch (Throwable ex) {
                    throw new BeanInstantiationException(clazz, "No default constructor found", ex);
                }
            }
        }
        return BeanUtils.instantiateClass(constructorToUse);
    } else {
        /**
         * 存在 lookup-method 和 replaced-method
         * 通過CGLIB生成代理類
         */
        return instantiateWithMethodInjection(bd, beanName, owner);
    }
}

BeanDefinition有一個屬性**methodOverrides**,它里面存放的是當(dāng)前bean里面是否有需要被重寫的方法,這些需要被重寫的方法可能是**lookup-method****replaced-method**。如果沒有需要重寫的方法,則直接通過反射調(diào)用構(gòu)造函數(shù)來實例化對象;如果有需要重寫的方法,這個時候就不能直接實例化對象了,需要通過CGLIB來創(chuàng)建增強子類,把父類的方法給重寫掉。

于是會調(diào)用instantiateWithMethodInjection()方法來實例化bean,最終是通過CglibSubclassCreator來實例化。

@Override
protected Object instantiateWithMethodInjection(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,
        @Nullable Constructor<?> ctor, Object... args) {
    // Must generate CGLIB subclass...
    return new CglibSubclassCreator(bd, owner).instantiate(ctor, args);
}

2、CglibSubclassCreator創(chuàng)建CGLIB子類,重寫父類方法。Spring會通過Enhancer來創(chuàng)建增強子類,被@Lookup標記的方法會被LookupOverrideMethodInterceptor攔截。

public Object instantiate(@Nullable Constructor<?> ctor, Object... args) {
    Class<?> subclass = createEnhancedSubclass(this.beanDefinition);
    Object instance;
    if (ctor == null) {
        instance = BeanUtils.instantiateClass(subclass);
    }
    else {
        try {
            Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes());
            instance = enhancedSubclassConstructor.newInstance(args);
        }
        catch (Exception ex) {
            throw new BeanInstantiationException(this.beanDefinition.getBeanClass(),
                    "Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex);
        }
    }
    Factory factory = (Factory) instance;
    factory.setCallbacks(new Callback[] {NoOp.INSTANCE,
            new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),
            new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)});
    return instance;
}

3、LookupOverrideMethodInterceptor要攔截被@Lookup標記的方法,必然要實現(xiàn)intercept()方法。邏輯很簡單,就是每次都調(diào)用BeanFactory#getBean()從容器中獲取bean。

@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable {
    // Cast is safe, as CallbackFilter filters are used selectively.
    LookupOverride lo = (LookupOverride) getBeanDefinition().getMethodOverrides().getOverride(method);
    Assert.state(lo != null, "LookupOverride not found");
    Object[] argsToUse = (args.length > 0 ? args : null);  // if no-arg, don't insist on args at all
    if (StringUtils.hasText(lo.getBeanName())) {
        return (argsToUse != null ? this.owner.getBean(lo.getBeanName(), argsToUse) :
                this.owner.getBean(lo.getBeanName()));
    }
    else {
        return (argsToUse != null ? this.owner.getBean(method.getReturnType(), argsToUse) :
                this.owner.getBean(method.getReturnType()));
    }
}

4. 總結(jié)

一個bean一旦擁有被@Lookup注解標記的方法,就意味著該方法需要被重寫掉,Spring在實例化bean的時候會自動基于CGLIB生成增強子類對象重寫掉父類方法。此時父類被@Lookup注解標記的方法體已經(jīng)不重要了,不會被執(zhí)行了,CGLIB子類會通過LookupOverrideMethodInterceptor攔截掉被@Lookup注解標記的方法。方法體重寫的邏輯也很簡單,就是每次都通過BeanFactory獲取bean,只要bean本身是原型的,每次拿到的都將是不同的實例。

到此這篇關(guān)于Spring @Lookup深入分析實現(xiàn)原理的文章就介紹到這了,更多相關(guān)Spring @Lookup內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解析java基本數(shù)據(jù)類型傳遞與引用傳遞區(qū)別

    解析java基本數(shù)據(jù)類型傳遞與引用傳遞區(qū)別

    這篇文章主要介紹了java基本數(shù)據(jù)類型傳遞與引用傳遞區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 深入解析Java的Hibernate框架中的一對一關(guān)聯(lián)映射

    深入解析Java的Hibernate框架中的一對一關(guān)聯(lián)映射

    這篇文章主要介紹了Java的Hibernate框架的一對一關(guān)聯(lián)映射,包括對一對一外聯(lián)映射的講解,需要的朋友可以參考下
    2016-01-01
  • mybatis-plus中更新null值的問題解決

    mybatis-plus中更新null值的問題解決

    本文主要介紹 mybatis-plus 中常使用的 update 相關(guān)方法的區(qū)別,以及更新 null 的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-04-04
  • Java中final修飾的方法是否可以被重寫示例詳解

    Java中final修飾的方法是否可以被重寫示例詳解

    這篇文章主要給大家介紹了關(guān)于Java中final修飾的方法是否可以被重寫的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • SpringMvc入門指南(必看)

    SpringMvc入門指南(必看)

    下面小編就為大家?guī)硪黄猄pringMvc入門指南(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • SpringBoot項目啟動后立馬自動關(guān)閉的解決方案

    SpringBoot項目啟動后立馬自動關(guān)閉的解決方案

    這篇文章主要介紹了SpringBoot項目啟動后立馬自動關(guān)閉的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Java魔法值處理的四種方式

    Java魔法值處理的四種方式

    這篇文章主要介紹了Java魔法值處理的四種方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Java動態(tài)腳本Groovy獲取Bean技巧

    Java動態(tài)腳本Groovy獲取Bean技巧

    這篇文章主要給大家分享的是Java動態(tài)腳本Groovy獲取Bean技巧,在Java代碼中當(dāng)我們需要一個Bean對象,通常會使用spring中@Autowired注解,用來自動裝配對象。下面我們一起進入文章學(xué)習(xí)個表格多 詳細內(nèi)容吧

    2021-12-12
  • 關(guān)于Java中常見的負載均衡算法

    關(guān)于Java中常見的負載均衡算法

    這篇文章主要介紹了關(guān)于Java中常見的負載均衡算法,負載平衡是一種電子計算機技術(shù),用來在多個計算機、網(wǎng)絡(luò)連接、CPU、磁盤驅(qū)動器或其他資源中分配負載,以達到優(yōu)化資源使用、最大化吞吐率、最小化響應(yīng)時間、同時避免過載的目的,需要的朋友可以參考下
    2023-08-08
  • Mybatis-plus在項目中的簡單應(yīng)用

    Mybatis-plus在項目中的簡單應(yīng)用

    Mybatis-plus是Spring框架中OOM的一大利器,其簡單易用參考官網(wǎng)文檔即可很快上手,本文主要介紹了邏輯刪除,自動填充,分頁插件等的簡單使用,感興趣的可以了解一下
    2021-07-07

最新評論