Spring @Lookup深入分析實現(xiàn)原理
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ū)別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
深入解析Java的Hibernate框架中的一對一關(guān)聯(lián)映射
這篇文章主要介紹了Java的Hibernate框架的一對一關(guān)聯(lián)映射,包括對一對一外聯(lián)映射的講解,需要的朋友可以參考下2016-01-01
SpringBoot項目啟動后立馬自動關(guān)閉的解決方案
這篇文章主要介紹了SpringBoot項目啟動后立馬自動關(guān)閉的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03

