001/*
002 * Copyright 2002-2018 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.aop.framework.autoproxy;
018
019import org.springframework.core.NamedThreadLocal;
020import org.springframework.lang.Nullable;
021
022/**
023 * Holder for the current proxy creation context, as exposed by auto-proxy creators
024 * such as {@link AbstractAdvisorAutoProxyCreator}.
025 *
026 * @author Juergen Hoeller
027 * @author Ramnivas Laddad
028 * @since 2.5
029 */
030public final class ProxyCreationContext {
031
032        /** ThreadLocal holding the current proxied bean name during Advisor matching. */
033        private static final ThreadLocal<String> currentProxiedBeanName =
034                        new NamedThreadLocal<>("Name of currently proxied bean");
035
036
037        private ProxyCreationContext() {
038        }
039
040
041        /**
042         * Return the name of the currently proxied bean instance.
043         * @return the name of the bean, or {@code null} if none available
044         */
045        @Nullable
046        public static String getCurrentProxiedBeanName() {
047                return currentProxiedBeanName.get();
048        }
049
050        /**
051         * Set the name of the currently proxied bean instance.
052         * @param beanName the name of the bean, or {@code null} to reset it
053         */
054        static void setCurrentProxiedBeanName(@Nullable String beanName) {
055                if (beanName != null) {
056                        currentProxiedBeanName.set(beanName);
057                }
058                else {
059                        currentProxiedBeanName.remove();
060                }
061        }
062
063}