Created
October 4, 2019 21:52
-
-
Save bdemers/31811e75613b4e10f94fc327d99073ad to your computer and use it in GitHub Desktop.
Wrap Beans
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Bean | |
BeanPostProcessor delegateForFoo() { | |
return new WrappingBeanPostProcessor<>(Foo.class, original -> new MyCustomFoo(original)); | |
} | |
public class WrappingBeanPostProcessor<T> implements BeanPostProcessor { | |
private final Class<T> type; | |
private final UnaryOperator<T> beanCreator; | |
private WrappingBeanPostProcessor(Class<T> type, UnaryOperator<T> beanCreator) { | |
this.type = type; | |
this.beanCreator = beanCreator; | |
} | |
@Override | |
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | |
if (type.isInstance(bean)) { | |
return beanCreator.apply((T) bean); | |
} | |
return bean; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment