I am interested in knowing if there is an interface that I can use to tell Spring to launch a particular bean up, call its initialization procedure (either as an InitializingBean via afterPropertiesSet (), or via the init method or in some other way), and then throw it away.
My use case is a simple health checker that checks the database for valid values ββat startup for a web application. Although the overhead would be small for our particular bean, considering that a bean for eternity in the application context is pointless, once the bean is initialized, it is no longer needed.
I'm sure there are other use cases for this type of behavior, but I haven't found anything like it in Spring yet.
In particular, I am looking for it in the Java version of Spring, I have access to 3.x and as needed.
EDIT: based on the accepted answer, the following is a simple hack to provide a solution:
public final class NullReturningBeanPostProcessor implements BeanPostProcessor { private List<String> beanNamesToDiscard = new ArrayList<String>(); public NullReturningBeanPostProcessor() { super(); } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (beanNamesToDiscard.contains(beanName)) { return null; } return bean; } public void setBeanNamesToDiscard(List<String> beanNamesToDiscard) { if (beanNamesToDiscard != null) { this.beanNamesToDiscard = beanNamesToDiscard; } } }
Placing the bean mail processor with the appropriate beans for reset in the application context will make them empty and will have the right to garbage collection after they are initialized. The bean configuration metadata, unfortunately, will still remain in the application context.
MetroidFan2002
source share