001/*
002 * Copyright 2002-2020 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.context.support;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024
025import org.springframework.beans.factory.ObjectFactory;
026import org.springframework.beans.factory.config.Scope;
027import org.springframework.core.NamedThreadLocal;
028import org.springframework.lang.Nullable;
029
030/**
031 * A simple thread-backed {@link Scope} implementation.
032 *
033 * <p><b>NOTE:</b> This thread scope is not registered by default in common contexts.
034 * Instead, you need to explicitly assign it to a scope key in your setup, either through
035 * {@link org.springframework.beans.factory.config.ConfigurableBeanFactory#registerScope}
036 * or through a {@link org.springframework.beans.factory.config.CustomScopeConfigurer} bean.
037 *
038 * <p>{@code SimpleThreadScope} <em>does not clean up any objects</em> associated with it.
039 * It is therefore typically preferable to use a request-bound scope implementation such
040 * as {@code org.springframework.web.context.request.RequestScope} in web environments,
041 * implementing the full lifecycle for scoped attributes (including reliable destruction).
042 *
043 * <p>For an implementation of a thread-based {@code Scope} with support for destruction
044 * callbacks, refer to
045 * <a href="https://www.springbyexample.org/examples/custom-thread-scope-module.html">Spring by Example</a>.
046 *
047 * <p>Thanks to Eugene Kuleshov for submitting the original prototype for a thread scope!
048 *
049 * @author Arjen Poutsma
050 * @author Juergen Hoeller
051 * @since 3.0
052 * @see org.springframework.web.context.request.RequestScope
053 */
054public class SimpleThreadScope implements Scope {
055
056        private static final Log logger = LogFactory.getLog(SimpleThreadScope.class);
057
058        private final ThreadLocal<Map<String, Object>> threadScope =
059                        new NamedThreadLocal<Map<String, Object>>("SimpleThreadScope") {
060                                @Override
061                                protected Map<String, Object> initialValue() {
062                                        return new HashMap<>();
063                                }
064                        };
065
066
067        @Override
068        public Object get(String name, ObjectFactory<?> objectFactory) {
069                Map<String, Object> scope = this.threadScope.get();
070                // NOTE: Do NOT modify the following to use Map::computeIfAbsent. For details,
071                // see https://github.com/spring-projects/spring-framework/issues/25801.
072                Object scopedObject = scope.get(name);
073                if (scopedObject == null) {
074                        scopedObject = objectFactory.getObject();
075                        scope.put(name, scopedObject);
076                }
077                return scopedObject;
078        }
079
080        @Override
081        @Nullable
082        public Object remove(String name) {
083                Map<String, Object> scope = this.threadScope.get();
084                return scope.remove(name);
085        }
086
087        @Override
088        public void registerDestructionCallback(String name, Runnable callback) {
089                logger.warn("SimpleThreadScope does not support destruction callbacks. " +
090                                "Consider using RequestScope in a web environment.");
091        }
092
093        @Override
094        @Nullable
095        public Object resolveContextualObject(String key) {
096                return null;
097        }
098
099        @Override
100        public String getConversationId() {
101                return Thread.currentThread().getName();
102        }
103
104}