001/*
002 * Copyright 2002-2016 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 java.lang.reflect.Constructor;
020
021import org.springframework.beans.BeansException;
022import org.springframework.lang.Nullable;
023
024/**
025 * Extension of the {@link InstantiationAwareBeanPostProcessor} interface,
026 * adding a callback for predicting the eventual type of a processed bean.
027 *
028 * <p><b>NOTE:</b> This interface is a special purpose interface, mainly for
029 * internal use within the framework. In general, application-provided
030 * post-processors should simply implement the plain {@link BeanPostProcessor}
031 * interface or derive from the {@link InstantiationAwareBeanPostProcessorAdapter}
032 * class. New methods might be added to this interface even in point releases.
033 *
034 * @author Juergen Hoeller
035 * @since 2.0.3
036 * @see InstantiationAwareBeanPostProcessorAdapter
037 */
038public interface SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor {
039
040        /**
041         * Predict the type of the bean to be eventually returned from this
042         * processor's {@link #postProcessBeforeInstantiation} callback.
043         * <p>The default implementation returns {@code null}.
044         * @param beanClass the raw class of the bean
045         * @param beanName the name of the bean
046         * @return the type of the bean, or {@code null} if not predictable
047         * @throws org.springframework.beans.BeansException in case of errors
048         */
049        @Nullable
050        default Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
051                return null;
052        }
053
054        /**
055         * Determine the candidate constructors to use for the given bean.
056         * <p>The default implementation returns {@code null}.
057         * @param beanClass the raw class of the bean (never {@code null})
058         * @param beanName the name of the bean
059         * @return the candidate constructors, or {@code null} if none specified
060         * @throws org.springframework.beans.BeansException in case of errors
061         */
062        @Nullable
063        default Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName)
064                        throws BeansException {
065
066                return null;
067        }
068
069        /**
070         * Obtain a reference for early access to the specified bean,
071         * typically for the purpose of resolving a circular reference.
072         * <p>This callback gives post-processors a chance to expose a wrapper
073         * early - that is, before the target bean instance is fully initialized.
074         * The exposed object should be equivalent to the what
075         * {@link #postProcessBeforeInitialization} / {@link #postProcessAfterInitialization}
076         * would expose otherwise. Note that the object returned by this method will
077         * be used as bean reference unless the post-processor returns a different
078         * wrapper from said post-process callbacks. In other words: Those post-process
079         * callbacks may either eventually expose the same reference or alternatively
080         * return the raw bean instance from those subsequent callbacks (if the wrapper
081         * for the affected bean has been built for a call to this method already,
082         * it will be exposes as final bean reference by default).
083         * <p>The default implementation returns the given {@code bean} as-is.
084         * @param bean the raw bean instance
085         * @param beanName the name of the bean
086         * @return the object to expose as bean reference
087         * (typically with the passed-in bean instance as default)
088         * @throws org.springframework.beans.BeansException in case of errors
089         */
090        default Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
091                return bean;
092        }
093
094}