1 /*
2 * Copyright 2002-2008 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.config;
18
19 import java.util.Set;
20
21 import org.springframework.beans.BeansException;
22 import org.springframework.beans.TypeConverter;
23 import org.springframework.beans.factory.BeanFactory;
24
25 /**
26 * Extension of the {@link org.springframework.beans.factory.BeanFactory}
27 * interface to be implemented by bean factories that are capable of
28 * autowiring, provided that they want to expose this functionality for
29 * existing bean instances.
30 *
31 * <p>This subinterface of BeanFactory is not meant to be used in normal
32 * application code: stick to {@link org.springframework.beans.factory.BeanFactory}
33 * or {@link org.springframework.beans.factory.ListableBeanFactory} for
34 * typical use cases.
35 *
36 * <p>Integration code for other frameworks can leverage this interface to
37 * wire and populate existing bean instances that Spring does not control
38 * the lifecycle of. This is particularly useful for WebWork Actions and
39 * Tapestry Page objects, for example.
40 *
41 * <p>Note that this interface is not implemented by
42 * {@link org.springframework.context.ApplicationContext} facades,
43 * as it is hardly ever used by application code. That said, it is available
44 * from an application context too, accessible through ApplicationContext's
45 * {@link org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory()}
46 * method.
47 *
48 * <p>You may also implement the {@link org.springframework.beans.factory.BeanFactoryAware}
49 * interface, which exposes the internal BeanFactory even when running in an
50 * ApplicationContext, to get access to an AutowireCapableBeanFactory:
51 * simply cast the passed-in BeanFactory to AutowireCapableBeanFactory.
52 *
53 * @author Juergen Hoeller
54 * @since 04.12.2003
55 * @see org.springframework.beans.factory.BeanFactoryAware
56 * @see org.springframework.beans.factory.config.ConfigurableListableBeanFactory
57 * @see org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory()
58 */
59 public interface AutowireCapableBeanFactory extends BeanFactory {
60
61 /**
62 * Constant that indicates no externally defined autowiring. Note that
63 * BeanFactoryAware etc and annotation-driven injection will still be applied.
64 * @see #createBean
65 * @see #autowire
66 * @see #autowireBeanProperties
67 */
68 int AUTOWIRE_NO = 0;
69
70 /**
71 * Constant that indicates autowiring bean properties by name
72 * (applying to all bean property setters).
73 * @see #createBean
74 * @see #autowire
75 * @see #autowireBeanProperties
76 */
77 int AUTOWIRE_BY_NAME = 1;
78
79 /**
80 * Constant that indicates autowiring bean properties by type
81 * (applying to all bean property setters).
82 * @see #createBean
83 * @see #autowire
84 * @see #autowireBeanProperties
85 */
86 int AUTOWIRE_BY_TYPE = 2;
87
88 /**
89 * Constant that indicates autowiring the greediest constructor that
90 * can be satisfied (involves resolving the appropriate constructor).
91 * @see #createBean
92 * @see #autowire
93 */
94 int AUTOWIRE_CONSTRUCTOR = 3;
95
96 /**
97 * Constant that indicates determining an appropriate autowire strategy
98 * through introspection of the bean class.
99 * @see #createBean
100 * @see #autowire
101 */
102 int AUTOWIRE_AUTODETECT = 4;
103
104
105 //-------------------------------------------------------------------------
106 // Typical methods for creating and populating external bean instances
107 //-------------------------------------------------------------------------
108
109 /**
110 * Fully create a new bean instance of the given class.
111 * <p>Performs full initialization of the bean, including all applicable
112 * {@link BeanPostProcessor BeanPostProcessors}.
113 * <p>Note: This is intended for creating a fresh instance, populating annotated
114 * fields and methods as well as applying all standard bean initialiation callbacks.
115 * It does <i>not</> imply traditional by-name or by-type autowiring of properties;
116 * use {@link #createBean(Class, int, boolean)} for that purposes.
117 * @param beanClass the class of the bean to create
118 * @return the new bean instance
119 * @throws BeansException if instantiation or wiring failed
120 */
121 Object createBean(Class beanClass) throws BeansException;
122
123 /**
124 * Populate the given bean instance through applying after-instantiation callbacks
125 * and bean property post-processing (e.g. for annotation-driven injection).
126 * <p>Note: This is essentially intended for (re-)populating annotated fields and
127 * methods, either for new instances or for deserialized instances. It does
128 * <i>not</i> imply traditional by-name or by-type autowiring of properties;
129 * use {@link #autowireBeanProperties} for that purposes.
130 * @param existingBean the existing bean instance
131 * @throws BeansException if wiring failed
132 */
133 void autowireBean(Object existingBean) throws BeansException;
134
135 /**
136 * Configure the given raw bean: autowiring bean properties, applying
137 * bean property values, applying factory callbacks such as <code>setBeanName</code>
138 * and <code>setBeanFactory</code>, and also applying all bean post processors
139 * (including ones which might wrap the given raw bean).
140 * <p>This is effectively a superset of what {@link #initializeBean} provides,
141 * fully applying the configuration specified by the corresponding bean definition.
142 * <b>Note: This method requires a bean definition for the given name!</b>
143 * @param existingBean the existing bean instance
144 * @param beanName the name of the bean, to be passed to it if necessary
145 * (a bean definition of that name has to be available)
146 * @return the bean instance to use, either the original or a wrapped one
147 * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
148 * if there is no bean definition with the given name
149 * @throws BeansException if the initialization failed
150 * @see #initializeBean
151 */
152 Object configureBean(Object existingBean, String beanName) throws BeansException;
153
154 /**
155 * Resolve the specified dependency against the beans defined in this factory.
156 * @param descriptor the descriptor for the dependency
157 * @param beanName the name of the bean which declares the present dependency
158 * @return the resolved object, or <code>null</code> if none found
159 * @throws BeansException in dependency resolution failed
160 */
161 Object resolveDependency(DependencyDescriptor descriptor, String beanName) throws BeansException;
162
163
164 //-------------------------------------------------------------------------
165 // Specialized methods for fine-grained control over the bean lifecycle
166 //-------------------------------------------------------------------------
167
168 /**
169 * Fully create a new bean instance of the given class with the specified
170 * autowire strategy. All constants defined in this interface are supported here.
171 * <p>Performs full initialization of the bean, including all applicable
172 * {@link BeanPostProcessor BeanPostProcessors}. This is effectively a superset
173 * of what {@link #autowire} provides, adding {@link #initializeBean} behavior.
174 * @param beanClass the class of the bean to create
175 * @param autowireMode by name or type, using the constants in this interface
176 * @param dependencyCheck whether to perform a dependency check for objects
177 * (not applicable to autowiring a constructor, thus ignored there)
178 * @return the new bean instance
179 * @throws BeansException if instantiation or wiring failed
180 * @see #AUTOWIRE_NO
181 * @see #AUTOWIRE_BY_NAME
182 * @see #AUTOWIRE_BY_TYPE
183 * @see #AUTOWIRE_CONSTRUCTOR
184 * @see #AUTOWIRE_AUTODETECT
185 */
186 Object createBean(Class beanClass, int autowireMode, boolean dependencyCheck) throws BeansException;
187
188 /**
189 * Instantiate a new bean instance of the given class with the specified autowire
190 * strategy. All constants defined in this interface are supported here.
191 * Can also be invoked with <code>AUTOWIRE_NO</code> in order to just apply
192 * before-instantiation callbacks (e.g. for annotation-driven injection).
193 * <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors}
194 * callbacks or perform any further initialization of the bean. This interface
195 * offers distinct, fine-grained operations for those purposes, for example
196 * {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor}
197 * callbacks are applied, if applicable to the construction of the instance.
198 * @param beanClass the class of the bean to instantiate
199 * @param autowireMode by name or type, using the constants in this interface
200 * @param dependencyCheck whether to perform a dependency check for object
201 * references in the bean instance (not applicable to autowiring a constructor,
202 * thus ignored there)
203 * @return the new bean instance
204 * @throws BeansException if instantiation or wiring failed
205 * @see #AUTOWIRE_NO
206 * @see #AUTOWIRE_BY_NAME
207 * @see #AUTOWIRE_BY_TYPE
208 * @see #AUTOWIRE_CONSTRUCTOR
209 * @see #AUTOWIRE_AUTODETECT
210 * @see #initializeBean
211 * @see #applyBeanPostProcessorsBeforeInitialization
212 * @see #applyBeanPostProcessorsAfterInitialization
213 */
214 Object autowire(Class beanClass, int autowireMode, boolean dependencyCheck) throws BeansException;
215
216 /**
217 * Autowire the bean properties of the given bean instance by name or type.
218 * Can also be invoked with <code>AUTOWIRE_NO</code> in order to just apply
219 * after-instantiation callbacks (e.g. for annotation-driven injection).
220 * <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors}
221 * callbacks or perform any further initialization of the bean. This interface
222 * offers distinct, fine-grained operations for those purposes, for example
223 * {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor}
224 * callbacks are applied, if applicable to the configuration of the instance.
225 * @param existingBean the existing bean instance
226 * @param autowireMode by name or type, using the constants in this interface
227 * @param dependencyCheck whether to perform a dependency check for object
228 * references in the bean instance
229 * @throws BeansException if wiring failed
230 * @see #AUTOWIRE_BY_NAME
231 * @see #AUTOWIRE_BY_TYPE
232 * @see #AUTOWIRE_NO
233 */
234 void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck)
235 throws BeansException;
236
237 /**
238 * Apply the property values of the bean definition with the given name to
239 * the given bean instance. The bean definition can either define a fully
240 * self-contained bean, reusing its property values, or just property values
241 * meant to be used for existing bean instances.
242 * <p>This method does <i>not</i> autowire bean properties; it just applies
243 * explicitly defined property values. Use the {@link #autowireBeanProperties}
244 * method to autowire an existing bean instance.
245 * <b>Note: This method requires a bean definition for the given name!</b>
246 * <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors}
247 * callbacks or perform any further initialization of the bean. This interface
248 * offers distinct, fine-grained operations for those purposes, for example
249 * {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor}
250 * callbacks are applied, if applicable to the configuration of the instance.
251 * @param existingBean the existing bean instance
252 * @param beanName the name of the bean definition in the bean factory
253 * (a bean definition of that name has to be available)
254 * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
255 * if there is no bean definition with the given name
256 * @throws BeansException if applying the property values failed
257 * @see #autowireBeanProperties
258 */
259 void applyBeanPropertyValues(Object existingBean, String beanName) throws BeansException;
260
261 /**
262 * Initialize the given raw bean, applying factory callbacks
263 * such as <code>setBeanName</code> and <code>setBeanFactory</code>,
264 * also applying all bean post processors (including ones which
265 * might wrap the given raw bean).
266 * <p>Note that no bean definition of the given name has to exist
267 * in the bean factory. The passed-in bean name will simply be used
268 * for callbacks but not checked against the registered bean definitions.
269 * @param existingBean the existing bean instance
270 * @param beanName the name of the bean, to be passed to it if necessary
271 * (only passed to {@link BeanPostProcessor BeanPostProcessors})
272 * @return the bean instance to use, either the original or a wrapped one
273 * @throws BeansException if the initialization failed
274 */
275 Object initializeBean(Object existingBean, String beanName) throws BeansException;
276
277 /**
278 * Apply {@link BeanPostProcessor BeanPostProcessors} to the given existing bean
279 * instance, invoking their <code>postProcessBeforeInitialization</code> methods.
280 * The returned bean instance may be a wrapper around the original.
281 * @param existingBean the new bean instance
282 * @param beanName the name of the bean
283 * @return the bean instance to use, either the original or a wrapped one
284 * @throws BeansException if any post-processing failed
285 * @see BeanPostProcessor#postProcessBeforeInitialization
286 */
287 Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
288 throws BeansException;
289
290 /**
291 * Apply {@link BeanPostProcessor BeanPostProcessors} to the given existing bean
292 * instance, invoking their <code>postProcessAfterInitialization</code> methods.
293 * The returned bean instance may be a wrapper around the original.
294 * @param existingBean the new bean instance
295 * @param beanName the name of the bean
296 * @return the bean instance to use, either the original or a wrapped one
297 * @throws BeansException if any post-processing failed
298 * @see BeanPostProcessor#postProcessAfterInitialization
299 */
300 Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
301 throws BeansException;
302
303 /**
304 * Resolve the specified dependency against the beans defined in this factory.
305 * @param descriptor the descriptor for the dependency
306 * @param beanName the name of the bean which declares the present dependency
307 * @param autowiredBeanNames a Set that all names of autowired beans (used for
308 * resolving the present dependency) are supposed to be added to
309 * @param typeConverter the TypeConverter to use for populating arrays and
310 * collections
311 * @return the resolved object, or <code>null</code> if none found
312 * @throws BeansException in dependency resolution failed
313 */
314 Object resolveDependency(DependencyDescriptor descriptor, String beanName,
315 Set autowiredBeanNames, TypeConverter typeConverter) throws BeansException;
316
317 }