1 16 17 package org.springframework.beans.factory.support; 18 19 import java.lang.reflect.Constructor ; 20 import java.util.Arrays ; 21 import java.util.Iterator ; 22 23 import org.springframework.beans.MutablePropertyValues; 24 import org.springframework.beans.factory.config.AutowireCapableBeanFactory; 25 import org.springframework.beans.factory.config.BeanDefinition; 26 import org.springframework.beans.factory.config.ConstructorArgumentValues; 27 import org.springframework.core.AttributeAccessorSupport; 28 import org.springframework.util.Assert; 29 import org.springframework.util.ClassUtils; 30 import org.springframework.util.ObjectUtils; 31 32 48 public abstract class AbstractBeanDefinition extends AttributeAccessorSupport implements BeanDefinition { 49 50 54 public static final int AUTOWIRE_NO = AutowireCapableBeanFactory.AUTOWIRE_NO; 55 56 60 public static final int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME; 61 62 66 public static final int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE; 67 68 72 public static final int AUTOWIRE_CONSTRUCTOR = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR; 73 74 79 public static final int AUTOWIRE_AUTODETECT = AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT; 80 81 82 86 public static final int DEPENDENCY_CHECK_NONE = 0; 87 88 92 public static final int DEPENDENCY_CHECK_OBJECTS = 1; 93 94 99 public static final int DEPENDENCY_CHECK_SIMPLE = 2; 100 101 106 public static final int DEPENDENCY_CHECK_ALL = 3; 107 108 109 private Object beanClass; 110 111 private String scope = SCOPE_SINGLETON; 112 113 private boolean abstractFlag = false; 114 115 private boolean lazyInit = false; 116 117 private boolean autowireCandidate = true; 118 119 private int autowireMode = AUTOWIRE_NO; 120 121 private int dependencyCheck = DEPENDENCY_CHECK_NONE; 122 123 private String [] dependsOn; 124 125 private ConstructorArgumentValues constructorArgumentValues; 126 127 private MutablePropertyValues propertyValues; 128 129 private MethodOverrides methodOverrides = new MethodOverrides(); 130 131 private String factoryBeanName; 132 133 private String factoryMethodName; 134 135 private String initMethodName; 136 137 private String destroyMethodName; 138 139 private boolean enforceInitMethod = true; 140 141 private boolean enforceDestroyMethod = true; 142 143 private boolean synthetic = false; 144 145 private String resourceDescription; 146 147 private Object source; 148 149 private int role = BeanDefinition.ROLE_APPLICATION; 150 151 152 155 protected AbstractBeanDefinition() { 156 this(null, null); 157 } 158 159 163 protected AbstractBeanDefinition(ConstructorArgumentValues cargs, MutablePropertyValues pvs) { 164 setConstructorArgumentValues(cargs); 165 setPropertyValues(pvs); 166 } 167 168 173 protected AbstractBeanDefinition(AbstractBeanDefinition original) { 174 this.beanClass = original.beanClass; 175 176 setScope(original.getScope()); 177 setAbstract(original.isAbstract()); 178 setLazyInit(original.isLazyInit()); 179 180 setAutowireCandidate(original.isAutowireCandidate()); 181 setAutowireMode(original.getAutowireMode()); 182 setDependencyCheck(original.getDependencyCheck()); 183 setDependsOn(original.getDependsOn()); 184 185 setConstructorArgumentValues(new ConstructorArgumentValues(original.getConstructorArgumentValues())); 186 setPropertyValues(new MutablePropertyValues(original.getPropertyValues())); 187 setMethodOverrides(new MethodOverrides(original.getMethodOverrides())); 188 189 setFactoryBeanName(original.getFactoryBeanName()); 190 setFactoryMethodName(original.getFactoryMethodName()); 191 setInitMethodName(original.getInitMethodName()); 192 setEnforceInitMethod(original.isEnforceInitMethod()); 193 setDestroyMethodName(original.getDestroyMethodName()); 194 setEnforceDestroyMethod(original.isEnforceDestroyMethod()); 195 196 setSynthetic(original.isSynthetic()); 197 setResourceDescription(original.getResourceDescription()); 198 setSource(original.getSource()); 199 setRole(original.getRole()); 200 201 copyAttributesFrom(original); 202 } 203 204 218 public void overrideFrom(AbstractBeanDefinition other) { 219 if (other.beanClass != null) { 220 this.beanClass = other.beanClass; 221 } 222 223 setScope(other.getScope()); 224 setAbstract(other.isAbstract()); 225 setLazyInit(other.isLazyInit()); 226 227 setAutowireCandidate(other.isAutowireCandidate()); 228 setAutowireMode(other.getAutowireMode()); 229 setDependencyCheck(other.getDependencyCheck()); 230 setDependsOn(other.getDependsOn()); 231 232 getConstructorArgumentValues().addArgumentValues(other.getConstructorArgumentValues()); 233 getPropertyValues().addPropertyValues(other.getPropertyValues()); 234 getMethodOverrides().addOverrides(other.getMethodOverrides()); 235 236 if (other.getFactoryBeanName() != null) { 237 setFactoryBeanName(other.getFactoryBeanName()); 238 } 239 if (other.getFactoryMethodName() != null) { 240 setFactoryMethodName(other.getFactoryMethodName()); 241 } 242 if (other.getInitMethodName() != null) { 243 setInitMethodName(other.getInitMethodName()); 244 setEnforceInitMethod(other.isEnforceInitMethod()); 245 } 246 if (other.getDestroyMethodName() != null) { 247 setDestroyMethodName(other.getDestroyMethodName()); 248 setEnforceDestroyMethod(other.isEnforceDestroyMethod()); 249 } 250 251 setSynthetic(other.isSynthetic()); 252 setResourceDescription(other.getResourceDescription()); 253 setSource(other.getSource()); 254 setRole(other.getRole()); 255 256 copyAttributesFrom(other); 257 } 258 259 260 263 public boolean hasBeanClass() { 264 return (this.beanClass instanceof Class ); 265 } 266 267 270 public void setBeanClass(Class beanClass) { 271 this.beanClass = beanClass; 272 } 273 274 279 public Class getBeanClass() throws IllegalStateException { 280 if (this.beanClass == null) { 281 throw new IllegalStateException ("No bean class specified on bean definition"); 282 } 283 if (!(this.beanClass instanceof Class )) { 284 throw new IllegalStateException ( 285 "Bean class name [" + this.beanClass + "] has not been resolved into an actual Class"); 286 } 287 return (Class ) this.beanClass; 288 } 289 290 293 public void setBeanClassName(String beanClassName) { 294 this.beanClass = beanClassName; 295 } 296 297 300 public String getBeanClassName() { 301 if (this.beanClass instanceof Class ) { 302 return ((Class ) this.beanClass).getName(); 303 } 304 else { 305 return (String ) this.beanClass; 306 } 307 } 308 309 317 public Class resolveBeanClass(ClassLoader classLoader) throws ClassNotFoundException { 318 if (this.beanClass == null) { 319 return null; 320 } 321 Class resolvedClass = ClassUtils.forName(getBeanClassName(), classLoader); 322 this.beanClass = resolvedClass; 323 return resolvedClass; 324 } 325 326 327 334 public void setScope(String scope) { 335 Assert.notNull(scope, "Scope must not be null"); 336 this.scope = scope; 337 } 338 339 342 public String getScope() { 343 return this.scope; 344 } 345 346 358 public void setSingleton(boolean singleton) { 359 this.scope = (singleton ? SCOPE_SINGLETON : SCOPE_PROTOTYPE); 360 } 361 362 367 public boolean isSingleton() { 368 return (SCOPE_SINGLETON.equals(this.scope)); 369 } 370 371 376 public boolean isPrototype() { 377 return (SCOPE_PROTOTYPE.equals(this.scope)); 378 } 379 380 386 public void setAbstract(boolean abstractFlag) { 387 this.abstractFlag = abstractFlag; 388 } 389 390 394 public boolean isAbstract() { 395 return this.abstractFlag; 396 } 397 398 403 public void setLazyInit(boolean lazyInit) { 404 this.lazyInit = lazyInit; 405 } 406 407 411 public boolean isLazyInit() { 412 return this.lazyInit; 413 } 414 415 416 420 public void setAutowireCandidate(boolean autowireCandidate) { 421 this.autowireCandidate = autowireCandidate; 422 } 423 424 428 public boolean isAutowireCandidate() { 429 return this.autowireCandidate; 430 } 431 432 444 public void setAutowireMode(int autowireMode) { 445 this.autowireMode = autowireMode; 446 } 447 448 451 public int getAutowireMode() { 452 return this.autowireMode; 453 } 454 455 462 public int getResolvedAutowireMode() { 463 if (this.autowireMode == AUTOWIRE_AUTODETECT) { 464 Constructor [] constructors = getBeanClass().getConstructors(); 468 for (int i = 0; i < constructors.length; i++) { 469 if (constructors[i].getParameterTypes().length == 0) { 470 return AUTOWIRE_BY_TYPE; 471 } 472 } 473 return AUTOWIRE_CONSTRUCTOR; 474 } 475 else { 476 return this.autowireMode; 477 } 478 } 479 480 489 public void setDependencyCheck(int dependencyCheck) { 490 this.dependencyCheck = dependencyCheck; 491 } 492 493 496 public int getDependencyCheck() { 497 return this.dependencyCheck; 498 } 499 500 507 public void setDependsOn(String [] dependsOn) { 508 this.dependsOn = dependsOn; 509 } 510 511 514 public String [] getDependsOn() { 515 return this.dependsOn; 516 } 517 518 519 522 public void setConstructorArgumentValues(ConstructorArgumentValues constructorArgumentValues) { 523 this.constructorArgumentValues = 524 (constructorArgumentValues != null ? constructorArgumentValues : new ConstructorArgumentValues()); 525 } 526 527 530 public ConstructorArgumentValues getConstructorArgumentValues() { 531 return constructorArgumentValues; 532 } 533 534 537 public boolean hasConstructorArgumentValues() { 538 return !this.constructorArgumentValues.isEmpty(); 539 } 540 541 544 public void setPropertyValues(MutablePropertyValues propertyValues) { 545 this.propertyValues = (propertyValues != null ? propertyValues : new MutablePropertyValues()); 546 } 547 548 551 public MutablePropertyValues getPropertyValues() { 552 return this.propertyValues; 553 } 554 555 558 public void setMethodOverrides(MethodOverrides methodOverrides) { 559 this.methodOverrides = (methodOverrides != null ? methodOverrides : new MethodOverrides()); 560 } 561 562 567 public MethodOverrides getMethodOverrides() { 568 return this.methodOverrides; 569 } 570 571 572 575 public void setFactoryBeanName(String factoryBeanName) { 576 this.factoryBeanName = factoryBeanName; 577 } 578 579 582 public String getFactoryBeanName() { 583 return this.factoryBeanName; 584 } 585 586 595 public void setFactoryMethodName(String factoryMethodName) { 596 this.factoryMethodName = factoryMethodName; 597 } 598 599 602 public String getFactoryMethodName() { 603 return this.factoryMethodName; 604 } 605 606 610 public void setInitMethodName(String initMethodName) { 611 this.initMethodName = initMethodName; 612 } 613 614 617 public String getInitMethodName() { 618 return this.initMethodName; 619 } 620 621 626 public void setEnforceInitMethod(boolean enforceInitMethod) { 627 this.enforceInitMethod = enforceInitMethod; 628 } 629 630 634 public boolean isEnforceInitMethod() { 635 return this.enforceInitMethod; 636 } 637 638 642 public void setDestroyMethodName(String destroyMethodName) { 643 this.destroyMethodName = destroyMethodName; 644 } 645 646 649 public String getDestroyMethodName() { 650 return this.destroyMethodName; 651 } 652 653 658 public void setEnforceDestroyMethod(boolean enforceDestroyMethod) { 659 this.enforceDestroyMethod = enforceDestroyMethod; 660 } 661 662 666 public boolean isEnforceDestroyMethod() { 667 return this.enforceDestroyMethod; 668 } 669 670 671 676 public void setSynthetic(boolean synthetic) { 677 this.synthetic = synthetic; 678 } 679 680 684 public boolean isSynthetic() { 685 return this.synthetic; 686 } 687 688 692 public void setResourceDescription(String resourceDescription) { 693 this.resourceDescription = resourceDescription; 694 } 695 696 700 public String getResourceDescription() { 701 return this.resourceDescription; 702 } 703 704 708 public void setSource(Object source) { 709 this.source = source; 710 } 711 712 public Object getSource() { 713 return this.source; 714 } 715 716 719 public void setRole(int role) { 720 this.role = role; 721 } 722 723 726 public int getRole() { 727 return this.role; 728 } 729 730 731 735 public void validate() throws BeanDefinitionValidationException { 736 if (!getMethodOverrides().isEmpty() && getFactoryMethodName() != null) { 737 throw new BeanDefinitionValidationException( 738 "Cannot combine static factory method with method overrides: " + 739 "the static factory method must create the instance"); 740 } 741 742 if (hasBeanClass()) { 743 prepareMethodOverrides(); 744 } 745 } 746 747 752 public void prepareMethodOverrides() throws BeanDefinitionValidationException { 753 for (Iterator it = getMethodOverrides().getOverrides().iterator(); it.hasNext(); ) { 755 MethodOverride mo = (MethodOverride) it.next(); 756 prepareMethodOverride(mo); 757 } 758 } 759 760 767 protected void prepareMethodOverride(MethodOverride mo) throws BeanDefinitionValidationException { 768 int count = ClassUtils.getMethodCountForName(getBeanClass(), mo.getMethodName()); 769 if (count == 0) { 770 throw new BeanDefinitionValidationException( 771 "Invalid method override: no method with name '" + mo.getMethodName() + 772 "' on class [" + getBeanClassName() + "]"); 773 } 774 else if (count == 1) { 775 mo.setOverloaded(false); 777 } 778 } 779 780 781 public boolean equals(Object other) { 782 if (this == other) { 783 return true; 784 } 785 if (!(other instanceof AbstractBeanDefinition)) { 786 return false; 787 } 788 789 AbstractBeanDefinition that = (AbstractBeanDefinition) other; 790 791 if (!ObjectUtils.nullSafeEquals(this.beanClass, that.beanClass)) return false; 792 if (!ObjectUtils.nullSafeEquals(this.scope, that.scope)) return false; 793 if (this.abstractFlag != that.abstractFlag) return false; 794 if (this.lazyInit != that.lazyInit) return false; 795 796 if (this.autowireCandidate != that.autowireCandidate) return false; 797 if (this.autowireMode != that.autowireMode) return false; 798 if (this.dependencyCheck != that.dependencyCheck) return false; 799 if (!Arrays.equals(this.dependsOn, that.dependsOn)) return false; 800 801 if (!ObjectUtils.nullSafeEquals(this.constructorArgumentValues, that.constructorArgumentValues)) return false; 802 if (!ObjectUtils.nullSafeEquals(this.propertyValues, that.propertyValues)) return false; 803 if (!ObjectUtils.nullSafeEquals(this.methodOverrides, that.methodOverrides)) return false; 804 805 if (!ObjectUtils.nullSafeEquals(this.factoryBeanName, that.factoryBeanName)) return false; 806 if (!ObjectUtils.nullSafeEquals(this.factoryMethodName, that.factoryMethodName)) return false; 807 if (!ObjectUtils.nullSafeEquals(this.initMethodName, that.initMethodName)) return false; 808 if (this.enforceInitMethod != that.enforceInitMethod) return false; 809 if (!ObjectUtils.nullSafeEquals(this.destroyMethodName, that.destroyMethodName)) return false; 810 if (this.enforceDestroyMethod != that.enforceDestroyMethod) return false; 811 812 if (!ObjectUtils.nullSafeEquals(this.resourceDescription, that.resourceDescription)) return false; 813 if (!ObjectUtils.nullSafeEquals(this.source, that.source)) return false; 814 if (this.role != that.role) return false; 815 816 return super.equals(other); 817 } 818 819 public int hashCode() { 820 int hashCode = ObjectUtils.nullSafeHashCode(this.beanClass); 821 hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.scope); 822 hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.constructorArgumentValues); 823 hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.propertyValues); 824 hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.factoryBeanName); 825 hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.factoryMethodName); 826 hashCode = 29 * hashCode + super.hashCode(); 827 return hashCode; 828 } 829 830 public String toString() { 831 StringBuffer sb = new StringBuffer ("class ["); 832 sb.append(getBeanClassName()).append("]"); 833 sb.append("; scope=").append(this.scope); 834 sb.append("; abstract=").append(this.abstractFlag); 835 sb.append("; lazyInit=").append(this.lazyInit); 836 sb.append("; autowireCandidate=").append(this.autowireCandidate); 837 sb.append("; autowireMode=").append(this.autowireMode); 838 sb.append("; dependencyCheck=").append(this.dependencyCheck); 839 sb.append("; factoryBeanName=").append(this.factoryBeanName); 840 sb.append("; factoryMethodName=").append(this.factoryMethodName); 841 sb.append("; initMethodName=").append(this.initMethodName); 842 sb.append("; destroyMethodName=").append(this.destroyMethodName); 843 if (this.resourceDescription != null) { 844 sb.append("; defined in ").append(this.resourceDescription); 845 } 846 return sb.toString(); 847 } 848 849 } 850 | Popular Tags |