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