to access spring context in any part of the application:
 
add this in web.xml:
 
    <listener>
        
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-jms.xml</param-value>
    </context-param>    

add a singleton implementation of ServletContextAware in spring-jms.xml:
 
<bean id="springAppContext" 
class="com.hmacorpmis.soa.objects.SpringAppContext"/>
 
code this in springappcontext:
 
public class SpringAppContext implements ServletContextAware {
 private static ServletContext servletContext;
 
 public void setServletContext(ServletContext arg0) {
  servletContext = arg0;
 }
 
 public static ServletContext getServletContext() {
  return servletContext;
 }
}
 
to get spring bean in any part of the application:
 
      WebApplicationContext webAppContext = 
WebApplicationContextUtils.getRequiredWebApplicationContext(SpringAppContext.getServletContext());
       JmsTemplate template = (JmsTemplate) 
webAppContext.getBean("jmsTemplate");

 
HTH.
 
ErwinP
 

-----Original Message-----
From: Dejan Miloševic [mailto:[EMAIL PROTECTED]
Sent: Friday, December 01, 2006 2:47 AM
To: axis-user@ws.apache.org
Subject: Accessing spring context inside axis2 handlers and CallbackHandler



I've found some solutions for this on the net, all of them trying to access 
current message context with MessageContext.getCurrentMessageContext(), then 
retrieve the ServletContext, then call spring's 
WebApplicationContextUtils.getWebApplicationContext(servletContext). This 
worked well with axis1 and can work in axis2 handlers, since they have 
MessageContext as input parameter for invoke(), but cannot work in 
CallbackHandler because it cannot access current message context 
(MessageContext.getCurrentMessageContext() returns null). I need spring beans 
inside CallbackHandler to access the DB and retrieve the pass for the given 
user. 

 

Has anyone solved this problem?

 

I had one not-best-practise idea to store the ApplicationContext in a static 
field in some class just upon its creation. For instance in 
ContextLoaderListener replace the method contextInitialized with:

 

            public void contextInitialized(ServletContextEvent event) {

                        this.contextLoader = createContextLoader();

                        this.webApplicationContext = 
this.contextLoader.initWebApplicationContext(event.getServletContext());

            }

 

And provide field and getter for this context:

 

            private static WebApplicationContext webApplicationContext;

 

            public static WebApplicationContext getWebApplicationContext()  {

        return webApplicationContext;

}

 

Then anywhere in application (including axis2 handlers and CallbackHandler) I 
can call MyContextLoaderListener.getWebApplicationContext() and the spring 
beans can be accessed.

 

Is this so bad solution? Any better ideas?

Reply via email to