spring-framework / org.springframework.beans.factory

Package org.springframework.beans.factory

Types

BeanFactoryUtils

abstract class BeanFactoryUtils

Convenience methods operating on bean factories, in particular on the ListableBeanFactory interface.

Returns bean counts, bean names or bean instances, taking into account the nesting hierarchy of a bean factory (which the methods defined on the ListableBeanFactory interface don't, in contrast to the methods defined on the BeanFactory interface).

NamedBean

interface NamedBean

Counterpart of BeanNameAware. Returns the bean name of an object.

This interface can be introduced to avoid a brittle dependence on bean name in objects used with Spring IoC and Spring AOP.

ObjectProvider

interface ObjectProvider<T : Any> : ObjectFactory<T>

A variant of ObjectFactory designed specifically for injection points, allowing for programmatic optionality and lenient not-unique handling.

SmartFactoryBean

interface SmartFactoryBean<T : Any> : FactoryBean<T>

Extension of the FactoryBean interface. Implementations may indicate whether they always return independent instances, for the case where their #isSingleton() implementation returning false does not clearly indicate independent instances.

Plain FactoryBean implementations which do not implement this extended interface are simply assumed to always return independent instances if their #isSingleton() implementation returns false; the exposed object is only accessed on demand.

NOTE: This interface is a special purpose interface, mainly for internal use within the framework and within collaborating frameworks. In general, application-provided FactoryBeans should simply implement the plain FactoryBean interface. New methods might be added to this extended interface even in point releases.

SmartInitializingSingleton

interface SmartInitializingSingleton

Callback interface triggered at the end of the singleton pre-instantiation phase during BeanFactory bootstrap. This interface can be implemented by singleton beans in order to perform some initialization after the regular singleton instantiation algorithm, avoiding side effects with accidental early initialization (e.g. from ListableBeanFactory#getBeansOfType calls). In that sense, it is an alternative to InitializingBean which gets triggered right at the end of a bean's local construction phase.

This callback variant is somewhat similar to org.springframework.context.event.ContextRefreshedEvent but doesn't require an implementation of org.springframework.context.ApplicationListener, with no need to filter context references across a context hierarchy etc. It also implies a more minimal dependency on just the beans package and is being honored by standalone ListableBeanFactory implementations, not just in an org.springframework.context.ApplicationContext environment.

NOTE: If you intend to start/manage asynchronous tasks, preferably implement org.springframework.context.Lifecycle instead which offers a richer model for runtime management and allows for phased startup/shutdown.

Exceptions

BeanCreationNotAllowedException

open class BeanCreationNotAllowedException : BeanCreationException

Exception thrown in case of a bean being requested despite bean creation currently not being allowed (for example, during the shutdown phase of a bean factory).

BeanCurrentlyInCreationException

open class BeanCurrentlyInCreationException : BeanCreationException

Exception thrown in case of a reference to a bean that's currently in creation. Typically happens when constructor autowiring matches the currently constructed bean.

BeanExpressionException

open class BeanExpressionException : FatalBeanException

Exception that indicates an expression evaluation attempt having failed.

BeanInitializationException

open class BeanInitializationException : FatalBeanException

Exception that a bean implementation is suggested to throw if its own factory-aware initialization code fails. BeansExceptions thrown by bean factory methods themselves should simply be propagated as-is.

Note that afterPropertiesSet() or a custom "init-method" can throw any exception.

BeanIsAbstractException

open class BeanIsAbstractException : BeanCreationException

Exception thrown when a bean instance has been requested for a bean definition which has been marked as abstract.

BeanIsNotAFactoryException

open class BeanIsNotAFactoryException : BeanNotOfRequiredTypeException

Exception thrown when a bean is not a factory, but a user tries to get at the factory for the given bean name. Whether a bean is a factory is determined by whether it implements the FactoryBean interface.

BeanNotOfRequiredTypeException

open class BeanNotOfRequiredTypeException : BeansException

Thrown when a bean doesn't match the expected type.

CannotLoadBeanClassException

open class CannotLoadBeanClassException : FatalBeanException

Exception thrown when the BeanFactory cannot load the specified class of a given bean.

FactoryBeanNotInitializedException

open class FactoryBeanNotInitializedException : FatalBeanException

Exception to be thrown from a FactoryBean's getObject() method if the bean is not fully initialized yet, for example because it is involved in a circular reference.

Note: A circular reference with a FactoryBean cannot be solved by eagerly caching singleton instances like with normal beans. The reason is that every FactoryBean needs to be fully initialized before it can return the created bean, while only specific normal beans need to be initialized - that is, if a collaborating bean actually invokes them on initialization instead of just storing the reference.

NoUniqueBeanDefinitionException

open class NoUniqueBeanDefinitionException : NoSuchBeanDefinitionException

Exception thrown when a BeanFactory is asked for a bean instance for which multiple matching candidates have been found when only one matching bean was expected.

UnsatisfiedDependencyException

open class UnsatisfiedDependencyException : BeanCreationException

Exception thrown when a bean depends on other beans or simple properties that were not specified in the bean factory definition, although dependency checking was enabled.

Functions

findAnnotationOnBean

fun <T : Annotation> ListableBeanFactory.findAnnotationOnBean(beanName: String): Annotation?

Extension for ListableBeanFactory.findAnnotationOnBean providing a findAnnotationOnBean<Foo>("foo") variant.

getBean

fun <T : Any> BeanFactory.getBean(): T

Extension for BeanFactory.getBean providing a getBean<Foo>() variant.

fun <T : Any> BeanFactory.getBean(name: String): T

Extension for BeanFactory.getBean providing a getBean<Foo>("foo") variant.

fun <T : Any> BeanFactory.getBean(vararg args: Any): T

Extension for BeanFactory.getBean providing a getBean<Foo>(arg1, arg2) variant.

getBeanNamesForAnnotation

fun <T : Annotation> ListableBeanFactory.getBeanNamesForAnnotation(): Array<out String>

Extension for ListableBeanFactory.getBeanNamesForAnnotation providing a getBeansOfType<Foo>() variant.

getBeanNamesForType

fun <T : Any> ListableBeanFactory.getBeanNamesForType(includeNonSingletons: Boolean = true, allowEagerInit: Boolean = true): Array<out String>

Extension for ListableBeanFactory.getBeanNamesForType providing a getBeanNamesForType<Foo>() variant.

getBeansOfType

fun <T : Any> ListableBeanFactory.getBeansOfType(includeNonSingletons: Boolean = true, allowEagerInit: Boolean = true): Map<String, T>

Extension for ListableBeanFactory.getBeansOfType providing a getBeansOfType<Foo>() variant.

getBeansWithAnnotation

fun <T : Annotation> ListableBeanFactory.getBeansWithAnnotation(): Map<String, Any>

Extension for ListableBeanFactory.getBeansWithAnnotation providing a getBeansWithAnnotation<Foo>() variant.