001/*
002 * Copyright 2002-2018 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;
018
019/**
020 * Extension of the {@link FactoryBean} interface. Implementations may
021 * indicate whether they always return independent instances, for the
022 * case where their {@link #isSingleton()} implementation returning
023 * {@code false} does not clearly indicate independent instances.
024 *
025 * <p>Plain {@link FactoryBean} implementations which do not implement
026 * this extended interface are simply assumed to always return independent
027 * instances if their {@link #isSingleton()} implementation returns
028 * {@code false}; the exposed object is only accessed on demand.
029 *
030 * <p><b>NOTE:</b> This interface is a special purpose interface, mainly for
031 * internal use within the framework and within collaborating frameworks.
032 * In general, application-provided FactoryBeans should simply implement
033 * the plain {@link FactoryBean} interface. New methods might be added
034 * to this extended interface even in point releases.
035 *
036 * @author Juergen Hoeller
037 * @since 2.0.3
038 * @param <T> the bean type
039 * @see #isPrototype()
040 * @see #isSingleton()
041 */
042public interface SmartFactoryBean<T> extends FactoryBean<T> {
043
044        /**
045         * Is the object managed by this factory a prototype? That is,
046         * will {@link #getObject()} always return an independent instance?
047         * <p>The prototype status of the FactoryBean itself will generally
048         * be provided by the owning {@link BeanFactory}; usually, it has to be
049         * defined as singleton there.
050         * <p>This method is supposed to strictly check for independent instances;
051         * it should not return {@code true} for scoped objects or other
052         * kinds of non-singleton, non-independent objects. For this reason,
053         * this is not simply the inverted form of {@link #isSingleton()}.
054         * <p>The default implementation returns {@code false}.
055         * @return whether the exposed object is a prototype
056         * @see #getObject()
057         * @see #isSingleton()
058         */
059        default boolean isPrototype() {
060                return false;
061        }
062
063        /**
064         * Does this FactoryBean expect eager initialization, that is,
065         * eagerly initialize itself as well as expect eager initialization
066         * of its singleton object (if any)?
067         * <p>A standard FactoryBean is not expected to initialize eagerly:
068         * Its {@link #getObject()} will only be called for actual access, even
069         * in case of a singleton object. Returning {@code true} from this
070         * method suggests that {@link #getObject()} should be called eagerly,
071         * also applying post-processors eagerly. This may make sense in case
072         * of a {@link #isSingleton() singleton} object, in particular if
073         * post-processors expect to be applied on startup.
074         * <p>The default implementation returns {@code false}.
075         * @return whether eager initialization applies
076         * @see org.springframework.beans.factory.config.ConfigurableListableBeanFactory#preInstantiateSingletons()
077         */
078        default boolean isEagerInit() {
079                return false;
080        }
081
082}