When running a standalone application, there might be a need to have it controlled by web services. This example shows how to configure an embedded Jetty instance to run your Spring Web Services and still allow the embedded Spring Web Services context reference the main application context as a parent.
This example will primarily focus on embedding the Web Services, but it's based on Simple Spring Web Services. Anything not covered here should be explained in the other example.
The Jetty configuration configures what would be considered the server context even though
there isn't anything there except a Person
bean to be shared with the
web application context. The Spring Web Services web application
is created by adding a Context
to Jetty that has the MessageDispatcherServlet
and the Spring configuration file spring-ws-embedded-context.xml in it.
Note | |
---|---|
There isn't a need for a web.xml. The configuration is completely handled by configuring Jetty in Spring. |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- This bean will be available for injection in the child context that the Web Service is in. --> <bean class="org.springbyexample.person.schema.beans.Person" p:id="1" p:firstName="Joe" p:lastName="Smith" /> <context:property-placeholder location="org/springbyexample/ws/embedded/ws.properties"/> <!-- Manually start server after setting parent context. (init-method="start") --> <bean id="jettyServer" class="org.mortbay.jetty.Server" destroy-method="stop"> <property name="threadPool"> <bean id="ThreadPool" class="org.mortbay.thread.concurrent.ThreadPool"> <constructor-arg value="0" /> </bean> </property> <property name="connectors"> <list> <bean id="Connector" class="org.mortbay.jetty.nio.SelectChannelConnector" p:port="${ws.port}" p:maxIdleTime="30000" p:acceptors="2" p:confidentialPort="8443" /> </list> </property> <property name="handlers"> <list> <bean class="org.mortbay.jetty.servlet.Context" p:contextPath="/${ws.context.path}"> <property name="sessionHandler"> <bean class="org.mortbay.jetty.servlet.SessionHandler" /> </property> <property name="servletHandler"> <bean class="org.mortbay.jetty.servlet.ServletHandler"> <property name="servlets"> <list> <bean class="org.mortbay.jetty.servlet.ServletHolder" p:name="spring-ws"> <property name="servlet"> <bean class="org.springframework.ws.transport.http.MessageDispatcherServlet" /> </property> <property name="initParameters"> <map> <entry key="contextConfigLocation" value="classpath:/spring-ws-embedded-context.xml" /> </map> </property> </bean> </list> </property> <property name="servletMappings"> <list> <bean class="org.mortbay.jetty.servlet.ServletMapping" p:servletName="spring-ws" p:pathSpec="/*" /> </list> </property> </bean> </property> </bean> <bean class="org.mortbay.jetty.handler.DefaultHandler" /> <bean class="org.mortbay.jetty.handler.RequestLogHandler" /> </list> </property> </bean> </beans>
This configuration file is the same as any that would be used to configure
a Spring Web Services web application. If this file wasn't explicitly
specified in the MessageDispatcherServlet
, the servlet would look for the
default file /WEB-INF/spring-ws-servlet.xml (/WEB-INF/${servlet.name}-servlet.xml).
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.springbyexample.ws.embedded.service" /> <bean id="person" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition" p:portTypeName="Person" p:locationUri="/personService/" p:requestSuffix="-request" p:responseSuffix="-response"> <property name="schema"> <bean class="org.springframework.xml.xsd.SimpleXsdSchema" p:xsd="classpath:/person.xsd" /> </property> </bean> <bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping"> <description>An endpoint mapping strategy that looks for @Endpoint and @PayloadRoot annotations.</description> </bean> <bean class="org.springframework.ws.server.endpoint.adapter.MarshallingMethodEndpointAdapter"> <description>Enables the MessageDispatchServlet to invoke methods requiring OXM marshalling.</description> <constructor-arg ref="marshaller"/> </bean> <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller" p:contextPath="org.springbyexample.person.schema.beans" /> </beans>