You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Dejan Milošević <de...@kombank.com> on 2006/12/01 08:46:39 UTC

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?


Re: Accessing spring context inside axis2 handlers and CallbackHandler

Posted by robert lazarski <ro...@gmail.com>.
>
> 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?

That's pretty much the idea and what most people end up doing. My only
suggestion is to use the ApplicationContextAware interface and create
a bean that holds the reference for your app ctx - just access it via
a static method. That way you don't need to mess around with the
servlet context. See the Axis2 1.1 spring tutorial and the src distro
for some tips.

HTH,
Robert

---------------------------------------------------------------------
To unsubscribe, e-mail: axis-user-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-user-help@ws.apache.org


RE: Accessing spring context inside axis2 handlers and CallbackHandler

Posted by "Pader, Erwin" <Er...@hma.org>.
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:dejan.milosevic@kombank.com]
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?