1 16 17 package org.springframework.beans.factory.config; 18 19 import org.springframework.beans.BeanWrapper; 20 import org.springframework.beans.BeanWrapperImpl; 21 import org.springframework.beans.BeansException; 22 import org.springframework.beans.FatalBeanException; 23 import org.springframework.beans.factory.BeanFactory; 24 import org.springframework.beans.factory.BeanFactoryAware; 25 import org.springframework.beans.factory.BeanFactoryUtils; 26 import org.springframework.beans.factory.BeanNameAware; 27 import org.springframework.beans.factory.FactoryBean; 28 import org.springframework.util.StringUtils; 29 30 82 public class PropertyPathFactoryBean implements FactoryBean, BeanNameAware, BeanFactoryAware { 83 84 private BeanWrapper targetBeanWrapper; 85 86 private String targetBeanName; 87 88 private String propertyPath; 89 90 private Class resultType; 91 92 private String beanName; 93 94 private BeanFactory beanFactory; 95 96 97 104 public void setTargetObject(Object targetObject) { 105 this.targetBeanWrapper = new BeanWrapperImpl(targetObject); 106 } 107 108 115 public void setTargetBeanName(String targetBeanName) { 116 this.targetBeanName = StringUtils.trimAllWhitespace(targetBeanName); 117 } 118 119 124 public void setPropertyPath(String propertyPath) { 125 this.propertyPath = StringUtils.trimAllWhitespace(propertyPath); 126 } 127 128 136 public void setResultType(Class resultType) { 137 this.resultType = resultType; 138 } 139 140 146 public void setBeanName(String beanName) { 147 this.beanName = StringUtils.trimAllWhitespace(BeanFactoryUtils.originalBeanName(beanName)); 148 } 149 150 151 public void setBeanFactory(BeanFactory beanFactory) { 152 this.beanFactory = beanFactory; 153 154 if (this.targetBeanWrapper != null && this.targetBeanName != null) { 155 throw new IllegalArgumentException ("Specify either targetObject or targetBeanName, not both"); 156 } 157 158 if (this.targetBeanWrapper == null && this.targetBeanName == null) { 159 if (this.propertyPath != null) { 160 throw new IllegalArgumentException ( 161 "Specify targetObject or targetBeanName in combination with propertyPath"); 162 } 163 164 int dotIndex = this.beanName.indexOf('.'); 166 if (dotIndex == -1) { 167 throw new IllegalArgumentException ( 168 "Neither targetObject nor targetBeanName specified, and PropertyPathFactoryBean " + 169 "bean name '" + this.beanName + "' does not follow 'beanName.property' syntax"); 170 } 171 this.targetBeanName = this.beanName.substring(0, dotIndex); 172 this.propertyPath = this.beanName.substring(dotIndex + 1); 173 } 174 175 else if (this.propertyPath == null) { 176 throw new IllegalArgumentException ("propertyPath is required"); 178 } 179 180 if (this.targetBeanWrapper == null && this.beanFactory.isSingleton(this.targetBeanName)) { 181 this.targetBeanWrapper = new BeanWrapperImpl(this.beanFactory.getBean(this.targetBeanName)); 183 this.resultType = this.targetBeanWrapper.getPropertyType(this.propertyPath); 184 } 185 } 186 187 188 public Object getObject() throws BeansException { 189 BeanWrapper target = this.targetBeanWrapper; 190 if (target == null) { 191 target = new BeanWrapperImpl(this.beanFactory.getBean(this.targetBeanName)); 193 } 194 195 Object value = target.getPropertyValue(this.propertyPath); 196 if (value == null) { 197 throw new FatalBeanException("PropertyPathFactoryBean is not allowed to return null, " + 198 "but property value for path '" + this.propertyPath + "' is null"); 199 } 200 return value; 201 } 202 203 public Class getObjectType() { 204 return this.resultType; 205 } 206 207 213 public boolean isSingleton() { 214 return false; 215 } 216 217 } 218 | Popular Tags |