spring-framework / org.springframework.beans.factory.config / InstantiationAwareBeanPostProcessorAdapter

InstantiationAwareBeanPostProcessorAdapter

abstract class InstantiationAwareBeanPostProcessorAdapter : SmartInstantiationAwareBeanPostProcessor

Adapter that implements all methods on SmartInstantiationAwareBeanPostProcessor as no-ops, which will not change normal processing of each bean instantiated by the container. Subclasses may override merely those methods that they are actually interested in.

Note that this base class is only recommendable if you actually require InstantiationAwareBeanPostProcessor functionality. If all you need is plain BeanPostProcessor functionality, prefer a straight implementation of that (simpler) interface.

Author
Rod Johnson

Author
Juergen Hoeller

Since
2.0

Constructors

<init>

InstantiationAwareBeanPostProcessorAdapter()

Adapter that implements all methods on SmartInstantiationAwareBeanPostProcessor as no-ops, which will not change normal processing of each bean instantiated by the container. Subclasses may override merely those methods that they are actually interested in.

Note that this base class is only recommendable if you actually require InstantiationAwareBeanPostProcessor functionality. If all you need is plain BeanPostProcessor functionality, prefer a straight implementation of that (simpler) interface.

Functions

determineCandidateConstructors

open fun determineCandidateConstructors(beanClass: Class<*>, beanName: String): Array<Constructor<*>>

getEarlyBeanReference

open fun getEarlyBeanReference(bean: Any, beanName: String): Any

postProcessAfterInitialization

open fun postProcessAfterInitialization(bean: Any, beanName: String): Any

postProcessAfterInstantiation

open fun postProcessAfterInstantiation(bean: Any, beanName: String): Boolean

postProcessBeforeInitialization

open fun postProcessBeforeInitialization(bean: Any, beanName: String): Any

postProcessBeforeInstantiation

open fun postProcessBeforeInstantiation(beanClass: Class<*>, beanName: String): Any

postProcessPropertyValues

open fun postProcessPropertyValues(pvs: PropertyValues, pds: Array<PropertyDescriptor>, bean: Any, beanName: String): PropertyValues

predictBeanType

open fun predictBeanType(beanClass: Class<*>, beanName: String): Class<*>

Inheritors

AutowiredAnnotationBeanPostProcessor

open class AutowiredAnnotationBeanPostProcessor : InstantiationAwareBeanPostProcessorAdapter, MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware

org.springframework.beans.factory.config.BeanPostProcessor implementation that autowires annotated fields, setter methods and arbitrary config methods. Such members to be injected are detected through a Java 5 annotation: by default, Spring's Autowired and Value annotations.

Also supports JSR-330's javax.inject.Inject annotation, if available, as a direct alternative to Spring's own @Autowired.

Only one constructor (at max) of any given bean class may carry this annotation with the 'required' parameter set to true, indicating the constructor to autowire when used as a Spring bean. If multiple non-required constructors carry the annotation, they will be considered as candidates for autowiring. The constructor with the greatest number of dependencies that can be satisfied by matching beans in the Spring container will be chosen. If none of the candidates can be satisfied, then a default constructor (if present) will be used. An annotated constructor does not have to be public.

Fields are injected right after construction of a bean, before any config methods are invoked. Such a config field does not have to be public.

Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container. Bean property setter methods are effectively just a special case of such a general config method. Config methods do not have to be public.

Note: A default AutowiredAnnotationBeanPostProcessor will be registered by the "context:annotation-config" and "context:component-scan" XML tags. Remove or turn off the default annotation configuration there if you intend to specify a custom AutowiredAnnotationBeanPostProcessor bean definition.

NOTE: Annotation injection will be performed before XML injection; thus the latter configuration will override the former for properties wired through both approaches.

In addition to regular injection points as discussed above, this post-processor also handles Spring's Lookup annotation which identifies lookup methods to be replaced by the container at runtime. This is essentially a type-safe version of getBean(Class, args) and getBean(String, args), See Lookup for details.

RequiredAnnotationBeanPostProcessor

