001/* 002 * Copyright 2002-2019 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.beans.factory.support; 018 019import org.springframework.beans.factory.config.BeanDefinition; 020import org.springframework.lang.Nullable; 021import org.springframework.util.ObjectUtils; 022 023/** 024 * GenericBeanDefinition is a one-stop shop for standard bean definition purposes. 025 * Like any bean definition, it allows for specifying a class plus optionally 026 * constructor argument values and property values. Additionally, deriving from a 027 * parent bean definition can be flexibly configured through the "parentName" property. 028 * 029 * <p>In general, use this {@code GenericBeanDefinition} class for the purpose of 030 * registering user-visible bean definitions (which a post-processor might operate on, 031 * potentially even reconfiguring the parent name). Use {@code RootBeanDefinition} / 032 * {@code ChildBeanDefinition} where parent/child relationships happen to be pre-determined. 033 * 034 * @author Juergen Hoeller 035 * @since 2.5 036 * @see #setParentName 037 * @see RootBeanDefinition 038 * @see ChildBeanDefinition 039 */ 040@SuppressWarnings("serial") 041public class GenericBeanDefinition extends AbstractBeanDefinition { 042 043 @Nullable 044 private String parentName; 045 046 047 /** 048 * Create a new GenericBeanDefinition, to be configured through its bean 049 * properties and configuration methods. 050 * @see #setBeanClass 051 * @see #setScope 052 * @see #setConstructorArgumentValues 053 * @see #setPropertyValues 054 */ 055 public GenericBeanDefinition() { 056 super(); 057 } 058 059 /** 060 * Create a new GenericBeanDefinition as deep copy of the given 061 * bean definition. 062 * @param original the original bean definition to copy from 063 */ 064 public GenericBeanDefinition(BeanDefinition original) { 065 super(original); 066 } 067 068 069 @Override 070 public void setParentName(@Nullable String parentName) { 071 this.parentName = parentName; 072 } 073 074 @Override 075 @Nullable 076 public String getParentName() { 077 return this.parentName; 078 } 079 080 081 @Override 082 public AbstractBeanDefinition cloneBeanDefinition() { 083 return new GenericBeanDefinition(this); 084 } 085 086 @Override 087 public boolean equals(@Nullable Object other) { 088 if (this == other) { 089 return true; 090 } 091 if (!(other instanceof GenericBeanDefinition)) { 092 return false; 093 } 094 GenericBeanDefinition that = (GenericBeanDefinition) other; 095 return (ObjectUtils.nullSafeEquals(this.parentName, that.parentName) && super.equals(other)); 096 } 097 098 @Override 099 public String toString() { 100 if (this.parentName != null) { 101 return "Generic bean with parent '" + this.parentName + "': " + super.toString(); 102 } 103 return "Generic bean: " + super.toString(); 104 } 105 106}