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.util.ObjectUtils;
021
022/**
023 * GenericBeanDefinition is a one-stop shop for standard bean definition purposes.
024 * Like any bean definition, it allows for specifying a class plus optionally
025 * constructor argument values and property values. Additionally, deriving from a
026 * parent bean definition can be flexibly configured through the "parentName" property.
027 *
028 * <p>In general, use this {@code GenericBeanDefinition} class for the purpose of
029 * registering user-visible bean definitions (which a post-processor might operate on,
030 * potentially even reconfiguring the parent name). Use {@code RootBeanDefinition} /
031 * {@code ChildBeanDefinition} where parent/child relationships happen to be pre-determined.
032 *
033 * @author Juergen Hoeller
034 * @since 2.5
035 * @see #setParentName
036 * @see RootBeanDefinition
037 * @see ChildBeanDefinition
038 */
039@SuppressWarnings("serial")
040public class GenericBeanDefinition extends AbstractBeanDefinition {
041
042        private String parentName;
043
044
045        /**
046         * Create a new GenericBeanDefinition, to be configured through its bean
047         * properties and configuration methods.
048         * @see #setBeanClass
049         * @see #setScope
050         * @see #setConstructorArgumentValues
051         * @see #setPropertyValues
052         */
053        public GenericBeanDefinition() {
054                super();
055        }
056
057        /**
058         * Create a new GenericBeanDefinition as deep copy of the given
059         * bean definition.
060         * @param original the original bean definition to copy from
061         */
062        public GenericBeanDefinition(BeanDefinition original) {
063                super(original);
064        }
065
066
067        @Override
068        public void setParentName(String parentName) {
069                this.parentName = parentName;
070        }
071
072        @Override
073        public String getParentName() {
074                return this.parentName;
075        }
076
077
078        @Override
079        public AbstractBeanDefinition cloneBeanDefinition() {
080                return new GenericBeanDefinition(this);
081        }
082
083        @Override
084        public boolean equals(Object other) {
085                if (this == other) {
086                        return true;
087                }
088                if (!(other instanceof GenericBeanDefinition)) {
089                        return false;
090                }
091                GenericBeanDefinition that = (GenericBeanDefinition) other;
092                return (ObjectUtils.nullSafeEquals(this.parentName, that.parentName) && super.equals(other));
093        }
094
095        @Override
096        public String toString() {
097                StringBuilder sb = new StringBuilder("Generic bean");
098                if (this.parentName != null) {
099                        sb.append(" with parent '").append(this.parentName).append("'");
100                }
101                sb.append(": ").append(super.toString());
102                return sb.toString();
103        }
104
105}