|
From: Alastair R. <ala...@ph...> - 2003-12-19 11:48:43
|
Regarding wanting to have an axis servlet specific Spring application =
context, I recently had the same problem. Here's how I got round it.
In your web.xml, declare the Spring config(s) you want to be available =
to your web app in a context param and also declare a Spring =
ContextLoaderListener. This will initialise the Spring context and store =
it in your servlet context. For example:
<context-param>
<param-name>contextConfigLocation</param-name>
=
<param-value>/WEB-INF/applicationContext-core.xml,/WEB-INF/applicationCon=
text-web.xml</param-value>
</context-param>
<listener>
=
<listener-class>org.springframework.web.context.ContextLoaderListener</li=
stener-class>
</listener>
Now your POJOs which you expose as Axis web services need to be able to =
get access to the servlet context so they can get at the Spring context. =
You can do this using the Axis MessageContext. In your POJO you would do =
something like:=20
import org.apache.axis.MessageContext;
import org.apache.axis.transport.http.HTTPConstants;
import org.springframework.context.ApplicationContext;
import =
org.springframework.web.context.support.WebApplicationContextUtils;
...
HttpServlet servlet =3D =
(HttpServlet)MessageContext.getCurrentContext().getProperty(HTTPConstants=
.MC_HTTP_SERVLET);
ApplicationContext springContext =3D =
WebApplicationContextUtils.getWebApplicationContext(servlet.getServletCon=
text());
This is the simplest approach, and is OK provided your web services are =
only ever accessed by HTTP. However, I don't like the fact this exposes =
the servlet API to your web service object. So to remove this =
dependency, I created an Axis Handler (HTTPSpringContextHandler extends =
org.apache.axis.handlers.BasicHandler) which I attach to the HTTP =
request flow. This handler's invoke() method is along the lines of:=20
public final void invoke(MessageContext oContext) throws AxisFault
{
if (oContext.isClient()) {
throw new AxisFault("Cannot use server-only handler in a client =
context");
}
/* This handler only deals with SOAP requests, not responses */
if (!context.getPastPivot()) {
HttpServlet servlet =3D =
(HttpServlet)context.getProperty(HTTPConstants.MC_HTTP_SERVLET);
if (servlet !=3D null) {
// Get the Spring context from web app context and add it to the =
Axis message context
ApplicationContext springContext =3D =
WebApplicationContextUtils.getWebApplicationContext(servlet.getServletCon=
text());
context.setProperty("MySpringContextProperty", springContext);
}
}
}
Add this to the HTTP request flow in your Axis WSDD file:=20
<transport name=3D"http">
<requestFlow>
...
<handler type=3D"java:com.blah.HTTPSpringContextHandler"/>
...
</requestFlow>
</transport>
Then in your web service POJO you can retrieve the Spring context from =
the Axis MessageContext:
MessageContext msgContext =3D MessageContext.getCurrentContext();
ApplicationContext springContext =3D =
(ApplicationContext)msgContext.getProperty("MySpringContextProperty");
Also, my web service POJOs are just thin wrappers around Spring beans =
which implement all the functionality, so really the POJOs just become =
'adaptors' for Axis.=20
Hope this helps.
Regards,=20
Al.
> -----Original Message-----
> From: spr...@li...=20
> [mailto:spr...@li...]
> On Behalf Of Michael Young
> Sent: 18 December 2003 21:27
> To: Spr...@li...;=20
> Spr...@li...
> Subject: [Springframework-developer] Spring and axis
>=20
>=20
> I wonder if anyone has integrated axis into Spring Framework,=20
> or if the gurus can shed some lights on this?
>=20
> We are building a web application with some UI components,=20
> but most of the interfaces are Web Services. We are using=20
> Spring and Axis. The problem is Axis needs its own servlet so=20
> that it knows which web services classes to call when a web=20
> services request comes in.
>=20
> So for the axis servlet (and my web services classes that get=20
> called it), I can only get hold of the application wide=20
> application context from Spring framework. I can't seem to=20
> have an axis servlet specific Spring application context.
>=20
> It seems one should be able to use Spring to handle web=20
> services requests as well. Spring can just delegate the=20
> requests to some controllers, and the controllers will invoke Axis?
>=20
> Any thoughts? Thanks! /Michael.
>=20
|