zoukankan      html  css  js  c++  java
  • BeanDefinition源码解析

    我们知道BeanDefintion定义了Bean在IoC容器内的基本数据结构。在学习IoC之前先了解BeanDefition对我们理解IoC容器是有帮助的。

    首先BeanDefinition是一个接口,继承了AttributeAccessor和BeanMetadataElement。

    我们主要来学习下他的一个实现类,AbstractBeanDefinition。

    这虽然是一个抽象类,但是提供了BeanDefinition接口的全部实现。是一个基本的框架。其子类有RootBeanDefinition(表示父级),ChildBeanDefinition(表示子级)和GenericBeanDefinition(一般的BeanDefinition实现)。

    来看代码:

      1 /*
      2  * Copyright 2002-2016 the original author or authors.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package org.springframework.beans.factory.support;
     18 
     19 import java.lang.reflect.Constructor;
     20 import java.util.Arrays;
     21 import java.util.LinkedHashMap;
     22 import java.util.LinkedHashSet;
     23 import java.util.Map;
     24 import java.util.Set;
     25 
     26 import org.springframework.beans.BeanMetadataAttributeAccessor;
     27 import org.springframework.beans.MutablePropertyValues;
     28 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
     29 import org.springframework.beans.factory.config.BeanDefinition;
     30 import org.springframework.beans.factory.config.ConstructorArgumentValues;
     31 import org.springframework.core.io.DescriptiveResource;
     32 import org.springframework.core.io.Resource;
     33 import org.springframework.util.Assert;
     34 import org.springframework.util.ClassUtils;
     35 import org.springframework.util.ObjectUtils;
     36 import org.springframework.util.StringUtils;
     37 
     38 /**
     39  * Base class for concrete, full-fledged {@link BeanDefinition} classes,
     40  * factoring out common properties of {@link GenericBeanDefinition},
     41  * {@link RootBeanDefinition}, and {@link ChildBeanDefinition}.
     42  *
     43  * <p>The autowire constants match the ones defined in the
     44  * {@link org.springframework.beans.factory.config.AutowireCapableBeanFactory}
     45  * interface.
     46  *
     47  * @author Rod Johnson
     48  * @author Juergen Hoeller
     49  * @author Rob Harrop
     50  * @author Mark Fisher
     51  * @see GenericBeanDefinition
     52  * @see RootBeanDefinition
     53  * @see ChildBeanDefinition
     54  */
     55 @SuppressWarnings("serial")
     56 public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor
     57         implements BeanDefinition, Cloneable {
     58 
     59     
     60     public static final String SCOPE_DEFAULT = "";
     61 
     62     /***************************自动注入时的策略***************************/
     63 
     64     public static final int AUTOWIRE_NO = AutowireCapableBeanFactory.AUTOWIRE_NO;
     65 
     66     public static final int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
     67 
     68     public static final int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
     69 
     70     public static final int AUTOWIRE_CONSTRUCTOR = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;
     71 
     72     @Deprecated
     73     public static final int AUTOWIRE_AUTODETECT = AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT;
     74 
     75     /***************************依赖检查的策略***************************/
     76     //不做检查
     77     public static final int DEPENDENCY_CHECK_NONE = 0;
     78 
     79     //检查除简单类型属性以及集合类型属性外的引用类型属性
     80     public static final int DEPENDENCY_CHECK_OBJECTS = 1;
     81 
     82     //检查简单类型属性以及集合类型属性
     83     public static final int DEPENDENCY_CHECK_SIMPLE = 2;
     84 
     85     //全部检查
     86     public static final int DEPENDENCY_CHECK_ALL = 3;
     87 
     88     public static final String INFER_METHOD = "(inferred)";
     89 
     90     //表示这个Bean的类,可能是字符串,比如"com.insaneXs.TestA",也可能是class对象
     91     private volatile Object beanClass;
     92 
     93     //表示这个bean的范围
     94     private String scope = SCOPE_DEFAULT;
     95 
     96     //是否是抽象的,在定义bean的时候,由abstract属性设置,抽象的BeanDefition在getBean时会抛出异常
     97     private boolean abstractFlag = false;
     98 
     99     //是否延迟加载
    100     private boolean lazyInit = false;
    101 
    102     private int autowireMode = AUTOWIRE_NO;
    103 
    104     private int dependencyCheck = DEPENDENCY_CHECK_NONE;
    105 
    106     //依赖关系,在定义bean的时候,由depend-on属性设置,被依赖的bean在该bean之前被容器初始化
    107     private String[] dependsOn;
    108 
    109     private boolean autowireCandidate = true;
    110 
    111     private boolean primary = false;
    112 
    113     private final Map<String, AutowireCandidateQualifier> qualifiers =
    114             new LinkedHashMap<String, AutowireCandidateQualifier>(0);
    115 
    116     //是都允许访问非共有的属性
    117     private boolean nonPublicAccessAllowed = true;
    118 
    119     //调用构造函数时,是否采用宽松匹配
    120     private boolean lenientConstructorResolution = true;
    121 
    122     //工厂类Bean的名字
    123     private String factoryBeanName;
    124 
    125     //工厂方法的名字
    126     private String factoryMethodName;
    127 
    128     //构造函数参数的封装
    129     private ConstructorArgumentValues constructorArgumentValues;
    130 
    131     //在定义bean时,property标签设置的属性
    132     private MutablePropertyValues propertyValues;
    133 
    134     //跟Bean定义时的look-up有关
    135     private MethodOverrides methodOverrides = new MethodOverrides();
    136 
    137     //指定init方法名称,会在初始化的时候被调用
    138     private String initMethodName;
    139 
    140     //指定destroy方法名称,会在被销毁时调用
    141     private String destroyMethodName;
    142 
    143 
    144     private boolean enforceInitMethod = true;
    145 
    146     private boolean enforceDestroyMethod = true;
    147 
    148     //表示该Bean是否是由程序生成的
    149     private boolean synthetic = false;
    150 
    151     private int role = BeanDefinition.ROLE_APPLICATION;
    152 
    153     private String description;
    154 
    155     private Resource resource;
    156 
    157 
    158     protected AbstractBeanDefinition() {
    159         this(null, null);
    160     }
    161 
    162     protected AbstractBeanDefinition(ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
    163         setConstructorArgumentValues(cargs);
    164         setPropertyValues(pvs);
    165     }
    166 
    167     //深拷贝
    168     protected AbstractBeanDefinition(BeanDefinition original) {
    169         setParentName(original.getParentName());
    170         setBeanClassName(original.getBeanClassName());
    171         setScope(original.getScope());
    172         setAbstract(original.isAbstract());
    173         setLazyInit(original.isLazyInit());
    174         setFactoryBeanName(original.getFactoryBeanName());
    175         setFactoryMethodName(original.getFactoryMethodName());
    176         setConstructorArgumentValues(new ConstructorArgumentValues(original.getConstructorArgumentValues()));
    177         setPropertyValues(new MutablePropertyValues(original.getPropertyValues()));
    178         setRole(original.getRole());
    179         setSource(original.getSource());
    180         copyAttributesFrom(original);
    181 
    182         if (original instanceof AbstractBeanDefinition) {
    183             AbstractBeanDefinition originalAbd = (AbstractBeanDefinition) original;
    184             if (originalAbd.hasBeanClass()) {
    185                 setBeanClass(originalAbd.getBeanClass());
    186             }
    187             setAutowireMode(originalAbd.getAutowireMode());
    188             setDependencyCheck(originalAbd.getDependencyCheck());
    189             setDependsOn(originalAbd.getDependsOn());
    190             setAutowireCandidate(originalAbd.isAutowireCandidate());
    191             setPrimary(originalAbd.isPrimary());
    192             copyQualifiersFrom(originalAbd);
    193             setNonPublicAccessAllowed(originalAbd.isNonPublicAccessAllowed());
    194             setLenientConstructorResolution(originalAbd.isLenientConstructorResolution());
    195             setMethodOverrides(new MethodOverrides(originalAbd.getMethodOverrides()));
    196             setInitMethodName(originalAbd.getInitMethodName());
    197             setEnforceInitMethod(originalAbd.isEnforceInitMethod());
    198             setDestroyMethodName(originalAbd.getDestroyMethodName());
    199             setEnforceDestroyMethod(originalAbd.isEnforceDestroyMethod());
    200             setSynthetic(originalAbd.isSynthetic());
    201             setResource(originalAbd.getResource());
    202         }
    203         else {
    204             setResourceDescription(original.getResourceDescription());
    205         }
    206     }
    207 
    208 
    209     //由其他BeanDefinition的值来覆盖自身的值
    210     public void overrideFrom(BeanDefinition other) {
    211         if (StringUtils.hasLength(other.getBeanClassName())) {
    212             setBeanClassName(other.getBeanClassName());
    213         }
    214         if (StringUtils.hasLength(other.getScope())) {
    215             setScope(other.getScope());
    216         }
    217         setAbstract(other.isAbstract());
    218         setLazyInit(other.isLazyInit());
    219         if (StringUtils.hasLength(other.getFactoryBeanName())) {
    220             setFactoryBeanName(other.getFactoryBeanName());
    221         }
    222         if (StringUtils.hasLength(other.getFactoryMethodName())) {
    223             setFactoryMethodName(other.getFactoryMethodName());
    224         }
    225         getConstructorArgumentValues().addArgumentValues(other.getConstructorArgumentValues());
    226         getPropertyValues().addPropertyValues(other.getPropertyValues());
    227         setRole(other.getRole());
    228         setSource(other.getSource());
    229         copyAttributesFrom(other);
    230 
    231         if (other instanceof AbstractBeanDefinition) {
    232             AbstractBeanDefinition otherAbd = (AbstractBeanDefinition) other;
    233             if (otherAbd.hasBeanClass()) {
    234                 setBeanClass(otherAbd.getBeanClass());
    235             }
    236             setAutowireMode(otherAbd.getAutowireMode());
    237             setDependencyCheck(otherAbd.getDependencyCheck());
    238             setDependsOn(otherAbd.getDependsOn());
    239             setAutowireCandidate(otherAbd.isAutowireCandidate());
    240             setPrimary(otherAbd.isPrimary());
    241             copyQualifiersFrom(otherAbd);
    242             setNonPublicAccessAllowed(otherAbd.isNonPublicAccessAllowed());
    243             setLenientConstructorResolution(otherAbd.isLenientConstructorResolution());
    244             getMethodOverrides().addOverrides(otherAbd.getMethodOverrides());
    245             if (StringUtils.hasLength(otherAbd.getInitMethodName())) {
    246                 setInitMethodName(otherAbd.getInitMethodName());
    247                 setEnforceInitMethod(otherAbd.isEnforceInitMethod());
    248             }
    249             if (otherAbd.getDestroyMethodName() != null) {
    250                 setDestroyMethodName(otherAbd.getDestroyMethodName());
    251                 setEnforceDestroyMethod(otherAbd.isEnforceDestroyMethod());
    252             }
    253             setSynthetic(otherAbd.isSynthetic());
    254             setResource(otherAbd.getResource());
    255         }
    256         else {
    257             setResourceDescription(other.getResourceDescription());
    258         }
    259     }
    260 
    261     //根据默认设置来修改自身
    262     public void applyDefaults(BeanDefinitionDefaults defaults) {
    263         setLazyInit(defaults.isLazyInit());
    264         setAutowireMode(defaults.getAutowireMode());
    265         setDependencyCheck(defaults.getDependencyCheck());
    266         setInitMethodName(defaults.getInitMethodName());
    267         setEnforceInitMethod(false);
    268         setDestroyMethodName(defaults.getDestroyMethodName());
    269         setEnforceDestroyMethod(false);
    270     }
    271 
    272 
    273     //设置bean的className
    274     @Override
    275     public void setBeanClassName(String beanClassName) {
    276         this.beanClass = beanClassName;
    277     }
    278 
    279     //获取这个Bean的className
    280     @Override
    281     public String getBeanClassName() {
    282         Object beanClassObject = this.beanClass;
    283         //如果已经被解析成class,则通过class.getName方法返回class的名称
    284         if (beanClassObject instanceof Class) {
    285             return ((Class<?>) beanClassObject).getName();
    286         }
    287         else {
    288             return (String) beanClassObject;
    289         }
    290     }
    291 
    292     //设置这个bean的class
    293     public void setBeanClass(Class<?> beanClass) {
    294         this.beanClass = beanClass;
    295     }
    296 
    297     //返回这个bean的class,如果beanClass为空,未被解析,则会抛出异常
    298     public Class<?> getBeanClass() throws IllegalStateException {
    299         Object beanClassObject = this.beanClass;
    300         if (beanClassObject == null) {
    301             throw new IllegalStateException("No bean class specified on bean definition");
    302         }
    303         if (!(beanClassObject instanceof Class)) {
    304             throw new IllegalStateException(
    305                     "Bean class name [" + beanClassObject + "] has not been resolved into an actual Class");
    306         }
    307         return (Class<?>) beanClassObject;
    308     }
    309 
    310     //beanClass是否被解析成class
    311     public boolean hasBeanClass() {
    312         return (this.beanClass instanceof Class);
    313     }
    314 
    315     //解析beanClass
    316     public Class<?> resolveBeanClass(ClassLoader classLoader) throws ClassNotFoundException {
    317         String className = getBeanClassName();
    318         if (className == null) {
    319             return null;
    320         }
    321         Class<?> resolvedClass = ClassUtils.forName(className, classLoader);
    322         this.beanClass = resolvedClass;
    323         return resolvedClass;
    324     }
    325 
    326     //设置bean的范围
    327     @Override
    328     public void setScope(String scope) {
    329         this.scope = scope;
    330     }
    331 
    332     //获取bean的返回
    333     @Override
    334     public String getScope() {
    335         return this.scope;
    336     }
    337 
    338     //是否是单例
    339     @Override
    340     public boolean isSingleton() {
    341         return SCOPE_SINGLETON.equals(scope) || SCOPE_DEFAULT.equals(scope);
    342     }
    343 
    344     //是否是prototype
    345     @Override
    346     public boolean isPrototype() {
    347         return SCOPE_PROTOTYPE.equals(scope);
    348     }
    349 
    350     
    351     public void setAbstract(boolean abstractFlag) {
    352         this.abstractFlag = abstractFlag;
    353     }
    354 
    355 
    356     @Override
    357     public boolean isAbstract() {
    358         return this.abstractFlag;
    359     }
    360 
    361     @Override
    362     public void setLazyInit(boolean lazyInit) {
    363         this.lazyInit = lazyInit;
    364     }
    365 
    366     @Override
    367     public boolean isLazyInit() {
    368         return this.lazyInit;
    369     }
    370 
    371     
    372     public void setAutowireMode(int autowireMode) {
    373         this.autowireMode = autowireMode;
    374     }
    375 
    376     
    377     public int getAutowireMode() {
    378         return this.autowireMode;
    379     }
    380 
    381     //获取自动注入模式
    382     public int getResolvedAutowireMode() {
    383         //如果是自动检测,则根据构造函数来判断
    384         if (this.autowireMode == AUTOWIRE_AUTODETECT) {
    385             // Work out whether to apply setter autowiring or constructor autowiring.
    386             // If it has a no-arg constructor it's deemed to be setter autowiring,
    387             // otherwise we'll try constructor autowiring.
    388             Constructor<?>[] constructors = getBeanClass().getConstructors();
    389             for (Constructor<?> constructor : constructors) {
    390                 if (constructor.getParameterTypes().length == 0) {
    391                     return AUTOWIRE_BY_TYPE;
    392                 }
    393             }
    394             return AUTOWIRE_CONSTRUCTOR;
    395         }
    396         else {
    397             return this.autowireMode;
    398         }
    399     }
    400 
    401     
    402     public void setDependencyCheck(int dependencyCheck) {
    403         this.dependencyCheck = dependencyCheck;
    404     }
    405 
    406     
    407     public int getDependencyCheck() {
    408         return this.dependencyCheck;
    409     }
    410 
    411     
    412     @Override
    413     public void setDependsOn(String... dependsOn) {
    414         this.dependsOn = dependsOn;
    415     }
    416 
    417     
    418     @Override
    419     public String[] getDependsOn() {
    420         return this.dependsOn;
    421     }
    422 
    423     
    424     @Override
    425     public void setAutowireCandidate(boolean autowireCandidate) {
    426         this.autowireCandidate = autowireCandidate;
    427     }
    428 
    429     
    430     @Override
    431     public boolean isAutowireCandidate() {
    432         return this.autowireCandidate;
    433     }
    434 
    435     
    436     @Override
    437     public void setPrimary(boolean primary) {
    438         this.primary = primary;
    439     }
    440 
    441     
    442     @Override
    443     public boolean isPrimary() {
    444         return this.primary;
    445     }
    446 
    447     
    448     //自动注入时,用来增加其他限制
    449     public void addQualifier(AutowireCandidateQualifier qualifier) {
    450         this.qualifiers.put(qualifier.getTypeName(), qualifier);
    451     }
    452 
    453     
    454     public boolean hasQualifier(String typeName) {
    455         return this.qualifiers.keySet().contains(typeName);
    456     }
    457 
    458     
    459     public AutowireCandidateQualifier getQualifier(String typeName) {
    460         return this.qualifiers.get(typeName);
    461     }
    462 
    463     
    464     public Set<AutowireCandidateQualifier> getQualifiers() {
    465         return new LinkedHashSet<AutowireCandidateQualifier>(this.qualifiers.values());
    466     }
    467 
    468     
    469     public void copyQualifiersFrom(AbstractBeanDefinition source) {
    470         Assert.notNull(source, "Source must not be null");
    471         this.qualifiers.putAll(source.qualifiers);
    472     }
    473 
    474     
    475 
    476     public void setNonPublicAccessAllowed(boolean nonPublicAccessAllowed) {
    477         this.nonPublicAccessAllowed = nonPublicAccessAllowed;
    478     }
    479 
    480     
    481     public boolean isNonPublicAccessAllowed() {
    482         return this.nonPublicAccessAllowed;
    483     }
    484 
    485     
    486     //构造器是否宽松匹配
    487     public void setLenientConstructorResolution(boolean lenientConstructorResolution) {
    488         this.lenientConstructorResolution = lenientConstructorResolution;
    489     }
    490 
    491     
    492     public boolean isLenientConstructorResolution() {
    493         return this.lenientConstructorResolution;
    494     }
    495 
    496     
    497     //设置获取工厂Bean的Name
    498     @Override
    499     public void setFactoryBeanName(String factoryBeanName) {
    500         this.factoryBeanName = factoryBeanName;
    501     }
    502 
    503     
    504     @Override
    505     public String getFactoryBeanName() {
    506         return this.factoryBeanName;
    507     }
    508 
    509     //设置工厂方法,通常用来实现单例模式
    510     @Override
    511     public void setFactoryMethodName(String factoryMethodName) {
    512         this.factoryMethodName = factoryMethodName;
    513     }
    514 
    515     
    516     @Override
    517     public String getFactoryMethodName() {
    518         return this.factoryMethodName;
    519     }
    520 
    521     //设置构造函数参数
    522     public void setConstructorArgumentValues(ConstructorArgumentValues constructorArgumentValues) {
    523         this.constructorArgumentValues =
    524                 (constructorArgumentValues != null ? constructorArgumentValues : new ConstructorArgumentValues());
    525     }
    526 
    527     
    528     @Override
    529     public ConstructorArgumentValues getConstructorArgumentValues() {
    530         return this.constructorArgumentValues;
    531     }
    532 
    533     
    534     public boolean hasConstructorArgumentValues() {
    535         return !this.constructorArgumentValues.isEmpty();
    536     }
    537 
    538     //设置属性
    539     public void setPropertyValues(MutablePropertyValues propertyValues) {
    540         this.propertyValues = (propertyValues != null ? propertyValues : new MutablePropertyValues());
    541     }
    542 
    543     
    544     @Override
    545     public MutablePropertyValues getPropertyValues() {
    546         return this.propertyValues;
    547     }
    548 
    549     //方法注入
    550     public void setMethodOverrides(MethodOverrides methodOverrides) {
    551         this.methodOverrides = (methodOverrides != null ? methodOverrides : new MethodOverrides());
    552     }
    553 
    554     
    555     public MethodOverrides getMethodOverrides() {
    556         return this.methodOverrides;
    557     }
    558 
    559     //设置init方法
    560     public void setInitMethodName(String initMethodName) {
    561         this.initMethodName = initMethodName;
    562     }
    563 
    564     
    565     public String getInitMethodName() {
    566         return this.initMethodName;
    567     }
    568 
    569     
    570     public void setEnforceInitMethod(boolean enforceInitMethod) {
    571         this.enforceInitMethod = enforceInitMethod;
    572     }
    573 
    574     
    575     public boolean isEnforceInitMethod() {
    576         return this.enforceInitMethod;
    577     }
    578 
    579     
    580     //设置destory方法
    581     public void setDestroyMethodName(String destroyMethodName) {
    582         this.destroyMethodName = destroyMethodName;
    583     }
    584 
    585     
    586     public String getDestroyMethodName() {
    587         return this.destroyMethodName;
    588     }
    589 
    590     
    591     public void setEnforceDestroyMethod(boolean enforceDestroyMethod) {
    592         this.enforceDestroyMethod = enforceDestroyMethod;
    593     }
    594 
    595     
    596     public boolean isEnforceDestroyMethod() {
    597         return this.enforceDestroyMethod;
    598     }
    599 
    600     
    601     //是否由代码生成
    602     public void setSynthetic(boolean synthetic) {
    603         this.synthetic = synthetic;
    604     }
    605 
    606     
    607     public boolean isSynthetic() {
    608         return this.synthetic;
    609     }
    610 
    611     
    612     public void setRole(int role) {
    613         this.role = role;
    614     }
    615 
    616     
    617     @Override
    618     public int getRole() {
    619         return this.role;
    620     }
    621 
    622     
    623     public void setDescription(String description) {
    624         this.description = description;
    625     }
    626 
    627     
    628     @Override
    629     public String getDescription() {
    630         return this.description;
    631     }
    632 
    633     
    634     public void setResource(Resource resource) {
    635         this.resource = resource;
    636     }
    637 
    638     
    639     public Resource getResource() {
    640         return this.resource;
    641     }
    642 
    643     
    644     public void setResourceDescription(String resourceDescription) {
    645         this.resource = new DescriptiveResource(resourceDescription);
    646     }
    647 
    648     
    649     @Override
    650     public String getResourceDescription() {
    651         return (this.resource != null ? this.resource.getDescription() : null);
    652     }
    653 
    654     public void setOriginatingBeanDefinition(BeanDefinition originatingBd) {
    655         this.resource = new BeanDefinitionResource(originatingBd);
    656     }
    657 
    658     
    659     @Override
    660     public BeanDefinition getOriginatingBeanDefinition() {
    661         return (this.resource instanceof BeanDefinitionResource ?
    662                 ((BeanDefinitionResource) this.resource).getBeanDefinition() : null);
    663     }
    664 
    665     //校验
    666     public void validate() throws BeanDefinitionValidationException {
    667         //指定了工厂方法的bean,不能Y由methodOverriders。
    668         //因为工厂方法已经指定了bean实例化的方法,CGLib在这种情况下无效
    669         if (!getMethodOverrides().isEmpty() && getFactoryMethodName() != null) {
    670             throw new BeanDefinitionValidationException(
    671                     "Cannot combine static factory method with method overrides: " +
    672                     "the static factory method must create the instance");
    673         }
    674 
    675         if (hasBeanClass()) {
    676             prepareMethodOverrides();
    677         }
    678     }
    679 
    680     
    681     public void prepareMethodOverrides() throws BeanDefinitionValidationException {
    682         // Check that lookup methods exists.
    683         MethodOverrides methodOverrides = getMethodOverrides();
    684         if (!methodOverrides.isEmpty()) {
    685             Set<MethodOverride> overrides = methodOverrides.getOverrides();
    686             synchronized (overrides) {
    687                 for (MethodOverride mo : overrides) {
    688                     prepareMethodOverride(mo);
    689                 }
    690             }
    691         }
    692     }
    693 
    694     //确认这个重写的方法是否有被重载
    695     protected void prepareMethodOverride(MethodOverride mo) throws BeanDefinitionValidationException {
    696         int count = ClassUtils.getMethodCountForName(getBeanClass(), mo.getMethodName());
    697         if (count == 0) {
    698             throw new BeanDefinitionValidationException(
    699                     "Invalid method override: no method with name '" + mo.getMethodName() +
    700                     "' on class [" + getBeanClassName() + "]");
    701         }
    702         else if (count == 1) {
    703             //重载的值默认为true,
    704             //API上说设置为false可以提高运行性能
    705             mo.setOverloaded(false);
    706         }
    707     }
    708 
    709 
    710     
    711     @Override
    712     public Object clone() {
    713         return cloneBeanDefinition();
    714     }
    715 
    716     
    717     public abstract AbstractBeanDefinition cloneBeanDefinition();
    718 
    719     @Override
    720     public boolean equals(Object other) {
    721         if (this == other) {
    722             return true;
    723         }
    724         if (!(other instanceof AbstractBeanDefinition)) {
    725             return false;
    726         }
    727 
    728         AbstractBeanDefinition that = (AbstractBeanDefinition) other;
    729 
    730         if (!ObjectUtils.nullSafeEquals(getBeanClassName(), that.getBeanClassName())) return false;
    731         if (!ObjectUtils.nullSafeEquals(this.scope, that.scope)) return false;
    732         if (this.abstractFlag != that.abstractFlag) return false;
    733         if (this.lazyInit != that.lazyInit) return false;
    734 
    735         if (this.autowireMode != that.autowireMode) return false;
    736         if (this.dependencyCheck != that.dependencyCheck) return false;
    737         if (!Arrays.equals(this.dependsOn, that.dependsOn)) return false;
    738         if (this.autowireCandidate != that.autowireCandidate) return false;
    739         if (!ObjectUtils.nullSafeEquals(this.qualifiers, that.qualifiers)) return false;
    740         if (this.primary != that.primary) return false;
    741 
    742         if (this.nonPublicAccessAllowed != that.nonPublicAccessAllowed) return false;
    743         if (this.lenientConstructorResolution != that.lenientConstructorResolution) return false;
    744         if (!ObjectUtils.nullSafeEquals(this.constructorArgumentValues, that.constructorArgumentValues)) return false;
    745         if (!ObjectUtils.nullSafeEquals(this.propertyValues, that.propertyValues)) return false;
    746         if (!ObjectUtils.nullSafeEquals(this.methodOverrides, that.methodOverrides)) return false;
    747 
    748         if (!ObjectUtils.nullSafeEquals(this.factoryBeanName, that.factoryBeanName)) return false;
    749         if (!ObjectUtils.nullSafeEquals(this.factoryMethodName, that.factoryMethodName)) return false;
    750         if (!ObjectUtils.nullSafeEquals(this.initMethodName, that.initMethodName)) return false;
    751         if (this.enforceInitMethod != that.enforceInitMethod) return false;
    752         if (!ObjectUtils.nullSafeEquals(this.destroyMethodName, that.destroyMethodName)) return false;
    753         if (this.enforceDestroyMethod != that.enforceDestroyMethod) return false;
    754 
    755         if (this.synthetic != that.synthetic) return false;
    756         if (this.role != that.role) return false;
    757 
    758         return super.equals(other);
    759     }
    760 
    761     @Override
    762     public int hashCode() {
    763         int hashCode = ObjectUtils.nullSafeHashCode(getBeanClassName());
    764         hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.scope);
    765         hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.constructorArgumentValues);
    766         hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.propertyValues);
    767         hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.factoryBeanName);
    768         hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.factoryMethodName);
    769         hashCode = 29 * hashCode + super.hashCode();
    770         return hashCode;
    771     }
    772 
    773     @Override
    774     public String toString() {
    775         StringBuilder sb = new StringBuilder("class [");
    776         sb.append(getBeanClassName()).append("]");
    777         sb.append("; scope=").append(this.scope);
    778         sb.append("; abstract=").append(this.abstractFlag);
    779         sb.append("; lazyInit=").append(this.lazyInit);
    780         sb.append("; autowireMode=").append(this.autowireMode);
    781         sb.append("; dependencyCheck=").append(this.dependencyCheck);
    782         sb.append("; autowireCandidate=").append(this.autowireCandidate);
    783         sb.append("; primary=").append(this.primary);
    784         sb.append("; factoryBeanName=").append(this.factoryBeanName);
    785         sb.append("; factoryMethodName=").append(this.factoryMethodName);
    786         sb.append("; initMethodName=").append(this.initMethodName);
    787         sb.append("; destroyMethodName=").append(this.destroyMethodName);
    788         if (this.resource != null) {
    789             sb.append("; defined in ").append(this.resource.getDescription());
    790         }
    791         return sb.toString();
    792     }
    793 
    794 }
  • 相关阅读:
    常用DOS命令
    Intellij IDEA debug断点调试技巧与总结详解篇
    IDEA中常用函数的快捷键及自定义方法模板
    System.out.println()的真实含义
    盘点一下Github上开源的编程面试/学习相关的仓库
    IntelliJ IDEA
    JAVA基础——接口(全网最详细教程)
    Css基础
    CSS属性(根据继承性分为两类)
    HTML5新增标签(新增27个标签,废弃16个标签)
  • 原文地址:https://www.cnblogs.com/insaneXs/p/7797291.html
Copyright © 2011-2022 走看看