I’m interested to know if there is an interface that I can use to tell Spring to start a particular bean up, invoke its initialization procedure (either as an InitializingBean via afterPropertiesSet(), or via an init-method, or some other way), and then throw it away.
My use case is a simple “sanity-checker” that will check the database for valid values upon startup for a web application. Although the overhead would be small for our particular bean, keeping that bean for all eternity in the application context is pointless, as once the bean had 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 this yet in Spring.
In particular, I’m looking for it in the Java variant of Spring, I have access to 3.x and up as needed.
EDIT: based on the accepted answer, the following is a simple hack to provide the solution:
public final class NullReturningBeanPostProcessor implements BeanPostProcessor {
private List<String> beanNamesToDiscard = new ArrayList<String>();
/**
* Creates a new {@link NullReturningBeanPostProcessor} instance.
*/
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 this bean post processor with the appropriate beans to discard in the application context will make them null, and eligible for garbage collection after they’ve been initialized. The bean configuration metadata, unfortunately, will still remain in the application context.
To achieve this, I would make that bean implement
BeanPostProcessorand then:This should cause the ApplicationContext to discard it but only after it has performed the initialization steps.
Note that you also need to implement
postProcessBeforeInitialization()and there just writereturn bean;.Update: this does NOT work. But not due to MetroidFan2002’s comment (see bellow), but due to another part of the JavaDoc:
So obviously you cannot apply a BeanPostProcessor to itself.
Sorry for the false alarm 🙂