spring-data-mongodb requires a spring ApplicationEventMulticaster to have been initialised before it can be used

112 views Asked by At

I'm using shiro with spring data and mongodb in a web application. I've created a simple mongodb realm which is also configured via spring.

I'm also using the spring DelegatingFilterProxy along with ShiroFilterFactoryBean.

when I try to use it I invariably get: java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: Root WebApplicationContext

the problem is that spring-data-mongodb requires a spring ApplicationEventMulticaster to have been initialised before it can be used.

ShiroFilterFactoryBean is a beanPostProcessor, and as such, during initialisation, spring attempts to configure its realms(and hence my realm and spring data mongo based userDao). it fails because ApplicationEventMulticaster has not yet been created.

the relevant part in spring. org.springframework.context.support.AbstractApplicationContext.refresh()

... 
try { 
     ... 
     registerBeanPostProcessors(beanFactory); 

     // Initialize message source for this context. 
     InitMessageSource(); 

     // Initialize event multicaster for this context. 
     initApplicationEventMulticaster(); 

     // Initialize other special beans in specific context subclasses. 
     onRefresh(); 
     registerListeners(); 
     ... 
    } 

you can see that the BeanPostProcessors are setup before initApplicationEventMulticaster() is ever called.

so my query is, whats the best way to solve this? whats the sanest option?

Thanks

1

There are 1 answers

2
AntJavaDev On

you said something about WebApplicationContext , so i suppose you have a *-servlet.xml that configures the specified controller . Spring allows you to have a global context that spring will be responsible to pass it to all other WebApplicationContexts , so you have to define the DAO logic that will be global in an applicationContext.xml and let spring do the others