Issue
I had this code which works with Spring 3 :
public class WebConsoleStarter extends ContextLoaderListener {
private static final Logger LOG = LoggerFactory.getLogger(WebConsoleStarter.class);
public void contextInitialized(ServletContextEvent event) {
...
super.contextInitialized(event);
initializeWebClient(event.getServletContext() , getContextClassLoader().getCurrentWebApplicationContext());
...
}
private void initializeWebClient(ServletContext servletContext, WebApplicationContext context) {
ConnectionFactory connectionFactory = (ConnectionFactory)context.getBean("connectionFactory");
servletContext.setAttribute(WebClient.CONNECTION_FACTORY_ATTRIBUTE, connectionFactory);
WebClient.initContext(servletContext);
}
}
But this code can't work with Spring4 because the getContextLoader does not exist anymore. I see there is a "getContextClassLoader" method, but seems to be different.
I see in spring documentation this phrase :
"org.springframework.web.context.ContextLoaderListener.getContextLoader() in favor of simply subclassing ContextLoaderListener itself (which extends ContextLoader, as of Spring 3.0)"
But don't understand what I have to do really here in my code :/
Solution
I just replace the old by :
public class WebConsoleStarter extends ContextLoaderListener {
private static final Logger LOG = LoggerFactory.getLogger(WebConsoleStarter.class);
public void contextInitialized(ServletContextEvent event) {
...
super.contextInitialized(event);
initializeWebClient(event.getServletContext() , ContextLoader.getCurrentWebApplicationContext());
...
}
private void initializeWebClient(ServletContext servletContext, WebApplicationContext context) {
ConnectionFactory connectionFactory = (ConnectionFactory)context.getBean("connectionFactory");
servletContext.setAttribute(WebClient.CONNECTION_FACTORY_ATTRIBUTE, connectionFactory);
WebClient.initContext(servletContext);
}
}
Answered By - user2178964
Answer Checked By - Pedro (JavaFixing Volunteer)