1 16 17 package org.springframework.beans.factory.config; 18 19 import org.springframework.beans.BeansException; 20 import org.springframework.beans.factory.BeanFactory; 21 import org.springframework.beans.factory.BeanFactoryAware; 22 import org.springframework.beans.factory.FactoryBeanNotInitializedException; 23 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 24 import org.springframework.beans.factory.SmartFactoryBean; 25 26 46 public class BeanReferenceFactoryBean implements SmartFactoryBean, BeanFactoryAware { 47 48 private String targetBeanName; 49 50 private BeanFactory beanFactory; 51 52 53 61 public void setTargetBeanName(String targetBeanName) { 62 this.targetBeanName = targetBeanName; 63 } 64 65 public void setBeanFactory(BeanFactory beanFactory) { 66 this.beanFactory = beanFactory; 67 if (this.targetBeanName == null) { 68 throw new IllegalArgumentException ("'targetBeanName' is required"); 69 } 70 if (!this.beanFactory.containsBean(this.targetBeanName)) { 71 throw new NoSuchBeanDefinitionException(this.targetBeanName, this.beanFactory.toString()); 72 } 73 } 74 75 76 public Object getObject() throws BeansException { 77 if (this.beanFactory == null) { 78 throw new FactoryBeanNotInitializedException(); 79 } 80 return this.beanFactory.getBean(this.targetBeanName); 81 } 82 83 public Class getObjectType() { 84 return this.beanFactory.getType(this.targetBeanName); 85 } 86 87 public boolean isSingleton() { 88 return this.beanFactory.isSingleton(this.targetBeanName); 89 } 90 91 public boolean isPrototype() { 92 return this.beanFactory.isPrototype(this.targetBeanName); 93 } 94 95 } 96 | Popular Tags |