Spring Framework 之 Scope

文章目录

  • 作用
  • 原理
  • Scope interface
  • 例子 ServletContextScope
  • 自定义Scope

作用

与@Component和@Bean共同作用,决定生成的Bean的生命周期,即何时创建一个新的bean,要想使用这个功能,那么使用者必须每次都调用BeanFactory#getBean, autrowire的对象不会被自动替换的。

原理

之前在分析beanFactory的时候都分析了,所有的对象都是在getBean的时候根据情况自动创建的。
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean

protected <T> T doGetBean(
			String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
			throws BeansException {

		String beanName = transformedBeanName(name);
		Object beanInstance;

		// Eagerly check singleton cache for manually registered singletons.
		Object sharedInstance = getSingleton(beanName);
		if (sharedInstance != null && args == null) {
			if (logger.isTraceEnabled()) {
				if (isSingletonCurrentlyInCreation(beanName)) {
					logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
							"' that is not fully initialized yet - a consequence of a circular reference");
				}
				else {
					logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
				}
			}
			beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, null);
		}

		else {
			// Fail if we're already creating this bean instance:
			// We're assumably within a circular reference.
			if (isPrototypeCurrentlyInCreation(beanName)) {
				throw new BeanCurrentlyInCreationException(beanName);
			}

			// Check if bean definition exists in this factory.
			BeanFactory parentBeanFactory = getParentBeanFactory();
			if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
				// Not found -> check parent.
				String nameToLookup = originalBeanName(name);
				if (parentBeanFactory instanceof AbstractBeanFactory) {
					return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
							nameToLookup, requiredType, args, typeCheckOnly);
				}
				else if (args != null) {
					// Delegation to parent with explicit args.
					return (T) parentBeanFactory.getBean(nameToLookup, args);
				}
				else if (requiredType != null) {
					// No args -> delegate to standard getBean method.
					return parentBeanFactory.getBean(nameToLookup, requiredType);
				}
				else {
					return (T) parentBeanFactory.getBean(nameToLookup);
				}
			}

			if (!typeCheckOnly) {
				markBeanAsCreated(beanName);
			}

			StartupStep beanCreation = this.applicationStartup.start("spring.beans.instantiate")
					.tag("beanName", name);
			try {
				if (requiredType != null) {
					beanCreation.tag("beanType", requiredType::toString);
				}
				RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
				checkMergedBeanDefinition(mbd, beanName, args);

				// Guarantee initialization of beans that the current bean depends on.
				String[] dependsOn = mbd.getDependsOn();
				if (dependsOn != null) {
					for (String dep : dependsOn) {
						if (isDependent(beanName, dep)) {
							throw new BeanCreationException(mbd.getResourceDescription(), beanName,
									"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
						}
						registerDependentBean(dep, beanName);
						try {
							getBean(dep);
						}
						catch (NoSuchBeanDefinitionException ex) {
							throw new BeanCreationException(mbd.getResourceDescription(), beanName,
									"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
						}
					}
				}

				// Create bean instance.
				if (mbd.isSingleton()) {
					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);
				}

				else if (mbd.isPrototype()) {
					// It's a prototype -> create a new instance.
					Object prototypeInstance = null;
					try {
						beforePrototypeCreation(beanName);
						prototypeInstance = createBean(beanName, mbd, args);
					}
					finally {
						afterPrototypeCreation(beanName);
					}
					beanInstance = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
				}

				else {
					String scopeName = mbd.getScope();
					if (!StringUtils.hasLength(scopeName)) {
						throw new IllegalStateException("No scope name defined for bean '" + beanName + "'");
					}
					Scope scope = this.scopes.get(scopeName);
					if (scope == null) {
						throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
					}
					try {
						Object scopedInstance = scope.get(beanName, () -> {
							beforePrototypeCreation(beanName);
							try {
								return createBean(beanName, mbd, args);
							}
							finally {
								afterPrototypeCreation(beanName);
							}
						});
						beanInstance = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
					}
					catch (IllegalStateException ex) {
						throw new ScopeNotActiveException(beanName, scopeName, ex);
					}
				}
			}
			catch (BeansException ex) {
				beanCreation.tag("exception", ex.getClass().toString());
				beanCreation.tag("message", String.valueOf(ex.getMessage()));
				cleanupAfterBeanCreationFailure(beanName);
				throw ex;
			}
			finally {
				beanCreation.end();
			}
		}

		return adaptBeanInstance(name, beanInstance, requiredType);
	}

精简如下:

if (mbd.isSingleton()) {
//create singleton bean
}else if (mbd.isPrototype()) {
//create prototype bean
}else {
Scope scope = this.scopes.get(scopeName);
Object scopedInstance = scope.get(beanName, () -> {
	beforePrototypeCreation(beanName);
	try {
		return createBean(beanName, mbd, args);
	}
	finally {
		afterPrototypeCreation(beanName);
	}
});
}

