KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2006 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.PropertyValue;
20 import org.springframework.beans.factory.config.RuntimeBeanReference;
21 import org.springframework.util.ObjectUtils;
22
23 /**
24  * Programmatic means of constructing
25  * {@link org.springframework.beans.factory.config.BeanDefinition BeanDefinitions}
26  * using the builder pattern. Intended primarily for use when implementing Spring 2.0
27  * {@link org.springframework.beans.factory.xml.NamespaceHandler NamespaceHandlers}.
28  *
29  * @author Rod Johnson
30  * @author Rob Harrop
31  * @author Juergen Hoeller
32  * @since 2.0
33  */

34 public class BeanDefinitionBuilder {
35
36     /**
37      * Create a new <code>BeanDefinitionBuilder</code> used to construct a {@link RootBeanDefinition}.
38      * @param beanClass the <code>Class</code> of the bean the definition is being created for.
39      */

40     public static BeanDefinitionBuilder rootBeanDefinition(Class JavaDoc beanClass) {
41         return rootBeanDefinition(beanClass, null);
42     }
43
44     /**
45      * Create a new <code>BeanDefinitionBuilder</code> used to construct a {@link RootBeanDefinition}.
46      * @param beanClass the <code>Class</code> of the bean the definition is being created for.
47      * @param factoryMethod the name of the method to use to construct the bean instance.
48      */

49     public static BeanDefinitionBuilder rootBeanDefinition(Class JavaDoc beanClass, String JavaDoc factoryMethod) {
50         BeanDefinitionBuilder builder = new BeanDefinitionBuilder();
51         builder.beanDefinition = new RootBeanDefinition();
52         builder.beanDefinition.setBeanClass(beanClass);
53         builder.beanDefinition.setFactoryMethodName(factoryMethod);
54         return builder;
55     }
56
57     /**
58      * Create a new <code>BeanDefinitionBuilder</code> used to construct a {@link ChildBeanDefinition}.
59      * @param parentBeanName the name of the parent bean.
60      */

61     public static BeanDefinitionBuilder childBeanDefinition(String JavaDoc parentBeanName) {
62         BeanDefinitionBuilder builder = new BeanDefinitionBuilder();
63         builder.beanDefinition = new ChildBeanDefinition(parentBeanName);
64         return builder;
65     }
66
67
68     /**
69      * The <code>BeanDefinition</code> instance we are creating.
70      */

71     private AbstractBeanDefinition beanDefinition;
72
73     /**
74      * Our current position with respect to constructor args.
75      */

76     private int constructorArgIndex;
77
78
79     /**
80      * Enforce the use of factory methods.
81      */

82     private BeanDefinitionBuilder() {
83     }
84
85     /**
86      * Return the current BeanDefinition object in its raw (unvalidated) form.
87      * @see #getBeanDefinition()
88      */

89     public AbstractBeanDefinition getRawBeanDefinition() {
90         return this.beanDefinition;
91     }
92
93     /**
94      * Validate and return the created BeanDefinition object.
95      */

96     public AbstractBeanDefinition getBeanDefinition() {
97         this.beanDefinition.validate();
98         return this.beanDefinition;
99     }
100
101
102     /**
103      * Add the supplied property value under the given name.
104      */

105     public BeanDefinitionBuilder addPropertyValue(String JavaDoc name, Object JavaDoc value) {
106         this.beanDefinition.getPropertyValues().addPropertyValue(new PropertyValue(name, value));
107         return this;
108     }
109
110     /**
111      * Add a reference to the specified bean name under the property specified.
112      * @param name the name of the property to add the reference to
113      * @param beanName the name of the bean being referenced
114      */

115     public BeanDefinitionBuilder addPropertyReference(String JavaDoc name, String JavaDoc beanName) {
116         return addPropertyValue(name, new RuntimeBeanReference(beanName));
117     }
118
119     /**
120      * Add an indexed constructor arg value. The current index is tracked internally and all
121      * additions are at the present point.
122      */

123     public BeanDefinitionBuilder addConstructorArg(Object JavaDoc value) {
124         this.beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(constructorArgIndex++, value);
125         return this;
126     }
127
128     /**
129      * Add a reference to a named bean as a constructor arg.
130      * @see #addConstructorArg(Object)
131      */

132     public BeanDefinitionBuilder addConstructorArgReference(String JavaDoc beanName) {
133         return addConstructorArg(new RuntimeBeanReference(beanName));
134     }
135
136     /**
137      * Set the name of the factory method to use for this definition.
138      */

139     public BeanDefinitionBuilder setFactoryMethod(String JavaDoc factoryMethod) {
140         this.beanDefinition.setFactoryMethodName(factoryMethod);
141         return this;
142     }
143
144     /**
145      * Set the name of the factory bean to use for this definition.
146      */

147     public BeanDefinitionBuilder setFactoryBean(String JavaDoc factoryBean, String JavaDoc factoryMethod) {
148         this.beanDefinition.setFactoryBeanName(factoryBean);
149         this.beanDefinition.setFactoryMethodName(factoryMethod);
150         return this;
151     }
152
153     /**
154      * Set the scope of this definition.
155      * @see org.springframework.beans.factory.config.BeanDefinition#SCOPE_SINGLETON
156      * @see org.springframework.beans.factory.config.BeanDefinition#SCOPE_PROTOTYPE
157      */

158     public BeanDefinitionBuilder setScope(String JavaDoc scope) {
159         this.beanDefinition.setScope(scope);
160         return this;
161     }
162
163     /**
164      * Set whether or not this definition describes a singleton bean,
165      * as alternative to <code>setScope</code>.
166      * @see #setScope
167      */

168     public BeanDefinitionBuilder setSingleton(boolean singleton) {
169         this.beanDefinition.setSingleton(singleton);
170         return this;
171     }
172
173     /**
174      * Set whether or not this definition is abstract.
175      */

176     public BeanDefinitionBuilder setAbstract(boolean flag) {
177         this.beanDefinition.setAbstract(flag);
178         return this;
179     }
180
181     /**
182      * Set whether beans for this definition should be lazily initialized or not.
183      */

184     public BeanDefinitionBuilder setLazyInit(boolean lazy) {
185         this.beanDefinition.setLazyInit(lazy);
186         return this;
187     }
188
189     /**
190      * Set the autowire mode for this definition.
191      */

192     public BeanDefinitionBuilder setAutowireMode(int autowireMode) {
193         beanDefinition.setAutowireMode(autowireMode);
194         return this;
195     }
196
197     /**
198      * Set the depency check mode for this definition.
199      */

200     public BeanDefinitionBuilder setDependencyCheck(int dependencyCheck) {
201         beanDefinition.setDependencyCheck(dependencyCheck);
202         return this;
203     }
204
205     /**
206      * Set the destroy method for this definition.
207      */

208     public BeanDefinitionBuilder setDestroyMethodName(String JavaDoc methodName) {
209         beanDefinition.setDestroyMethodName(methodName);
210         return this;
211     }
212
213     /**
214      * Set the init method for this definition.
215      */

216     public BeanDefinitionBuilder setInitMethodName(String JavaDoc methodName) {
217         beanDefinition.setInitMethodName(methodName);
218         return this;
219     }
220
221     /**
222      * Set the description associated with this definition.
223      */

224     public BeanDefinitionBuilder setResourceDescription(String JavaDoc resourceDescription) {
225         this.beanDefinition.setResourceDescription(resourceDescription);
226         return this;
227     }
228
229     /**
230      * Append the specified bean name to the list of beans that this definition
231      * depends on.
232      */

233     public BeanDefinitionBuilder addDependsOn(String JavaDoc beanName) {
234         if (this.beanDefinition.getDependsOn() == null) {
235             this.beanDefinition.setDependsOn(new String JavaDoc[] { beanName });
236         }
237         else {
238             String JavaDoc[] added = (String JavaDoc[]) ObjectUtils.addObjectToArray(beanDefinition.getDependsOn(), beanName);
239             this.beanDefinition.setDependsOn(added);
240         }
241         return this;
242     }
243
244     /**
245      * Set the source of this definition.
246      * @see AbstractBeanDefinition#setSource
247      */

248     public BeanDefinitionBuilder setSource(Object JavaDoc source) {
249         this.beanDefinition.setSource(source);
250         return this;
251     }
252
253     /**
254      * Set the role of this definition.
255      * @see AbstractBeanDefinition#setRole
256      */

257     public BeanDefinitionBuilder setRole(int role) {
258         this.beanDefinition.setRole(role);
259         return this;
260     }
261
262 }
263
Popular Tags