1 16 17 package org.springframework.aop.framework.autoproxy.target; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 import org.springframework.aop.TargetSource; 23 import org.springframework.aop.framework.autoproxy.TargetSourceCreator; 24 import org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource; 25 import org.springframework.beans.factory.BeanFactory; 26 import org.springframework.beans.factory.BeanFactoryAware; 27 import org.springframework.beans.factory.DisposableBean; 28 import org.springframework.beans.factory.support.AbstractBeanFactory; 29 import org.springframework.beans.factory.support.DefaultListableBeanFactory; 30 import org.springframework.beans.factory.support.RootBeanDefinition; 31 32 50 public abstract class AbstractBeanFactoryBasedTargetSourceCreator 51 implements TargetSourceCreator, BeanFactoryAware, DisposableBean { 52 53 protected final Log logger = LogFactory.getLog(getClass()); 54 55 private AbstractBeanFactory beanFactory; 56 57 private DefaultListableBeanFactory internalBeanFactory; 58 59 60 public final void setBeanFactory(BeanFactory beanFactory) { 61 if (!(beanFactory instanceof AbstractBeanFactory)) { 62 throw new IllegalArgumentException ( 63 "Cannot do auto-TargetSource creation with a BeanFactory that doesn't extend AbstractBeanFactory: " + 64 beanFactory); 65 } 66 this.beanFactory = (AbstractBeanFactory) beanFactory; 67 this.internalBeanFactory = new DefaultListableBeanFactory(beanFactory); 68 } 69 70 73 protected final BeanFactory getBeanFactory() { 74 return this.beanFactory; 75 } 76 77 80 public void destroy() { 81 this.internalBeanFactory.destroySingletons(); 82 } 83 84 85 public final TargetSource getTargetSource(Class beanClass, String beanName) { 86 AbstractBeanFactoryBasedTargetSource targetSource = 87 createBeanFactoryBasedTargetSource(beanClass, beanName); 88 if (targetSource == null) { 89 return null; 90 } 91 92 if (logger.isDebugEnabled()) { 93 logger.debug("Configuring AbstractBeanFactoryBasedTargetSource: " + targetSource); 94 } 95 96 RootBeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName); 100 RootBeanDefinition bdCopy = new RootBeanDefinition(bd); 101 bdCopy.setSingleton(!isPrototypeBased()); 102 this.internalBeanFactory.registerBeanDefinition(beanName, bdCopy); 103 104 targetSource.setTargetBeanName(beanName); 106 targetSource.setBeanFactory(this.internalBeanFactory); 107 108 return targetSource; 109 } 110 111 117 protected boolean isPrototypeBased() { 118 return true; 119 } 120 121 132 protected abstract AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource( 133 Class beanClass, String beanName); 134 135 } 136 | Popular Tags |