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.target;
018
019import org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource;
020import org.springframework.aop.target.CommonsPool2TargetSource;
021import org.springframework.aop.target.PrototypeTargetSource;
022import org.springframework.aop.target.ThreadLocalTargetSource;
023import org.springframework.lang.Nullable;
024
025/**
026 * Convenient TargetSourceCreator using bean name prefixes to create one of three
027 * well-known TargetSource types:
028 * <li>: CommonsPool2TargetSource
029 * <li>% ThreadLocalTargetSource
030 * <li>! PrototypeTargetSource
031 *
032 * @author Rod Johnson
033 * @author Stephane Nicoll
034 * @see org.springframework.aop.target.CommonsPool2TargetSource
035 * @see org.springframework.aop.target.ThreadLocalTargetSource
036 * @see org.springframework.aop.target.PrototypeTargetSource
037 */
038public class QuickTargetSourceCreator extends AbstractBeanFactoryBasedTargetSourceCreator {
039
040        /**
041         * The CommonsPool2TargetSource prefix.
042         */
043        public static final String PREFIX_COMMONS_POOL = ":";
044
045        /**
046         * The ThreadLocalTargetSource prefix.
047         */
048        public static final String PREFIX_THREAD_LOCAL = "%";
049
050        /**
051         * The PrototypeTargetSource prefix.
052         */
053        public static final String PREFIX_PROTOTYPE = "!";
054
055        @Override
056        @Nullable
057        protected final AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource(
058                        Class<?> beanClass, String beanName) {
059
060                if (beanName.startsWith(PREFIX_COMMONS_POOL)) {
061                        CommonsPool2TargetSource cpts = new CommonsPool2TargetSource();
062                        cpts.setMaxSize(25);
063                        return cpts;
064                }
065                else if (beanName.startsWith(PREFIX_THREAD_LOCAL)) {
066                        return new ThreadLocalTargetSource();
067                }
068                else if (beanName.startsWith(PREFIX_PROTOTYPE)) {
069                        return new PrototypeTargetSource();
070                }
071                else {
072                        // No match. Don't create a custom target source.
073                        return null;
074                }
075        }
076
077}