Spring框架之基础类—AbstractBeanDefinition抽象类
来源:互联网 发布:js表格中输入数据 编辑:程序博客网 时间:2024/05/22 14:31
一、AbstractBeanDefinition简介
AbstractBeanDefinition成熟的BeanDefinition抽象类,是具体实现类的基类。
AbstractBeanDefinition主要是分离出GenericBeanDefinition、RootBeanDefinition和ChildBeanDefinition的公共属性。
其中,AbstractBeanDefinition内部定义的自动装配常亮是和AutowireCapableBeanFactory定义的自动装配常亮一致。
二、AbstractBeanDefinition源码详解
@SuppressWarnings("serial")public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor implements BeanDefinition, Cloneable { /** * 自动装配常亮 */ private int autowireMode = AUTOWIRE_NO; public static final int AUTOWIRE_NO = AutowireCapableBeanFactory.AUTOWIRE_NO; public static final int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME; public static final int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE; public static final int AUTOWIRE_CONSTRUCTOR = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR; public static final int AUTOWIRE_AUTODETECT = AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT; /** * 依赖检查常亮 */ private int dependencyCheck = DEPENDENCY_CHECK_NONE; public static final int DEPENDENCY_CHECK_NONE = 0; public static final int DEPENDENCY_CHECK_OBJECTS = 1; public static final int DEPENDENCY_CHECK_SIMPLE = 2; public static final int DEPENDENCY_CHECK_ALL = 3; /** * 基础配置变量 */ private volatile Object beanClass; // Bean对应的实现类 private String scope = SCOPE_DEFAULT; // Bean的作用域 public static final String SCOPE_DEFAULT = ""; private boolean lazyInit = false; // 是否延迟初始化 private boolean abstractFlag = false; // 是否为抽象Bean private String[] dependsOn; // Bean所依赖的Bean的名称列表 private boolean autowireCandidate = true; // 是否开启自动装配候选者 private boolean primary = false; // 是否为自动装配的主候选者 private String factoryBeanName; // 工厂Bean名称 private String factoryMethodName; // 工厂方法 private String initMethodName; // init-method配置的方法 private String destroyMethodName; // destroy-method配置的方法 private boolean enforceInitMethod = true; // 是否强制初始化方法 private boolean enforceDestroyMethod = true; // 是否强制销毁方法 private String description; // Bean的描述 private ConstructorArgumentValues constructorArgumentValues; // 对应Bean配置的构造器注入参数 private MutablePropertyValues propertyValues; //对应Bean配置的属性注入参数 private int role = BeanDefinition.ROLE_APPLICATION; // Bean的角色 private boolean nonPublicAccessAllowed = true; private boolean lenientConstructorResolution = true; private MethodOverrides methodOverrides = new MethodOverrides(); private boolean synthetic = false; private Resource resource; private final Map<String, AutowireCandidateQualifier> qualifiers = new LinkedHashMap<String, AutowireCandidateQualifier>(0); public static final String INFER_METHOD = "(inferred)"; /** * 构造器 */ protected AbstractBeanDefinition() { this(null, null); } protected AbstractBeanDefinition(ConstructorArgumentValues cargs, MutablePropertyValues pvs) { setConstructorArgumentValues(cargs); setPropertyValues(pvs); } protected AbstractBeanDefinition(BeanDefinition original) { setParentName(original.getParentName()); setBeanClassName(original.getBeanClassName()); setFactoryBeanName(original.getFactoryBeanName()); setFactoryMethodName(original.getFactoryMethodName()); setScope(original.getScope()); setAbstract(original.isAbstract()); setLazyInit(original.isLazyInit()); setRole(original.getRole()); setConstructorArgumentValues(new ConstructorArgumentValues(original.getConstructorArgumentValues())); setPropertyValues(new MutablePropertyValues(original.getPropertyValues())); setSource(original.getSource()); copyAttributesFrom(original); if (original instanceof AbstractBeanDefinition) { AbstractBeanDefinition originalAbd = (AbstractBeanDefinition) original; if (originalAbd.hasBeanClass()) { setBeanClass(originalAbd.getBeanClass()); } setAutowireMode(originalAbd.getAutowireMode()); setDependencyCheck(originalAbd.getDependencyCheck()); setDependsOn(originalAbd.getDependsOn()); setAutowireCandidate(originalAbd.isAutowireCandidate()); copyQualifiersFrom(originalAbd); setPrimary(originalAbd.isPrimary()); setNonPublicAccessAllowed(originalAbd.isNonPublicAccessAllowed()); setLenientConstructorResolution(originalAbd.isLenientConstructorResolution()); setInitMethodName(originalAbd.getInitMethodName()); setEnforceInitMethod(originalAbd.isEnforceInitMethod()); setDestroyMethodName(originalAbd.getDestroyMethodName()); setEnforceDestroyMethod(originalAbd.isEnforceDestroyMethod()); setMethodOverrides(new MethodOverrides(originalAbd.getMethodOverrides())); setSynthetic(originalAbd.isSynthetic()); setResource(originalAbd.getResource()); } else { setResourceDescription(original.getResourceDescription()); } } /** * 设置默认值 */ public void applyDefaults(BeanDefinitionDefaults defaults) { setLazyInit(defaults.isLazyInit()); setAutowireMode(defaults.getAutowireMode()); setDependencyCheck(defaults.getDependencyCheck()); setInitMethodName(defaults.getInitMethodName()); setEnforceInitMethod(false); setDestroyMethodName(defaults.getDestroyMethodName()); setEnforceDestroyMethod(false); } /** * 通过指定的BeanDefinition来覆盖当前设置 */ public void overrideFrom(BeanDefinition other) { if (StringUtils.hasLength(other.getBeanClassName())) { setBeanClassName(other.getBeanClassName()); } if (StringUtils.hasLength(other.getFactoryBeanName())) { setFactoryBeanName(other.getFactoryBeanName()); } if (StringUtils.hasLength(other.getFactoryMethodName())) { setFactoryMethodName(other.getFactoryMethodName()); } if (StringUtils.hasLength(other.getScope())) { setScope(other.getScope()); } setAbstract(other.isAbstract()); setLazyInit(other.isLazyInit()); setRole(other.getRole()); getConstructorArgumentValues().addArgumentValues(other.getConstructorArgumentValues()); getPropertyValues().addPropertyValues(other.getPropertyValues()); setSource(other.getSource()); copyAttributesFrom(other); if (other instanceof AbstractBeanDefinition) { AbstractBeanDefinition otherAbd = (AbstractBeanDefinition) other; if (otherAbd.hasBeanClass()) { setBeanClass(otherAbd.getBeanClass()); } setAutowireCandidate(otherAbd.isAutowireCandidate()); setAutowireMode(otherAbd.getAutowireMode()); copyQualifiersFrom(otherAbd); setPrimary(otherAbd.isPrimary()); setDependencyCheck(otherAbd.getDependencyCheck()); setDependsOn(otherAbd.getDependsOn()); setNonPublicAccessAllowed(otherAbd.isNonPublicAccessAllowed()); setLenientConstructorResolution(otherAbd.isLenientConstructorResolution()); if (StringUtils.hasLength(otherAbd.getInitMethodName())) { setInitMethodName(otherAbd.getInitMethodName()); setEnforceInitMethod(otherAbd.isEnforceInitMethod()); } if (otherAbd.getDestroyMethodName() != null) { setDestroyMethodName(otherAbd.getDestroyMethodName()); setEnforceDestroyMethod(otherAbd.isEnforceDestroyMethod()); } getMethodOverrides().addOverrides(otherAbd.getMethodOverrides()); setSynthetic(otherAbd.isSynthetic()); setResource(otherAbd.getResource()); } else { setResourceDescription(other.getResourceDescription()); } } /** * 验证该BeanDefinition */ public void validate() throws BeanDefinitionValidationException { if (!getMethodOverrides().isEmpty() && getFactoryMethodName() != null) { throw new BeanDefinitionValidationException( "Cannot combine static factory method with method overrides: " + "the static factory method must create the instance"); } if (hasBeanClass()) { prepareMethodOverrides(); } } /** * 验证和准备给定的重写方法 */ public void prepareMethodOverrides() throws BeanDefinitionValidationException { // Check that lookup methods exists. MethodOverrides methodOverrides = getMethodOverrides(); if (!methodOverrides.isEmpty()) { for (MethodOverride mo : methodOverrides.getOverrides()) { prepareMethodOverride(mo); } } } /** * 验证和准备给定的重写方法 */ protected void prepareMethodOverride(MethodOverride mo) throws BeanDefinitionValidationException { int count = ClassUtils.getMethodCountForName(getBeanClass(), mo.getMethodName()); if (count == 0) { throw new BeanDefinitionValidationException( "Invalid method override: no method with name '" + mo.getMethodName() + "' on class [" + getBeanClassName() + "]"); } else if (count == 1) { // Mark override as not overloaded, to avoid the overhead of arg type checking. mo.setOverloaded(false); } } /** * 克隆BeanDefinition抽象方法 */ public abstract AbstractBeanDefinition cloneBeanDefinition(); /** * 获取、设置基础配置属性方法 */ public boolean hasBeanClass() { return (this.beanClass instanceof Class); } public void setBeanClass(Class<?> beanClass) { this.beanClass = beanClass; } public Class<?> getBeanClass() throws IllegalStateException { Object beanClassObject = this.beanClass; if (beanClassObject == null) { throw new IllegalStateException("No bean class specified on bean definition"); } if (!(beanClassObject instanceof Class)) { throw new IllegalStateException( "Bean class name [" + beanClassObject + "] has not been resolved into an actual Class"); } return (Class<?>) beanClassObject; } @Override public void setBeanClassName(String beanClassName) { this.beanClass = beanClassName; } @Override public String getBeanClassName() { Object beanClassObject = this.beanClass; if (beanClassObject instanceof Class) { return ((Class<?>) beanClassObject).getName(); } else { return (String) beanClassObject; } } public Class<?> resolveBeanClass(ClassLoader classLoader) throws ClassNotFoundException { String className = getBeanClassName(); if (className == null) { return null; } Class<?> resolvedClass = ClassUtils.forName(className, classLoader); this.beanClass = resolvedClass; return resolvedClass; } @Override public void setScope(String scope) { this.scope = scope; } @Override public String getScope() { return this.scope; } @Override public boolean isSingleton() { return SCOPE_SINGLETON.equals(scope) || SCOPE_DEFAULT.equals(scope); } @Override public boolean isPrototype() { return SCOPE_PROTOTYPE.equals(scope); } public void setAbstract(boolean abstractFlag) { this.abstractFlag = abstractFlag; } @Override public boolean isAbstract() { return this.abstractFlag; } @Override public void setLazyInit(boolean lazyInit) { this.lazyInit = lazyInit; } @Override public boolean isLazyInit() { return this.lazyInit; } public void setAutowireMode(int autowireMode) { this.autowireMode = autowireMode; } public int getAutowireMode() { return this.autowireMode; } public int getResolvedAutowireMode() { if (this.autowireMode == AUTOWIRE_AUTODETECT) { // Work out whether to apply setter autowiring or constructor autowiring. // If it has a no-arg constructor it's deemed to be setter autowiring, // otherwise we'll try constructor autowiring. Constructor<?>[] constructors = getBeanClass().getConstructors(); for (Constructor<?> constructor : constructors) { if (constructor.getParameterTypes().length == 0) { return AUTOWIRE_BY_TYPE; } } return AUTOWIRE_CONSTRUCTOR; } else { return this.autowireMode; } } public void setDependencyCheck(int dependencyCheck) { this.dependencyCheck = dependencyCheck; } public int getDependencyCheck() { return this.dependencyCheck; } @Override public void setDependsOn(String... dependsOn) { this.dependsOn = dependsOn; } @Override public String[] getDependsOn() { return this.dependsOn; } @Override public void setAutowireCandidate(boolean autowireCandidate) { this.autowireCandidate = autowireCandidate; } @Override public boolean isAutowireCandidate() { return this.autowireCandidate; } @Override public void setPrimary(boolean primary) { this.primary = primary; } @Override public boolean isPrimary() { return this.primary; } public void addQualifier(AutowireCandidateQualifier qualifier) { this.qualifiers.put(qualifier.getTypeName(), qualifier); } public boolean hasQualifier(String typeName) { return this.qualifiers.keySet().contains(typeName); } public AutowireCandidateQualifier getQualifier(String typeName) { return this.qualifiers.get(typeName); } public Set<AutowireCandidateQualifier> getQualifiers() { return new LinkedHashSet<AutowireCandidateQualifier>(this.qualifiers.values()); } public void copyQualifiersFrom(AbstractBeanDefinition source) { Assert.notNull(source, "Source must not be null"); this.qualifiers.putAll(source.qualifiers); } public void setNonPublicAccessAllowed(boolean nonPublicAccessAllowed) { this.nonPublicAccessAllowed = nonPublicAccessAllowed; } public boolean isNonPublicAccessAllowed() { return this.nonPublicAccessAllowed; } public void setLenientConstructorResolution(boolean lenientConstructorResolution) { this.lenientConstructorResolution = lenientConstructorResolution; } public boolean isLenientConstructorResolution() { return this.lenientConstructorResolution; } public void setConstructorArgumentValues(ConstructorArgumentValues constructorArgumentValues) { this.constructorArgumentValues = (constructorArgumentValues != null ? constructorArgumentValues : new ConstructorArgumentValues()); } @Override public ConstructorArgumentValues getConstructorArgumentValues() { return this.constructorArgumentValues; } public boolean hasConstructorArgumentValues() { return !this.constructorArgumentValues.isEmpty(); } public void setPropertyValues(MutablePropertyValues propertyValues) { this.propertyValues = (propertyValues != null ? propertyValues : new MutablePropertyValues()); } @Override public MutablePropertyValues getPropertyValues() { return this.propertyValues; } public void setMethodOverrides(MethodOverrides methodOverrides) { this.methodOverrides = (methodOverrides != null ? methodOverrides : new MethodOverrides()); } public MethodOverrides getMethodOverrides() { return this.methodOverrides; } @Override public void setFactoryBeanName(String factoryBeanName) { this.factoryBeanName = factoryBeanName; } @Override public String getFactoryBeanName() { return this.factoryBeanName; } @Override public void setFactoryMethodName(String factoryMethodName) { this.factoryMethodName = factoryMethodName; } @Override public String getFactoryMethodName() { return this.factoryMethodName; } public void setInitMethodName(String initMethodName) { this.initMethodName = initMethodName; } public String getInitMethodName() { return this.initMethodName; } public void setEnforceInitMethod(boolean enforceInitMethod) { this.enforceInitMethod = enforceInitMethod; } public boolean isEnforceInitMethod() { return this.enforceInitMethod; } public void setDestroyMethodName(String destroyMethodName) { this.destroyMethodName = destroyMethodName; } public String getDestroyMethodName() { return this.destroyMethodName; } public void setEnforceDestroyMethod(boolean enforceDestroyMethod) { this.enforceDestroyMethod = enforceDestroyMethod; } public boolean isEnforceDestroyMethod() { return this.enforceDestroyMethod; } public void setSynthetic(boolean synthetic) { this.synthetic = synthetic; } public boolean isSynthetic() { return this.synthetic; } public void setRole(int role) { this.role = role; } @Override public int getRole() { return this.role; } public void setDescription(String description) { this.description = description; } @Override public String getDescription() { return this.description; } public void setResource(Resource resource) { this.resource = resource; } public Resource getResource() { return this.resource; } public void setResourceDescription(String resourceDescription) { this.resource = new DescriptiveResource(resourceDescription); } @Override public String getResourceDescription() { return (this.resource != null ? this.resource.getDescription() : null); } public void setOriginatingBeanDefinition(BeanDefinition originatingBd) { this.resource = new BeanDefinitionResource(originatingBd); } @Override public BeanDefinition getOriginatingBeanDefinition() { return (this.resource instanceof BeanDefinitionResource ? ((BeanDefinitionResource) this.resource).getBeanDefinition() : null); } @Override public Object clone() { return cloneBeanDefinition(); } @Override public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof AbstractBeanDefinition)) { return false; } AbstractBeanDefinition that = (AbstractBeanDefinition) other; if (!ObjectUtils.nullSafeEquals(getBeanClassName(), that.getBeanClassName())) return false; if (!ObjectUtils.nullSafeEquals(this.scope, that.scope)) return false; if (this.abstractFlag != that.abstractFlag) return false; if (this.lazyInit != that.lazyInit) return false; if (this.autowireMode != that.autowireMode) return false; if (this.dependencyCheck != that.dependencyCheck) return false; if (!Arrays.equals(this.dependsOn, that.dependsOn)) return false; if (this.autowireCandidate != that.autowireCandidate) return false; if (!ObjectUtils.nullSafeEquals(this.qualifiers, that.qualifiers)) return false; if (this.primary != that.primary) return false; if (this.nonPublicAccessAllowed != that.nonPublicAccessAllowed) return false; if (this.lenientConstructorResolution != that.lenientConstructorResolution) return false; if (!ObjectUtils.nullSafeEquals(this.constructorArgumentValues, that.constructorArgumentValues)) return false; if (!ObjectUtils.nullSafeEquals(this.propertyValues, that.propertyValues)) return false; if (!ObjectUtils.nullSafeEquals(this.methodOverrides, that.methodOverrides)) return false; if (!ObjectUtils.nullSafeEquals(this.factoryBeanName, that.factoryBeanName)) return false; if (!ObjectUtils.nullSafeEquals(this.factoryMethodName, that.factoryMethodName)) return false; if (!ObjectUtils.nullSafeEquals(this.initMethodName, that.initMethodName)) return false; if (this.enforceInitMethod != that.enforceInitMethod) return false; if (!ObjectUtils.nullSafeEquals(this.destroyMethodName, that.destroyMethodName)) return false; if (this.enforceDestroyMethod != that.enforceDestroyMethod) return false; if (this.synthetic != that.synthetic) return false; if (this.role != that.role) return false; return super.equals(other); } @Override public int hashCode() { int hashCode = ObjectUtils.nullSafeHashCode(getBeanClassName()); hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.scope); hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.constructorArgumentValues); hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.propertyValues); hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.factoryBeanName); hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.factoryMethodName); hashCode = 29 * hashCode + super.hashCode(); return hashCode; } @Override public String toString() { StringBuilder sb = new StringBuilder("class ["); sb.append(getBeanClassName()).append("]"); sb.append("; scope=").append(this.scope); sb.append("; abstract=").append(this.abstractFlag); sb.append("; lazyInit=").append(this.lazyInit); sb.append("; autowireMode=").append(this.autowireMode); sb.append("; dependencyCheck=").append(this.dependencyCheck); sb.append("; autowireCandidate=").append(this.autowireCandidate); sb.append("; primary=").append(this.primary); sb.append("; factoryBeanName=").append(this.factoryBeanName); sb.append("; factoryMethodName=").append(this.factoryMethodName); sb.append("; initMethodName=").append(this.initMethodName); sb.append("; destroyMethodName=").append(this.destroyMethodName); if (this.resource != null) { sb.append("; defined in ").append(this.resource.getDescription()); } return sb.toString(); }}
阅读全文
0 0
- Spring框架之基础类—AbstractBeanDefinition抽象类
- Spring框架之基础类—AttributeAccessorSupport抽象类
- Spring框架之基础类—BeanMetadataAttribute实现类
- Spring框架之基础类—GenericBeanDefinition实现类
- Spring框架之基础类—ChildBeanDefinition实现类
- Spring框架之基础类—AnnotatedGenericBeanDefinition实现类
- Spring框架之基础类—ScannedGenericBeanDefinition实现类
- Spring框架之基础类—BeanMetadataElement接口
- Spring框架之基础类—AttributeAccessor接口
- Spring框架之基础类—BeanDefinition接口
- Spring框架之基础类—AnnotatedBeanDefinition接口
- Spring框架之基础类—RootBeanDefinition接口
- java基础之----抽象类
- java基础之抽象类
- java基础之抽象类
- java基础之--抽象类
- Java基础之抽象类与抽象方法
- Spring框架基础之注解
- 关于android:configChanges
- 笔记 input 不可编辑的几种方法
- 常见的协议中使用的算法
- mysql中设置主键步长
- 函数:使用函数指针操作函数
- Spring框架之基础类—AbstractBeanDefinition抽象类
- SQL Server中的CLR编程——用.NET为SQL Server编写存储过程和函数
- neon实现图像缩放算法(双线性插值法)
- java之刨根问底拦不住------->
- 优秀的前端工程师任务目标和实践
- Matlab 的reshape函数
- 微信小程序 --- 无法跳转到tab页面问题
- 2017第111届中国文化用品商品交易会(2017上海文化展)会刊(参展商名录)
- HttpURLConnection网络解析+universalimageloader解析图片+XListView刷新、加载+无限轮播