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.apache.commons.logging.Log;
  18. import org.apache.commons.logging.LogFactory;
  19. import org.springframework.aop.TargetSource;
  20. import org.springframework.aop.framework.autoproxy.TargetSourceCreator;
  21. import org.springframework.aop.target.AbstractPrototypeBasedTargetSource;
  22. import org.springframework.beans.factory.BeanFactory;
  23. import org.springframework.beans.factory.support.BeanDefinitionRegistry;
  24. import org.springframework.beans.factory.support.DefaultListableBeanFactory;
  25. import org.springframework.beans.factory.support.RootBeanDefinition;
  26. /**
  27. * Convenient superclass for TargetSourceCreators that require creating
  28. * multiple instances of a prototype bean.
  29. * @author Rod Johnson
  30. * @version $Id: AbstractPrototypeBasedTargetSourceCreator.java,v 1.1 2004/04/21 11:54:41 jhoeller Exp $
  31. * @see org.springframework.aop.target.AbstractPrototypeBasedTargetSource
  32. */
  33. public abstract class AbstractPrototypeBasedTargetSourceCreator implements TargetSourceCreator {
  34. protected final Log logger = LogFactory.getLog(getClass());
  35. public final TargetSource getTargetSource(Object bean, String beanName, BeanFactory factory) {
  36. AbstractPrototypeBasedTargetSource prototypeTargetSource = createPrototypeTargetSource(bean, beanName, factory);
  37. if (prototypeTargetSource == null) {
  38. return null;
  39. }
  40. else {
  41. if (!(factory instanceof BeanDefinitionRegistry)) {
  42. logger.warn("Cannot do autopooling with a BeanFactory that doesn't implement BeanDefinitionRegistry");
  43. return null;
  44. }
  45. BeanDefinitionRegistry definitionRegistry = (BeanDefinitionRegistry) factory;
  46. RootBeanDefinition definition = (RootBeanDefinition) definitionRegistry.getBeanDefinition(beanName);
  47. logger.info("Configuring AbstractPrototypeBasedTargetSource...");
  48. // Infinite cycle will result if we don't use a different factory,
  49. // because a getBean() call with this beanName will go through the autoproxy
  50. // infrastructure again.
  51. // We to override just this bean definition, as it may reference other beans
  52. // and we're happy to take the parent's definition for those.
  53. DefaultListableBeanFactory beanFactory2 = new DefaultListableBeanFactory(factory);
  54. // Override the prototype bean
  55. beanFactory2.registerBeanDefinition(beanName, definition);
  56. // Complete configuring the PrototypeTargetSource
  57. prototypeTargetSource.setTargetBeanName(beanName);
  58. prototypeTargetSource.setBeanFactory(beanFactory2);
  59. return prototypeTargetSource;
  60. }
  61. }
  62. /**
  63. * Subclasses must implement this method to return a new AbstractPrototypeBasedTargetSource
  64. * if they wish to create a custom TargetSource for this bean, or null if they are
  65. * not interested it in, in which case no special target source will be created.
  66. * Subclasses should not call setTargetBeanName() or setBeanFactory() on the
  67. * AbstractPrototypeBasedTargetSource: This class's implementation of getTargetSource()
  68. * will do that.
  69. * @return the AbstractPrototypeBasedTargetSource, or null if we don't match this
  70. */
  71. protected abstract AbstractPrototypeBasedTargetSource createPrototypeTargetSource(Object bean, String beanName,
  72. BeanFactory factory);
  73. }