When writing an implementation class for the BeanPostProcessor
interface, you usually have the following code:
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof People) {
// do something
}
return bean;
}
The BeanPostProcessor
interface implementation class needs to enter this code each time it passes through each if block, and some of these entries don't do anything to the bean object, but return it directly.
Maybe the BeanPostProcessor
interface can have a supports()
method to judge whether to support processing in the postProcessBeforeInitialization
method.
The improved code is as follows.
public interface BeanPostProcessor {
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
default boolean supports(Object bean, String beanName) {
return true;
}
}
Comment From: sbrannen
Related Issues
- https://github.com/spring-projects/spring-framework/issues/21362#issuecomment-634603682
Comment From: huifer
Some of my thoughts
https://github.com/spring-projects/spring-framework/issues/21362#issuecomment-1158850666
Comment From: snicoll
Thanks for the suggestion but BeanPostProcessor
is a low-level interface and we'd rather not make it more complex at this time. Some improvements are scheduled in a separate issue, see https://github.com/spring-projects/spring-framework/issues/21362#issuecomment-634618632.