1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.aop.framework.autoproxy.target;
  17. import org.springframework.aop.target.AbstractPrototypeBasedTargetSource;
  18. import org.springframework.aop.target.CommonsPoolTargetSource;
  19. import org.springframework.aop.target.PrototypeTargetSource;
  20. import org.springframework.aop.target.ThreadLocalTargetSource;
  21. import org.springframework.beans.factory.BeanFactory;
  22. /**
  23. * Convenient TargetSourceCreator using bean name prefixes to create one of three
  24. * well-known TargetSource types:
  25. * <li>: CommonsPoolTargetSource
  26. * <li>% ThreadLocalTargetSource
  27. * <li>! PrototypeTargetSource
  28. * @author Rod Johnson
  29. * @version $Id: QuickTargetSourceCreator.java,v 1.5 2004/04/21 11:54:43 jhoeller Exp $
  30. * @see org.springframework.aop.target.CommonsPoolTargetSource
  31. * @see org.springframework.aop.target.ThreadLocalTargetSource
  32. * @see org.springframework.aop.target.PrototypeTargetSource
  33. */
  34. public class QuickTargetSourceCreator extends AbstractPrototypeBasedTargetSourceCreator {
  35. public static final String PREFIX_COMMONS_POOL = ":";
  36. public static final String PREFIX_THREAD_LOCAL = "%";
  37. public static final String PREFIX_PROTOTYPE = "!";
  38. protected final AbstractPrototypeBasedTargetSource createPrototypeTargetSource(Object bean, String beanName,
  39. BeanFactory factory) {
  40. if (beanName.startsWith(PREFIX_COMMONS_POOL)) {
  41. CommonsPoolTargetSource cpts = new CommonsPoolTargetSource();
  42. cpts.setMaxSize(25);
  43. return cpts;
  44. }
  45. else if (beanName.startsWith(PREFIX_THREAD_LOCAL)) {
  46. return new ThreadLocalTargetSource();
  47. }
  48. else if (beanName.startsWith(PREFIX_PROTOTYPE)) {
  49. return new PrototypeTargetSource();
  50. }
  51. else {
  52. // No match. Don't create a custom target source.
  53. return null;
  54. }
  55. }
  56. }