Spring中這兩個(gè)對(duì)象ObjectFactory與FactoryBean接口你使用過嗎?
1 接口對(duì)比
ObjectFactory
@FunctionalInterface
public interface ObjectFactory<T> {
T getObject() throws BeansException;
}
就是一個(gè)普通的函數(shù)式對(duì)象接口。
FactoryBean
public interface FactoryBean<T> {
// 返回真實(shí)的對(duì)象
T getObject() throws Exception;
// 返回對(duì)象類型
Class<?> getObjectType();
// 是否單例;如果是單例會(huì)將其創(chuàng)建的對(duì)象緩存到緩存池中。
boolean isSingleton();
}
該接口就是一個(gè)工廠Bean,在獲取對(duì)象時(shí),先判斷當(dāng)前對(duì)象是否是FactoryBean,如果是再根據(jù)getObjectType的返回類型判斷是否需要的類型,如果匹配則會(huì)調(diào)用getObject方法返回真實(shí)的對(duì)象。該接口用來自定義對(duì)象的創(chuàng)建。
注意:如果A.class 實(shí)現(xiàn)了FactoryBean,如果想獲取A本身這個(gè)對(duì)象則bean的名稱必須添加前綴 '&',也就是獲取Bean則需要ctx.getBean("&a")
當(dāng)注入屬性是ObjectFactory或者ObjectProvider類型時(shí),系統(tǒng)會(huì)直接創(chuàng)建DependencyObjectProvider對(duì)象然后進(jìn)行注入,只有在真正調(diào)用getObject方法的時(shí)候系統(tǒng)才會(huì)根據(jù)字段上的泛型類型進(jìn)行查找注入。
2 實(shí)際應(yīng)用
ObjectFactory在Spring源碼中應(yīng)用的比較多
2.1 創(chuàng)建Bean實(shí)例
public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {
protected <T> T doGetBean(String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly) throws BeansException {
// other code
if (mbd.isSingleton()) {
//getSingleton方法的第二個(gè)參數(shù)就是ObjectFactory對(duì)象(這里應(yīng)用了lamda表達(dá)式)
sharedInstance = getSingleton(beanName, () -> {
try {
return createBean(beanName, mbd, args);
} catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
});
beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
// other code
}
}
這里的getSingleton方法就是通過不同的Scope(singleton,prototype,request,session)創(chuàng)建Bean;具體的創(chuàng)建細(xì)節(jié)都是交個(gè)ObjectFactory來完成。
2.2 Servlet API注入
在Controller中注入Request,Response相關(guān)對(duì)象時(shí)也是通過ObjectFactory接口。
容器啟動(dòng)時(shí)實(shí)例化的上下文對(duì)象是AnnotationConfigServletWebServerApplicationContext;
調(diào)用在AbstractApplicationContext#refresh.postProcessBeanFactory
public class AnnotationConfigServletWebServerApplicationContext {
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
super.postProcessBeanFactory(beanFactory);
if (this.basePackages != null && this.basePackages.length > 0) {
this.scanner.scan(this.basePackages);
}
if (!this.annotatedClasses.isEmpty()) {
this.reader.register(ClassUtils.toClassArray(this.annotatedClasses));
}
}
}
super.postProcessBeanFactory(beanFactory)方法進(jìn)入到ServletWebServerApplicationContext中
public class ServletWebServerApplicationContext extends GenericWebApplicationContext implements ConfigurableWebServerApplicationContext {
@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
beanFactory.addBeanPostProcessor(new WebApplicationContextServletContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(ServletContextAware.class);
registerWebApplicationScopes();
}
private void registerWebApplicationScopes() {
ExistingWebApplicationScopes existingScopes = new ExistingWebApplicationScopes(getBeanFactory());
WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory());
existingScopes.restore();
}
}
WebApplicationContextUtils工具類
public abstract class WebApplicationContextUtils {
public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory) {
registerWebApplicationScopes(beanFactory, null);
}
public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory, @Nullable ServletContext sc) {
beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope());
if (sc != null) {
ServletContextScope appScope = new ServletContextScope(sc);
beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
// Register as ServletContext attribute, for ContextCleanupListener to detect it.
sc.setAttribute(ServletContextScope.class.getName(), appScope);
}
beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());
beanFactory.registerResolvableDependency(ServletResponse.class, new ResponseObjectFactory());
beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());
beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
if (jsfPresent) {
FacesDependencyRegistrar.registerFacesDependencies(beanFactory);
}
}
}
這里的RequestObjectFactory,ResponseObjectFactory,SessionObjectFactory,WebRequestObjectFactory都是ObjectFactory接口。
這幾個(gè)接口實(shí)際獲取的對(duì)象都是從當(dāng)前線程的上下文中獲取的(通過ThreadLocal),所以在Controller中直接屬性注入相應(yīng)的對(duì)象是線程安全的。
注意:這里registerResolvableDependency方法意圖就是當(dāng)有Bean需要注入相應(yīng)的Request,Response對(duì)象時(shí)直接注入第二個(gè)參數(shù)的值即可。
2.3 自定義定義ObjectFactory
在IOC容器,如果有兩個(gè)相同類型的Bean,這時(shí)候在注入的時(shí)候肯定是會(huì)報(bào)錯(cuò)的,示例如下:
public interface AccountDAO {
}
@Component
public class AccountADAO implements AccountDAO {
}
@Component
public class AccountBDAO implements AccountDAO {
}
@RestController
@RequestMapping("/accounts")
public class AccountController {
@Resource
private AccountDAO dao ;
}
當(dāng)我們有如上的Bean后,啟動(dòng)容器會(huì)報(bào)錯(cuò)如下:
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.pack.objectfactory.AccountDAO' available: expected single matching bean but found 2: accountADAO,accountBDAO
at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:220) ~[spring-beans-5.2.13.RELEASE.jar:5.2.13.RELEASE]
期望一個(gè)AccountDAO類型的Bean,但當(dāng)前環(huán)境卻有兩個(gè)。
解決這個(gè)辦法可以通過@Primary和@Qualifier來解決,這兩個(gè)方法這里不做介紹;接下來我們通過BeanFactory#registerResolvableDependency的方式來解決;
自定義BeanFactoryPostProcessor
// 方式1:直接通過beanFactory獲取指定的bean注入。
@Component
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// beanFactory的實(shí)例是DefaultListableBeanFactory,該實(shí)例內(nèi)部維護(hù)了一個(gè)ConcurrentMap resolvableDependencies 的集合,Class作為key。
beanFactory.registerResolvableDependency(AccountDAO.class, beanFactory.getBean("accountBDAO"));
}
}
自定義ObjectFactory
public class AccountObjectFactory implements ObjectFactory<AccountDAO> {
@Override
public AccountDAO getObject() throws BeansException {
return new AccountBDAO() ;
}
}
// 對(duì)應(yīng)的BeanFactoryPostProcessor
beanFactory.registerResolvableDependency(AccountDAO.class, new AccountObjectFactory());
當(dāng)一個(gè)Bean的屬性在填充(注入)時(shí)調(diào)用AbstractAutowireCapableBeanFactory.populateBean方法時(shí),會(huì)在當(dāng)前的IOC容器中查找符合的Bean,最終執(zhí)行如下方法:
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
protected Map<String, Object> findAutowireCandidates(@Nullable String beanName, Class<?> requiredType, DependencyDescriptor descriptor) {
// 從當(dāng)前IOC容器中查找所有指定類型的Bean
String[] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this, requiredType, true, descriptor.isEager());
Map<String, Object> result = new LinkedHashMap<>(candidateNames.length);
// 遍歷DefaultListableBeanFactory對(duì)象中通過registerResolvableDependency方法注冊(cè)的
for (Map.Entry<Class<?>, Object> classObjectEntry : this.resolvableDependencies.entrySet()) {
Class<?> autowiringType = classObjectEntry.getKey();
if (autowiringType.isAssignableFrom(requiredType)) {
Object autowiringValue = classObjectEntry.getValue();
// 解析自動(dòng)裝配的類型值(主要就是判斷當(dāng)前的值對(duì)象是否是ObjectFactory對(duì)象)
autowiringValue = AutowireUtils.resolveAutowiringValue(autowiringValue, requiredType);
if (requiredType.isInstance(autowiringValue)) {
// 要注意這里,如果通過registerResolvableDependency添加的對(duì)象是個(gè)ObjectFactory,那么最終會(huì)調(diào)用factory.getObject方法返回真實(shí)的對(duì)象并且加入到result集合中。這時(shí)候相當(dāng)于當(dāng)前類型還是找到了多個(gè)Bean還是會(huì)報(bào)錯(cuò)。
result.put(ObjectUtils.identityToString(autowiringValue), autowiringValue);
break;
}
}
}
for (String candidate : candidateNames) {
if (!isSelfReference(beanName, candidate) && isAutowireCandidate(candidate, descriptor)) {
addCandidateEntry(result, candidate, descriptor, requiredType);
}
}
return result;
}
}
// AutowireUtils.resolveAutowiringValue方法
// 該方法中會(huì)判斷對(duì)象是否是ObjectFactory
public static Object resolveAutowiringValue(Object autowiringValue, Class<?> requiredType) {
if (autowiringValue instanceof ObjectFactory && !requiredType.isInstance(autowiringValue)) {
ObjectFactory<?> factory = (ObjectFactory<?>) autowiringValue;
if (autowiringValue instanceof Serializable && requiredType.isInterface()) {
autowiringValue = Proxy.newProxyInstance(requiredType.getClassLoader(), new Class<?>[] {requiredType}, new ObjectFactoryDelegatingInvocationHandler(factory));
} else {
// 進(jìn)入這里之間調(diào)用getObject返回對(duì)象
return factory.getObject();
}
}
return autowiringValue;
}
完畢!??!