#Spring源码-核心接口ObjectFactory
#ObjectFactory
ObjectFactory
@FunctionalInterface
public interface ObjectFactory {
/**
* Return an instance (possibly shared or independent)
* of the object managed by this factory.
* @return the resulting instance
* @throws BeansException in case of creation errors
*
* 获取对象的工厂方法
*/
T getObject() throws BeansException;
}
ObjectFactory
ObjectFactory
看到这个方法,有种似曾相识的感觉,没错在FactoryBean
#FactoryBean
FactoryBean
public interface FactoryBean {
String OBJECT_TYPE_ATTRIBUTE = 'factoryBeanObjectType';
@Nullable
T getObject() throws Exception;
@Nullable
Class> getObjectType();
default boolean isSingleton() {
return true;
}
}
如果不了解FactoryBean
#ObjectFactory
ObjectFactory
#ObjectFactory
FactoryBean
在获取FactoryBean
ObjectFactory
Scope接口中的get()方法:
public interface Scope {
// 省略部分源码
Object get(String name, ObjectFactory> objectFactory);
// 省略部分源码
}
这个方法主要的目的就是从对应的域中获取到指定名称的对象,而ObjectFactory>主要是为了自定义的域提供顶级拓展,而不是只仅限于request、response或者session等域。
public interface ConfigurableListableBeanFactory
extends ListableBeanFactory, AutowireCapableBeanFactory, ConfigurableBeanFactory {
// 省略部分源码
/**
* Register a special dependency type with corresponding autowired value.
* This is intended for factory/context references that are supposed
* to be autowirable but are not defined as beans in the factory:
* e.g. a dependency of type ApplicationContext resolved to the
* ApplicationContext instance that the bean is living in.
*
Note: There are no such default types registered in a plain BeanFactory,
* not even for the BeanFactory interface itself.
* @param dependencyType the dependency type to register. This will typically
* be a base interface such as BeanFactory, with extensions of it resolved
* as well if declared as an autowiring dependency (e.g. ListableBeanFactory),
* as long as the given value actually implements the extended interface.
* @param autowiredValue the corresponding autowired value. This may also be an
* implementation of the {@link org.springframework.beans.factory.ObjectFactory}
* interface, which allows for lazy resolution of the actual target value.
*/
void registerResolvableDependency(Class> dependencyType, @Nullable Object autowiredValue);
// 省略部分源码
}
这个方法看似与ObjectFactory
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);
}
// 手动注入各种经过 ObjectFactory 代理的类, 以保证线程安全
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);
}
}
上面就有使用ConfigurableListableBeanFactory#registerResolvableDependency()方法将ObjectFactory
private static class ResponseObjectFactory implements ObjectFactory, Serializable {
@Override
public ServletResponse getObject() {
ServletResponse response = currentRequestAttributes().getResponse();
if (response == null) {
throw new IllegalStateException('Current servlet response not available - ' +
'consider using RequestContextFilter instead of RequestContextListener');
}
return response;
}
@Override
public String toString() {
return 'Current HttpServletResponse';
}
}
当开发者在某一个类中需要注入ServletResponse对象时,并不会直接创建一个ServletResponse对象进行注入,而是注入一个代理类,该代理类中的方法是通过ObjectFactoryDelegatingInvocationHandler实现的。这里需要注意的是ResponseObjectFactory类中的getObject()方法是从当前线程中去获取的ServletResponse,所以这里获取的ServletResponse是线程安全的。
private static class ObjectFactoryDelegatingInvocationHandler implements InvocationHandler, Serializable {
private final ObjectFactory> objectFactory;
ObjectFactoryDelegatingInvocationHandler(ObjectFactory> objectFactory) {
this.objectFactory = objectFactory;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
case 'equals':
// Only consider equal when proxies are identical.
return (proxy == args[0]);
case 'hashCode':
// Use hashCode of proxy.
return System.identityHashCode(proxy);
case 'toString':
return this.objectFactory.toString();
}
try {
return method.invoke(this.objectFactory.getObject(), args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
}
可以看到ObjectFactoryDelegatingInvocationHandler内部会持有一个ObjectFactory>,也就是刚刚说的ResponseObjectFactory。并且会调用ObjectFactory>#getObject()方法获取对象并将其进行注入,也就是上面ResponseObjectFactory类中的getObject()方法。所以开发者可以直接在类中注入HttpServletResponse并且保证线程安全。
GitHub源码地址:https://githucom/kapbc/kapcb-spring-source/tree/master/Spring-Framework-v13
文章为作者独立观点,不代表 股票程序化软件自动交易接口观点