001/*
002 * Copyright 2002-2017 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.config;
018
019import org.springframework.beans.BeanMetadataElement;
020import org.springframework.beans.MutablePropertyValues;
021import org.springframework.core.AttributeAccessor;
022
023/**
024 * A BeanDefinition describes a bean instance, which has property values,
025 * constructor argument values, and further information supplied by
026 * concrete implementations.
027 *
028 * <p>This is just a minimal interface: The main intention is to allow a
029 * {@link BeanFactoryPostProcessor} such as {@link PropertyPlaceholderConfigurer}
030 * to introspect and modify property values and other bean metadata.
031 *
032 * @author Juergen Hoeller
033 * @author Rob Harrop
034 * @since 19.03.2004
035 * @see ConfigurableListableBeanFactory#getBeanDefinition
036 * @see org.springframework.beans.factory.support.RootBeanDefinition
037 * @see org.springframework.beans.factory.support.ChildBeanDefinition
038 */
039public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
040
041        /**
042         * Scope identifier for the standard singleton scope: "singleton".
043         * <p>Note that extended bean factories might support further scopes.
044         * @see #setScope
045         */
046        String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;
047
048        /**
049         * Scope identifier for the standard prototype scope: "prototype".
050         * <p>Note that extended bean factories might support further scopes.
051         * @see #setScope
052         */
053        String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;
054
055
056        /**
057         * Role hint indicating that a {@code BeanDefinition} is a major part
058         * of the application. Typically corresponds to a user-defined bean.
059         */
060        int ROLE_APPLICATION = 0;
061
062        /**
063         * Role hint indicating that a {@code BeanDefinition} is a supporting
064         * part of some larger configuration, typically an outer
065         * {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
066         * {@code SUPPORT} beans are considered important enough to be aware
067         * of when looking more closely at a particular
068         * {@link org.springframework.beans.factory.parsing.ComponentDefinition},
069         * but not when looking at the overall configuration of an application.
070         */
071        int ROLE_SUPPORT = 1;
072
073        /**
074         * Role hint indicating that a {@code BeanDefinition} is providing an
075         * entirely background role and has no relevance to the end-user. This hint is
076         * used when registering beans that are completely part of the internal workings
077         * of a {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
078         */
079        int ROLE_INFRASTRUCTURE = 2;
080
081
082        // Modifiable attributes
083
084        /**
085         * Set the name of the parent definition of this bean definition, if any.
086         */
087        void setParentName(String parentName);
088
089        /**
090         * Return the name of the parent definition of this bean definition, if any.
091         */
092        String getParentName();
093
094        /**
095         * Specify the bean class name of this bean definition.
096         * <p>The class name can be modified during bean factory post-processing,
097         * typically replacing the original class name with a parsed variant of it.
098         * @see #setParentName
099         * @see #setFactoryBeanName
100         * @see #setFactoryMethodName
101         */
102        void setBeanClassName(String beanClassName);
103
104        /**
105         * Return the current bean class name of this bean definition.
106         * <p>Note that this does not have to be the actual class name used at runtime, in
107         * case of a child definition overriding/inheriting the class name from its parent.
108         * Also, this may just be the class that a factory method is called on, or it may
109         * even be empty in case of a factory bean reference that a method is called on.
110         * Hence, do <i>not</i> consider this to be the definitive bean type at runtime but
111         * rather only use it for parsing purposes at the individual bean definition level.
112         * @see #getParentName()
113         * @see #getFactoryBeanName()
114         * @see #getFactoryMethodName()
115         */
116        String getBeanClassName();
117
118        /**
119         * Override the target scope of this bean, specifying a new scope name.
120         * @see #SCOPE_SINGLETON
121         * @see #SCOPE_PROTOTYPE
122         */
123        void setScope(String scope);
124
125        /**
126         * Return the name of the current target scope for this bean,
127         * or {@code null} if not known yet.
128         */
129        String getScope();
130
131        /**
132         * Set whether this bean should be lazily initialized.
133         * <p>If {@code false}, the bean will get instantiated on startup by bean
134         * factories that perform eager initialization of singletons.
135         */
136        void setLazyInit(boolean lazyInit);
137
138        /**
139         * Return whether this bean should be lazily initialized, i.e. not
140         * eagerly instantiated on startup. Only applicable to a singleton bean.
141         */
142        boolean isLazyInit();
143
144        /**
145         * Set the names of the beans that this bean depends on being initialized.
146         * The bean factory will guarantee that these beans get initialized first.
147         */
148        void setDependsOn(String... dependsOn);
149
150        /**
151         * Return the bean names that this bean depends on.
152         */
153        String[] getDependsOn();
154
155        /**
156         * Set whether this bean is a candidate for getting autowired into some other bean.
157         * <p>Note that this flag is designed to only affect type-based autowiring.
158         * It does not affect explicit references by name, which will get resolved even
159         * if the specified bean is not marked as an autowire candidate. As a consequence,
160         * autowiring by name will nevertheless inject a bean if the name matches.
161         */
162        void setAutowireCandidate(boolean autowireCandidate);
163
164        /**
165         * Return whether this bean is a candidate for getting autowired into some other bean.
166         */
167        boolean isAutowireCandidate();
168
169        /**
170         * Set whether this bean is a primary autowire candidate.
171         * <p>If this value is {@code true} for exactly one bean among multiple
172         * matching candidates, it will serve as a tie-breaker.
173         */
174        void setPrimary(boolean primary);
175
176        /**
177         * Return whether this bean is a primary autowire candidate.
178         */
179        boolean isPrimary();
180
181        /**
182         * Specify the factory bean to use, if any.
183         * This the name of the bean to call the specified factory method on.
184         * @see #setFactoryMethodName
185         */
186        void setFactoryBeanName(String factoryBeanName);
187
188        /**
189         * Return the factory bean name, if any.
190         */
191        String getFactoryBeanName();
192
193        /**
194         * Specify a factory method, if any. This method will be invoked with
195         * constructor arguments, or with no arguments if none are specified.
196         * The method will be invoked on the specified factory bean, if any,
197         * or otherwise as a static method on the local bean class.
198         * @see #setFactoryBeanName
199         * @see #setBeanClassName
200         */
201        void setFactoryMethodName(String factoryMethodName);
202
203        /**
204         * Return a factory method, if any.
205         */
206        String getFactoryMethodName();
207
208        /**
209         * Return the constructor argument values for this bean.
210         * <p>The returned instance can be modified during bean factory post-processing.
211         * @return the ConstructorArgumentValues object (never {@code null})
212         */
213        ConstructorArgumentValues getConstructorArgumentValues();
214
215        /**
216         * Return the property values to be applied to a new instance of the bean.
217         * <p>The returned instance can be modified during bean factory post-processing.
218         * @return the MutablePropertyValues object (never {@code null})
219         */
220        MutablePropertyValues getPropertyValues();
221
222
223        // Read-only attributes
224
225        /**
226         * Return whether this a <b>Singleton</b>, with a single, shared instance
227         * returned on all calls.
228         * @see #SCOPE_SINGLETON
229         */
230        boolean isSingleton();
231
232        /**
233         * Return whether this a <b>Prototype</b>, with an independent instance
234         * returned for each call.
235         * @since 3.0
236         * @see #SCOPE_PROTOTYPE
237         */
238        boolean isPrototype();
239
240        /**
241         * Return whether this bean is "abstract", that is, not meant to be instantiated.
242         */
243        boolean isAbstract();
244
245        /**
246         * Get the role hint for this {@code BeanDefinition}. The role hint
247         * provides the frameworks as well as tools with an indication of
248         * the role and importance of a particular {@code BeanDefinition}.
249         * @see #ROLE_APPLICATION
250         * @see #ROLE_SUPPORT
251         * @see #ROLE_INFRASTRUCTURE
252         */
253        int getRole();
254
255        /**
256         * Return a human-readable description of this bean definition.
257         */
258        String getDescription();
259
260        /**
261         * Return a description of the resource that this bean definition
262         * came from (for the purpose of showing context in case of errors).
263         */
264        String getResourceDescription();
265
266        /**
267         * Return the originating BeanDefinition, or {@code null} if none.
268         * Allows for retrieving the decorated bean definition, if any.
269         * <p>Note that this method returns the immediate originator. Iterate through the
270         * originator chain to find the original BeanDefinition as defined by the user.
271         */
272        BeanDefinition getOriginatingBeanDefinition();
273
274}