1 16 17 package org.springframework.aop.target; 18 19 import org.springframework.beans.BeansException; 20 import org.springframework.beans.factory.BeanDefinitionStoreException; 21 import org.springframework.beans.factory.BeanFactory; 22 import org.springframework.beans.factory.DisposableBean; 23 import org.springframework.beans.factory.config.ConfigurableBeanFactory; 24 25 40 public abstract class AbstractPrototypeBasedTargetSource extends AbstractBeanFactoryBasedTargetSource { 41 42 public void setBeanFactory(BeanFactory beanFactory) throws BeansException { 43 super.setBeanFactory(beanFactory); 44 45 if (!beanFactory.isPrototype(getTargetBeanName())) { 47 throw new BeanDefinitionStoreException( 48 "Cannot use prototype-based TargetSource against non-prototype bean with name '" + 49 getTargetBeanName() + "': instances would not be independent"); 50 } 51 } 52 53 57 protected Object newPrototypeInstance() throws BeansException { 58 if (logger.isDebugEnabled()) { 59 logger.debug("Creating new instance of bean '" + getTargetBeanName() + "'"); 60 } 61 return getBeanFactory().getBean(getTargetBeanName()); 62 } 63 64 68 protected void destroyPrototypeInstance(Object target) { 69 if (logger.isDebugEnabled()) { 70 logger.debug("Destroying instance of bean '" + getTargetBeanName() + "'"); 71 } 72 if (getBeanFactory() instanceof ConfigurableBeanFactory) { 73 ((ConfigurableBeanFactory) getBeanFactory()).destroyBean(getTargetBeanName(), target); 74 } 75 else if (target instanceof DisposableBean) { 76 try { 77 ((DisposableBean) target).destroy(); 78 } 79 catch (Throwable ex) { 80 logger.error("Couldn't invoke destroy method of bean with name '" + getTargetBeanName() + "'", ex); 81 } 82 } 83 } 84 85 } 86 | Popular Tags |