open class RequiredAnnotationBeanPostProcessor : InstantiationAwareBeanPostProcessorAdapter, MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware

org.springframework.beans.factory.config.BeanPostProcessor implementation that enforces required JavaBean properties to have been configured. Required bean properties are detected through a Java 5 annotation: by default, Spring's Required annotation.

The motivation for the existence of this BeanPostProcessor is to allow developers to annotate the setter properties of their own classes with an arbitrary JDK 1.5 annotation to indicate that the container must check for the configuration of a dependency injected value. This neatly pushes responsibility for such checking onto the container (where it arguably belongs), and obviates the need (in part) for a developer to code a method that simply checks that all required properties have actually been set.

Please note that an 'init' method may still need to implemented (and may still be desirable), because all that this class does is enforce that a 'required' property has actually been configured with a value. It does not check anything else... In particular, it does not check that a configured value is not null.

Note: A default RequiredAnnotationBeanPostProcessor will be registered by the "context:annotation-config" and "context:component-scan" XML tags. Remove or turn off the default annotation configuration there if you intend to specify a custom RequiredAnnotationBeanPostProcessor bean definition.

ScriptFactoryPostProcessor

open class ScriptFactoryPostProcessor : InstantiationAwareBeanPostProcessorAdapter, BeanClassLoaderAware, BeanFactoryAware, ResourceLoaderAware, DisposableBean, Ordered

org.springframework.beans.factory.config.BeanPostProcessor that handles org.springframework.scripting.ScriptFactory definitions, replacing each factory with the actual scripted Java object generated by it.

This is similar to the org.springframework.beans.factory.FactoryBean mechanism, but is specifically tailored for scripts and not built into Spring's core container itself but rather implemented as an extension.

NOTE: The most important characteristic of this post-processor is that constructor arguments are applied to the org.springframework.scripting.ScriptFactory instance while bean property values are applied to the generated scripted object. Typically, constructor arguments include a script source locator and potentially script interfaces, while bean property values include references and config values to inject into the scripted object itself.

The following ScriptFactoryPostProcessor will automatically be applied to the two org.springframework.scripting.ScriptFactory definitions below. At runtime, the actual scripted objects will be exposed for "bshMessenger" and "groovyMessenger", rather than the org.springframework.scripting.ScriptFactory instances. Both of those are supposed to be castable to the example's Messenger interfaces here.

<bean class="org.springframework.scripting.support.ScriptFactoryPostProcessor"/> <bean id="bshMessenger" class="org.springframework.scripting.bsh.BshScriptFactory"> <constructor-arg value="classpath:mypackage/Messenger.bsh"/> <constructor-arg value="mypackage.Messenger"/> <property name="message" value="Hello World!"/> </bean> <bean id="groovyMessenger" class="org.springframework.scripting.groovy.GroovyScriptFactory"> <constructor-arg value="classpath:mypackage/Messenger.groovy"/> <property name="message" value="Hello World!"/> </bean>

NOTE: Please note that the above excerpt from a Spring XML bean definition file uses just the <bean/>-style syntax (in an effort to illustrate using the ScriptFactoryPostProcessor itself). In reality, you would never create a <bean/> definition for a ScriptFactoryPostProcessor explicitly; rather you would import the tags from the 'lang' namespace and simply create scripted beans using the tags in that namespace... as part of doing so, a ScriptFactoryPostProcessor will implicitly be created for you.

The Spring reference documentation contains numerous examples of using tags in the 'lang' namespace; by way of an example, find below a Groovy-backed bean defined using the 'lang:groovy' tag.

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang"> <!-- this is the bean definition for the Groovy-backed Messenger implementation --> <lang:groovy id="messenger" script-source="classpath:Messenger.groovy"> <lang:property name="message" value="I Can Do The Frug" /> </lang:groovy> <!-- an otherwise normal bean that will be injected by the Groovy-backed Messenger --> <bean id="bookingService" class="x.y.DefaultBookingService"> <property name="messenger" ref="messenger" /> </bean> </beans>