可以看到SINGLETON和PROTOTYPE都是硬编码的,不存在singleton和prototype的对应实现。其他的scope都称之为自定义scope,需要自定义Scope interface的实现并且调用org.springframework.beans.factory.config.ConfigurableBeanFactory#registerScope

Scope interface

public interface Scope {

	/**
	 * Return the object with the given name from the underlying scope,
	 * {@link org.springframework.beans.factory.ObjectFactory#getObject() creating it}
	 * if not found in the underlying storage mechanism.
	 * 

This is the central operation of a Scope, and the only operation * that is absolutely required. * @param name the name of the object to retrieve * @param objectFactory the {@link ObjectFactory} to use to create the scoped * object if it is not present in the underlying storage mechanism * @return the desired object (never {@code null}) * @throws IllegalStateException if the underlying scope is not currently active */ Object get(String name, ObjectFactory<?> objectFactory); /** * Remove the object with the given {@code name} from the underlying scope. *

Returns {@code null} if no object was found; otherwise * returns the removed {@code Object}. *

Note that an implementation should also remove a registered destruction * callback for the specified object, if any. It does, however, not * need to execute a registered destruction callback in this case, * since the object will be destroyed by the caller (if appropriate). *

Note: This is an optional operation. Implementations may throw * {@link UnsupportedOperationException} if they do not support explicitly * removing an object. * @param name the name of the object to remove * @return the removed object, or {@code null} if no object was present * @throws IllegalStateException if the underlying scope is not currently active * @see #registerDestructionCallback */ @Nullable Object remove(String name); /** * Register a callback to be executed on destruction of the specified * object in the scope (or at destruction of the entire scope, if the * scope does not destroy individual objects but rather only terminates * in its entirety). *

Note: This is an optional operation. This method will only * be called for scoped beans with actual destruction configuration * (DisposableBean, destroy-method, DestructionAwareBeanPostProcessor). * Implementations should do their best to execute a given callback * at the appropriate time. If such a callback is not supported by the * underlying runtime environment at all, the callback must be * ignored and a corresponding warning should be logged. *

Note that 'destruction' refers to automatic destruction of * the object as part of the scope's own lifecycle, not to the individual * scoped object having been explicitly removed by the application. * If a scoped object gets removed via this facade's {@link #remove(String)} * method, any registered destruction callback should be removed as well, * assuming that the removed object will be reused or manually destroyed. * @param name the name of the object to execute the destruction callback for * @param callback the destruction callback to be executed. * Note that the passed-in Runnable will never throw an exception, * so it can safely be executed without an enclosing try-catch block. * Furthermore, the Runnable will usually be serializable, provided * that its target object is serializable as well. * @throws IllegalStateException if the underlying scope is not currently active * @see org.springframework.beans.factory.DisposableBean * @see org.springframework.beans.factory.support.AbstractBeanDefinition#getDestroyMethodName() * @see DestructionAwareBeanPostProcessor */ void registerDestructionCallback(String name, Runnable callback); /** * Resolve the contextual object for the given key, if any. * E.g. the HttpServletRequest object for key "request". * @param key the contextual key * @return the corresponding object, or {@code null} if none found * @throws IllegalStateException if the underlying scope is not currently active */ @Nullable Object resolveContextualObject(String key); /** * Return the conversation ID for the current underlying scope, if any. *

The exact meaning of the conversation ID depends on the underlying * storage mechanism. In the case of session-scoped objects, the * conversation ID would typically be equal to (or derived from) the * {@link javax.servlet.http.HttpSession#getId() session ID}; in the * case of a custom conversation that sits within the overall session, * the specific ID for the current conversation would be appropriate. *

Note: This is an optional operation. It is perfectly valid to * return {@code null} in an implementation of this method if the * underlying storage mechanism has no obvious candidate for such an ID. * @return the conversation ID, or {@code null} if there is no * conversation ID for the current scope * @throws IllegalStateException if the underlying scope is not currently active */ @Nullable String getConversationId(); }

我们只关心Object get(String name, ObjectFactory objectFactory);, 其他的回调都没有用到过。
get方法总是在getBean的时候被调用,并且objectFactory总是会创建一个新的bean(类似prototype类型)。通常Scope的实现会自己实现一个缓存,用来决定是返回新的还是旧的bean

例子 ServletContextScope

