BeanPostProcessor class abstract
A hook that sees every bean as it is created, and may change or replace it before the container hands it out.
This is how cross-cutting features are built: wrapping beans in proxies (AOP, caching, transactions), checking or logging every bean, injecting something special. Applications rarely write one; the starters and annotations bring the ones they need. Both methods may return a different object (a wrapper); the defaults return the bean unchanged.
An application context finds post-processor beans by itself, creates
them before the other beans and orders them by PriorityOrdered,
Ordered or @Order. On a bare factory, register them with
ConfigurableBeanFactory.addBeanPostProcessor. A post-processor only
sees beans created after it was added, so it should depend on few
beans.
// Log each bean when it is ready.
@Component()
@Log()
class TimingPostProcessor extends BeanPostProcessor {
@override
Object postProcessAfterInitialization(Object bean, String beanName) {
log.info('ready: $beanName');
return bean;
}
}
Spring (Java):
@Component
@Slf4j
class TimingPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String name) {
log.info("ready: " + name);
return bean;
}
}
Init callbacks always run on the raw instance, even if postProcessBeforeInitialization returned a wrapper.
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
postProcessAfterInitialization(
Object bean, String beanName) → Object -
Called for each new bean after its init callbacks. The returned
object (a proxy, say) is what the container exposes and injects.
Also applied to
FactoryBeanproducts. -
postProcessBeforeInitialization(
Object bean, String beanName) → Object -
Called for each new bean after injection and the aware callbacks,
before the init callbacks (
@PostConstruct,afterPropertiesSet, init method). Returns the object to go on with, usuallybeanitself. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited