KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > support > ChildBeanDefinition


1 /*
2  * Copyright 2002-2007 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 org.springframework.beans.MutablePropertyValues;
20 import org.springframework.beans.factory.config.ConstructorArgumentValues;
21 import org.springframework.util.ObjectUtils;
22
23 /**
24  * Bean definition for beans which inherit settings from their parent.
25  *
26  * <p>Will use the bean class of the parent if none specified, but can
27  * also override it. In the latter case, the child bean class must be
28  * compatible with the parent, i.e. accept the parent's property values
29  * and constructor argument values, if any.
30  *
31  * <p>A child bean definition will inherit constructor argument values,
32  * property values and method overrides from the parent, with the option
33  * to add new values. If init method, destroy method and/or static factory
34  * method are specified, they will override the corresponding parent settings.
35  *
36  * <p>The remaining settings will <i>always</i> be taken from the child definition:
37  * depends on, autowire mode, dependency check, singleton, lazy init.
38  *
39  * @author Rod Johnson
40  * @author Juergen Hoeller
41  * @see RootBeanDefinition
42  */

43 public class ChildBeanDefinition extends AbstractBeanDefinition {
44
45     private final String JavaDoc parentName;
46
47
48     /**
49      * Create a new ChildBeanDefinition for the given parent, to be
50      * configured through its bean properties and configuration methods.
51      * @param parentName the name of the parent bean
52      * @see #setBeanClass
53      * @see #setBeanClassName
54      * @see #setSingleton
55      * @see #setAutowireMode
56      * @see #setDependencyCheck
57      * @see #setConstructorArgumentValues
58      * @see #setPropertyValues
59      */

60     public ChildBeanDefinition(String JavaDoc parentName) {
61         super();
62         this.parentName = parentName;
63     }
64
65     /**
66      * Create a new ChildBeanDefinition for the given parent.
67      * @param parentName the name of the parent bean
68      * @param pvs the additional property values of the child
69      */

70     public ChildBeanDefinition(String JavaDoc parentName, MutablePropertyValues pvs) {
71         super(null, pvs);
72         this.parentName = parentName;
73     }
74
75     /**
76      * Create a new ChildBeanDefinition for the given parent.
77      * @param parentName the name of the parent bean
78      * @param cargs the constructor argument values to apply
79      * @param pvs the additional property values of the child
80      */

81     public ChildBeanDefinition(
82             String JavaDoc parentName, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
83
84         super(cargs, pvs);
85         this.parentName = parentName;
86     }
87
88     /**
89      * Create a new ChildBeanDefinition for the given parent,
90      * providing constructor arguments and property values.
91      * @param parentName the name of the parent bean
92      * @param beanClass the class of the bean to instantiate
93      * @param cargs the constructor argument values to apply
94      * @param pvs the property values to apply
95      */

96     public ChildBeanDefinition(
97             String JavaDoc parentName, Class JavaDoc beanClass, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
98
99         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(
114             String JavaDoc parentName, String JavaDoc beanClassName, 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     /**
133      * Return the name of the parent definition of this bean definition.
134      */

135     public String JavaDoc getParentName() {
136         return this.parentName;
137     }
138
139     public void validate() throws BeanDefinitionValidationException {
140         super.validate();
141         if (this.parentName == null) {
142             throw new BeanDefinitionValidationException("'parentName' must be set in ChildBeanDefinition");
143         }
144     }
145
146
147     public boolean equals(Object JavaDoc other) {
148         if (this == other) {
149             return true;
150         }
151         if (!(other instanceof ChildBeanDefinition)) {
152             return false;
153         }
154         ChildBeanDefinition that = (ChildBeanDefinition) other;
155         return (ObjectUtils.nullSafeEquals(this.parentName, that.parentName) && super.equals(other));
156     }
157
158     public int hashCode() {
159         return ObjectUtils.nullSafeHashCode(this.parentName) * 29 + super.hashCode();
160     }
161
162     public String JavaDoc toString() {
163         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Child bean with parent '");
164         sb.append(this.parentName).append("': ").append(super.toString());
165         return sb.toString();
166     }
167
168 }
169
Popular Tags