001: /*
002: * Copyright 2002-2007 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: * http://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:
017: package org.springframework.beans.factory.support;
018:
019: import org.springframework.beans.MutablePropertyValues;
020: import org.springframework.beans.factory.config.ConstructorArgumentValues;
021: import org.springframework.util.ObjectUtils;
022:
023: /**
024: * Bean definition for beans which inherit settings from their parent.
025: *
026: * <p>Will use the bean class of the parent if none specified, but can
027: * also override it. In the latter case, the child bean class must be
028: * compatible with the parent, i.e. accept the parent's property values
029: * and constructor argument values, if any.
030: *
031: * <p>A child bean definition will inherit constructor argument values,
032: * property values and method overrides from the parent, with the option
033: * to add new values. If init method, destroy method and/or static factory
034: * method are specified, they will override the corresponding parent settings.
035: *
036: * <p>The remaining settings will <i>always</i> be taken from the child definition:
037: * depends on, autowire mode, dependency check, singleton, lazy init.
038: *
039: * @author Rod Johnson
040: * @author Juergen Hoeller
041: * @see RootBeanDefinition
042: */
043: public class ChildBeanDefinition extends AbstractBeanDefinition {
044:
045: private final String parentName;
046:
047: /**
048: * Create a new ChildBeanDefinition for the given parent, to be
049: * configured through its bean properties and configuration methods.
050: * @param parentName the name of the parent bean
051: * @see #setBeanClass
052: * @see #setBeanClassName
053: * @see #setSingleton
054: * @see #setAutowireMode
055: * @see #setDependencyCheck
056: * @see #setConstructorArgumentValues
057: * @see #setPropertyValues
058: */
059: public ChildBeanDefinition(String parentName) {
060: super ();
061: this .parentName = parentName;
062: }
063:
064: /**
065: * Create a new ChildBeanDefinition for the given parent.
066: * @param parentName the name of the parent bean
067: * @param pvs the additional property values of the child
068: */
069: public ChildBeanDefinition(String parentName,
070: MutablePropertyValues pvs) {
071: super (null, pvs);
072: this .parentName = parentName;
073: }
074:
075: /**
076: * Create a new ChildBeanDefinition for the given parent.
077: * @param parentName the name of the parent bean
078: * @param cargs the constructor argument values to apply
079: * @param pvs the additional property values of the child
080: */
081: public ChildBeanDefinition(String parentName,
082: ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
083:
084: super (cargs, pvs);
085: this .parentName = parentName;
086: }
087:
088: /**
089: * Create a new ChildBeanDefinition for the given parent,
090: * providing constructor arguments and property values.
091: * @param parentName the name of the parent bean
092: * @param beanClass the class of the bean to instantiate
093: * @param cargs the constructor argument values to apply
094: * @param pvs the property values to apply
095: */
096: public ChildBeanDefinition(String parentName, Class beanClass,
097: ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
098:
099: super (cargs, pvs);
100: this .parentName = parentName;
101: setBeanClass(beanClass);
102: }
103:
104: /**
105: * Create a new ChildBeanDefinition for the given parent,
106: * providing constructor arguments and property values.
107: * Takes a bean class name to avoid eager loading of the bean class.
108: * @param parentName the name of the parent bean
109: * @param beanClassName the name of the class to instantiate
110: * @param cargs the constructor argument values to apply
111: * @param pvs the property values to apply
112: */
113: public ChildBeanDefinition(String parentName, String beanClassName,
114: ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
115:
116: super (cargs, pvs);
117: this .parentName = parentName;
118: setBeanClassName(beanClassName);
119: }
120:
121: /**
122: * Create a new ChildBeanDefinition as deep copy of the given
123: * bean definition.
124: * @param original the original bean definition to copy from
125: */
126: public ChildBeanDefinition(ChildBeanDefinition original) {
127: super (original);
128: this .parentName = original.getParentName();
129: }
130:
131: /**
132: * Return the name of the parent definition of this bean definition.
133: */
134: public String getParentName() {
135: return this .parentName;
136: }
137:
138: public void validate() throws BeanDefinitionValidationException {
139: super .validate();
140: if (this .parentName == null) {
141: throw new BeanDefinitionValidationException(
142: "'parentName' must be set in ChildBeanDefinition");
143: }
144: }
145:
146: public boolean equals(Object other) {
147: if (this == other) {
148: return true;
149: }
150: if (!(other instanceof ChildBeanDefinition)) {
151: return false;
152: }
153: ChildBeanDefinition that = (ChildBeanDefinition) other;
154: return (ObjectUtils.nullSafeEquals(this .parentName,
155: that.parentName) && super .equals(other));
156: }
157:
158: public int hashCode() {
159: return ObjectUtils.nullSafeHashCode(this .parentName) * 29
160: + super .hashCode();
161: }
162:
163: public String toString() {
164: StringBuffer sb = new StringBuffer("Child bean with parent '");
165: sb.append(this .parentName).append("': ").append(
166: super.toString());
167: return sb.toString();
168: }
169:
170: }
|