1 16 17 package org.springframework.beans.factory.support; 18 19 import org.springframework.beans.PropertyValue; 20 import org.springframework.beans.factory.config.RuntimeBeanReference; 21 import org.springframework.util.ObjectUtils; 22 23 34 public class BeanDefinitionBuilder { 35 36 40 public static BeanDefinitionBuilder rootBeanDefinition(Class beanClass) { 41 return rootBeanDefinition(beanClass, null); 42 } 43 44 49 public static BeanDefinitionBuilder rootBeanDefinition(Class beanClass, String factoryMethod) { 50 BeanDefinitionBuilder builder = new BeanDefinitionBuilder(); 51 builder.beanDefinition = new RootBeanDefinition(); 52 builder.beanDefinition.setBeanClass(beanClass); 53 builder.beanDefinition.setFactoryMethodName(factoryMethod); 54 return builder; 55 } 56 57 61 public static BeanDefinitionBuilder childBeanDefinition(String parentBeanName) { 62 BeanDefinitionBuilder builder = new BeanDefinitionBuilder(); 63 builder.beanDefinition = new ChildBeanDefinition(parentBeanName); 64 return builder; 65 } 66 67 68 71 private AbstractBeanDefinition beanDefinition; 72 73 76 private int constructorArgIndex; 77 78 79 82 private BeanDefinitionBuilder() { 83 } 84 85 89 public AbstractBeanDefinition getRawBeanDefinition() { 90 return this.beanDefinition; 91 } 92 93 96 public AbstractBeanDefinition getBeanDefinition() { 97 this.beanDefinition.validate(); 98 return this.beanDefinition; 99 } 100 101 102 105 public BeanDefinitionBuilder addPropertyValue(String name, Object value) { 106 this.beanDefinition.getPropertyValues().addPropertyValue(new PropertyValue(name, value)); 107 return this; 108 } 109 110 115 public BeanDefinitionBuilder addPropertyReference(String name, String beanName) { 116 return addPropertyValue(name, new RuntimeBeanReference(beanName)); 117 } 118 119 123 public BeanDefinitionBuilder addConstructorArg(Object value) { 124 this.beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(constructorArgIndex++, value); 125 return this; 126 } 127 128 132 public BeanDefinitionBuilder addConstructorArgReference(String beanName) { 133 return addConstructorArg(new RuntimeBeanReference(beanName)); 134 } 135 136 139 public BeanDefinitionBuilder setFactoryMethod(String factoryMethod) { 140 this.beanDefinition.setFactoryMethodName(factoryMethod); 141 return this; 142 } 143 144 147 public BeanDefinitionBuilder setFactoryBean(String factoryBean, String factoryMethod) { 148 this.beanDefinition.setFactoryBeanName(factoryBean); 149 this.beanDefinition.setFactoryMethodName(factoryMethod); 150 return this; 151 } 152 153 158 public BeanDefinitionBuilder setScope(String scope) { 159 this.beanDefinition.setScope(scope); 160 return this; 161 } 162 163 168 public BeanDefinitionBuilder setSingleton(boolean singleton) { 169 this.beanDefinition.setSingleton(singleton); 170 return this; 171 } 172 173 176 public BeanDefinitionBuilder setAbstract(boolean flag) { 177 this.beanDefinition.setAbstract(flag); 178 return this; 179 } 180 181 184 public BeanDefinitionBuilder setLazyInit(boolean lazy) { 185 this.beanDefinition.setLazyInit(lazy); 186 return this; 187 } 188 189 192 public BeanDefinitionBuilder setAutowireMode(int autowireMode) { 193 beanDefinition.setAutowireMode(autowireMode); 194 return this; 195 } 196 197 200 public BeanDefinitionBuilder setDependencyCheck(int dependencyCheck) { 201 beanDefinition.setDependencyCheck(dependencyCheck); 202 return this; 203 } 204 205 208 public BeanDefinitionBuilder setDestroyMethodName(String methodName) { 209 beanDefinition.setDestroyMethodName(methodName); 210 return this; 211 } 212 213 216 public BeanDefinitionBuilder setInitMethodName(String methodName) { 217 beanDefinition.setInitMethodName(methodName); 218 return this; 219 } 220 221 224 public BeanDefinitionBuilder setResourceDescription(String resourceDescription) { 225 this.beanDefinition.setResourceDescription(resourceDescription); 226 return this; 227 } 228 229 233 public BeanDefinitionBuilder addDependsOn(String beanName) { 234 if (this.beanDefinition.getDependsOn() == null) { 235 this.beanDefinition.setDependsOn(new String [] { beanName }); 236 } 237 else { 238 String [] added = (String []) ObjectUtils.addObjectToArray(beanDefinition.getDependsOn(), beanName); 239 this.beanDefinition.setDependsOn(added); 240 } 241 return this; 242 } 243 244 248 public BeanDefinitionBuilder setSource(Object source) { 249 this.beanDefinition.setSource(source); 250 return this; 251 } 252 253 257 public BeanDefinitionBuilder setRole(int role) { 258 this.beanDefinition.setRole(role); 259 return this; 260 } 261 262 } 263 | Popular Tags |