这个scope在webApplication的整个生命周期有效
在webServer初始化的时候会注册这个生命周期
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#registerApplicationScope

	private void registerApplicationScope(ServletContext servletContext) {
		ServletContextScope appScope = new ServletContextScope(servletContext);
		getBeanFactory().registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
		// Register as ServletContext attribute, for ContextCleanupListener to detect it.
		servletContext.setAttribute(ServletContextScope.class.getName(), appScope);
	}

public class ServletContextScope implements Scope, DisposableBean {

	private final ServletContext servletContext;

	private final Map<String, Runnable> destructionCallbacks = new LinkedHashMap<>();


	/**
	 * Create a new Scope wrapper for the given ServletContext.
	 * @param servletContext the ServletContext to wrap
	 */
	public ServletContextScope(ServletContext servletContext) {
		Assert.notNull(servletContext, "ServletContext must not be null");
		this.servletContext = servletContext;
	}


	@Override
	public Object get(String name, ObjectFactory<?> objectFactory) {
		Object scopedObject = this.servletContext.getAttribute(name);
		if (scopedObject == null) {
			scopedObject = objectFactory.getObject();
			this.servletContext.setAttribute(name, scopedObject);
		}
		return scopedObject;
	}

	@Override
	@Nullable
	public Object remove(String name) {
		Object scopedObject = this.servletContext.getAttribute(name);
		if (scopedObject != null) {
			synchronized (this.destructionCallbacks) {
				this.destructionCallbacks.remove(name);
			}
			this.servletContext.removeAttribute(name);
			return scopedObject;
		}
		else {
			return null;
		}
	}

	@Override
	public void registerDestructionCallback(String name, Runnable callback) {
		synchronized (this.destructionCallbacks) {
			this.destructionCallbacks.put(name, callback);
		}
	}

	@Override
	@Nullable
	public Object resolveContextualObject(String key) {
		return null;
	}

	@Override
	@Nullable
	public String getConversationId() {
		return null;
	}


	/**
	 * Invoke all registered destruction callbacks.
	 * To be called on ServletContext shutdown.
	 * @see org.springframework.web.context.ContextCleanupListener
	 */
	@Override
	public void destroy() {
		synchronized (this.destructionCallbacks) {
			for (Runnable runnable : this.destructionCallbacks.values()) {
				runnable.run();
			}
			this.destructionCallbacks.clear();
		}
	}

}

其实就是把servletContext当作自己的缓存了, Request和Session的Scope类似

自定义Scope

1, 可以在SpringApplication初始化之前调用beanFactory.registerScope
2, 可以实现org.springframework.beans.factory.config.CustomScopeConfigurer

public class CustomScopeConfigurer implements BeanFactoryPostProcessor, BeanClassLoaderAware, Ordered {

	@Nullable
	private Map<String, Object> scopes;

	private int order = Ordered.LOWEST_PRECEDENCE;

	@Nullable
	private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();


	/**
	 * Specify the custom scopes that are to be registered.
	 * 

The keys indicate the scope names (of type String); each value * is expected to be the corresponding custom {@link Scope} instance * or class name. */ public void setScopes(Map<String, Object> scopes) { this.scopes = scopes; } /** * Add the given scope to this configurer's map of scopes. * @param scopeName the name of the scope * @param scope the scope implementation * @since 4.1.1 */ public void addScope(String scopeName, Scope scope) { if (this.scopes == null) { this.scopes = new LinkedHashMap<>(1); } this.scopes.put(scopeName, scope); } public void setOrder(int order) { this.order = order; } @Override public int getOrder() { return this.order; } @Override public void setBeanClassLoader(@Nullable ClassLoader beanClassLoader) { this.beanClassLoader = beanClassLoader; } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { if (this.scopes != null) { this.scopes.forEach((scopeKey, value) -> { if (value instanceof Scope) { beanFactory.registerScope(scopeKey, (Scope) value); } else if (value instanceof Class) { Class<?> scopeClass = (Class<?>) value; Assert.isAssignable(Scope.class, scopeClass, "Invalid scope class"); beanFactory.registerScope(scopeKey, (Scope) BeanUtils.instantiateClass(scopeClass)); } else if (value instanceof String) { Class<?> scopeClass = ClassUtils.resolveClassName((String) value, this.beanClassLoader); Assert.isAssignable(Scope.class, scopeClass, "Invalid scope class"); beanFactory.registerScope(scopeKey, (Scope) BeanUtils.instantiateClass(scopeClass)); } else { throw new IllegalArgumentException("Mapped value [" + value + "] for scope key [" + scopeKey + "] is not an instance of required type [" + Scope.class.getName() + "] or a corresponding Class or String value indicating a Scope implementation"); } }); } } }

可以看到就是利用的BeanFactoryPostProcessor实现的beanFactory.registerScope

你可能感兴趣的:(每天一篇技术博客,spring,java,后端)