I implemented this functionality now through an ApplicationListener, but don't know if this is the best choice.
Code:
public class ApplicationInitListener implements ApplicationListener, ServletContextAware {
private static final Logger log = Logger.getLogger(ApplicationInitListener.class);
private TypeService service;
private ServletContext ctx;
public void setTypeService(TypeService service) {
this.service = service;
}
public void setServletContext(ServletContext ctx) {
this.ctx = ctx;
}
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
createTypeCache();
} else if (event instanceof ContextClosedEvent) {
log.info("[Context] Application terminated.");
}
}
private void createTypeCache() {
ctx.setAttribute(Constants.AKEY_TYPES, service.getTypes());
log.info("[Context] Type cache initialized.");
}
}
And in my applicationContext configuration:
Code:
<bean id="typeService" class="com.service.TypeServiceImpl" autowire="byName"/>
<bean id="applicationInitListener" class="com.portal.listeners.ApplicationInitListener">
<property name="typeService">
<ref bean="typeService"/>
</property>
</bean>
But I can't imagine that this is the only viable solution for this problem. Is this the best approach afterall????
It seems that my context get refreshed twice in application startup, b/c my log displays the following:
INFO [main] (ApplicationInitListener.java:49) - [Context] Type cache initialized.
INFO [main] (ApplicationInitListener.java:49) - [Context] Type cache initialized.
The same with the "application terminated" log message..