| <!DOCTYPE html> |
| <!-- |
| | Generated by Apache Maven Doxia Site Renderer 1.7.4 at 2018-05-19 |
| | Rendered using Apache Maven Fluido Skin 1.6 |
| --> |
| <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| <head> |
| <meta charset="UTF-8" /> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| <meta name="Date-Revision-yyyymmdd" content="20180519" /> |
| <meta http-equiv="Content-Language" content="en" /> |
| <title>Apache Axis2 – Axis2 integration with the Spring Framework</title> |
| <link rel="stylesheet" href="../css/apache-maven-fluido-1.6.min.css" /> |
| <link rel="stylesheet" href="../css/site.css" /> |
| <link rel="stylesheet" href="../css/print.css" media="print" /> |
| <script type="text/javascript" src="../js/apache-maven-fluido-1.6.min.js"></script> |
| <meta http-equiv="content-type" content="" /> </head> |
| <body class="topBarDisabled"> |
| <div class="container-fluid"> |
| <div id="banner"> |
| <div class="pull-left"><a href="http://www.apache.org/" id="bannerLeft"><img src="http://www.apache.org/images/asf_logo_wide.png" alt="Apache Axis2"/></a></div> |
| <div class="pull-right"><a href=".././" id="bannerRight"><img src="../images/axis.jpg" /></a></div> |
| <div class="clear"><hr/></div> |
| </div> |
| |
| <div id="breadcrumbs"> |
| <ul class="breadcrumb"> |
| <li id="publishDate">Last Published: 2018-05-19<span class="divider">|</span> |
| </li> |
| <li id="projectVersion">Version: 1.7.8<span class="divider">|</span></li> |
| <li class=""><a href="http://www.apache.org" class="externalLink" title="Apache">Apache</a><span class="divider">/</span></li> |
| <li class=""><a href="../index.html" title="Axis2/Java">Axis2/Java</a><span class="divider">/</span></li> |
| <li class="active ">Axis2 integration with the Spring Framework</li> |
| </ul> |
| </div> |
| <div class="row-fluid"> |
| <div id="leftColumn" class="span2"> |
| <div class="well sidebar-nav"> |
| <ul class="nav nav-list"> |
| <li class="nav-header">Axis2/Java</li> |
| <li><a href="../index.html" title="Home"><span class="none"></span>Home</a> </li> |
| <li><a href="../download.html" title="Downloads"><span class="none"></span>Downloads</a> </li> |
| <li><a href="javascript:void(0)" title="Release Notes"><span class="icon-chevron-down"></span>Release Notes</a> |
| <ul class="nav nav-list"> |
| <li><a href="../release-notes/1.6.1.html" title="1.6.1"><span class="none"></span>1.6.1</a> </li> |
| <li><a href="../release-notes/1.6.2.html" title="1.6.2"><span class="none"></span>1.6.2</a> </li> |
| <li><a href="../release-notes/1.6.3.html" title="1.6.3"><span class="none"></span>1.6.3</a> </li> |
| <li><a href="../release-notes/1.6.4.html" title="1.6.4"><span class="none"></span>1.6.4</a> </li> |
| <li><a href="../release-notes/1.7.0.html" title="1.7.0"><span class="none"></span>1.7.0</a> </li> |
| <li><a href="../release-notes/1.7.1.html" title="1.7.1"><span class="none"></span>1.7.1</a> </li> |
| <li><a href="../release-notes/1.7.2.html" title="1.7.2"><span class="none"></span>1.7.2</a> </li> |
| <li><a href="../release-notes/1.7.3.html" title="1.7.3"><span class="none"></span>1.7.3</a> </li> |
| <li><a href="../release-notes/1.7.4.html" title="1.7.4"><span class="none"></span>1.7.4</a> </li> |
| <li><a href="../release-notes/1.7.5.html" title="1.7.5"><span class="none"></span>1.7.5</a> </li> |
| <li><a href="../release-notes/1.7.6.html" title="1.7.6"><span class="none"></span>1.7.6</a> </li> |
| <li><a href="../release-notes/1.7.7.html" title="1.7.7"><span class="none"></span>1.7.7</a> </li> |
| <li><a href="../release-notes/1.7.8.html" title="1.7.8"><span class="none"></span>1.7.8</a> </li> |
| </ul> |
| </li> |
| <li><a href="../modules/index.html" title="Modules"><span class="none"></span>Modules</a> </li> |
| <li><a href="../tools/index.html" title="Tools"><span class="none"></span>Tools</a> </li> |
| <li class="nav-header">Documentation</li> |
| <li><a href="../docs/toc.html" title="Table of Contents"><span class="none"></span>Table of Contents</a> </li> |
| <li><a href="../docs/installationguide.html" title="Installation Guide"><span class="none"></span>Installation Guide</a> </li> |
| <li><a href="../docs/quickstartguide.html" title="QuickStart Guide"><span class="none"></span>QuickStart Guide</a> </li> |
| <li><a href="../docs/userguide.html" title="User Guide"><span class="none"></span>User Guide</a> </li> |
| <li><a href="../docs/jaxws-guide.html" title="JAXWS Guide"><span class="none"></span>JAXWS Guide</a> </li> |
| <li><a href="../docs/pojoguide.html" title="POJO Guide"><span class="none"></span>POJO Guide</a> </li> |
| <li class="active"><a href="#"><span class="none"></span>Spring Guide</a> |
| </li> |
| <li><a href="../docs/webadminguide.html" title="Web Administrator's Guide"><span class="none"></span>Web Administrator's Guide</a> </li> |
| <li><a href="../docs/migration.html" title="Migration Guide (from Axis1)"><span class="none"></span>Migration Guide (from Axis1)</a> </li> |
| <li class="nav-header">Resources</li> |
| <li><a href="../faq.html" title="FAQ"><span class="none"></span>FAQ</a> </li> |
| <li><a href="../articles.html" title="Articles"><span class="none"></span>Articles</a> </li> |
| <li><a href="http://wiki.apache.org/ws/FrontPage/Axis2/" class="externalLink" title="Wiki"><span class="none"></span>Wiki</a> </li> |
| <li><a href="../refLib.html" title="Reference Library"><span class="none"></span>Reference Library</a> </li> |
| <li><a href="../apidocs/index.html" title="Online Java Docs"><span class="none"></span>Online Java Docs</a> </li> |
| <li class="nav-header">Get Involved</li> |
| <li><a href="../overview.html" title="Overview"><span class="none"></span>Overview</a> </li> |
| <li><a href="../svn.html" title="Checkout the Source"><span class="none"></span>Checkout the Source</a> </li> |
| <li><a href="../mail-lists.html" title="Mailing Lists"><span class="none"></span>Mailing Lists</a> </li> |
| <li><a href="../release-process.html" title="Release Process"><span class="none"></span>Release Process</a> </li> |
| <li><a href="../guidelines.html" title="Developer Guidelines"><span class="none"></span>Developer Guidelines</a> </li> |
| <li><a href="../siteHowTo.html" title="Build the Site"><span class="none"></span>Build the Site</a> </li> |
| <li class="nav-header">Project Information</li> |
| <li><a href="../team-list.html" title="Project Team"><span class="none"></span>Project Team</a> </li> |
| <li><a href="../issue-tracking.html" title="Issue Tracking"><span class="none"></span>Issue Tracking</a> </li> |
| <li><a href="http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/" class="externalLink" title="Source Code"><span class="none"></span>Source Code</a> </li> |
| <li><a href="../thanks.html" title="Acknowledgements"><span class="none"></span>Acknowledgements</a> </li> |
| <li class="nav-header">Apache</li> |
| <li><a href="http://www.apache.org/licenses/LICENSE-2.0.html" class="externalLink" title="License"><span class="none"></span>License</a> </li> |
| <li><a href="http://www.apache.org/foundation/sponsorship.html" class="externalLink" title="Sponsorship"><span class="none"></span>Sponsorship</a> </li> |
| <li><a href="http://www.apache.org/foundation/thanks.html" class="externalLink" title="Thanks"><span class="none"></span>Thanks</a> </li> |
| <li><a href="http://www.apache.org/security/" class="externalLink" title="Security"><span class="none"></span>Security</a> </li> |
| </ul> |
| <hr /> |
| <div id="poweredBy"> |
| <div class="clear"></div> |
| <div class="clear"></div> |
| <div class="clear"></div> |
| <div class="clear"></div> |
| <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy"><img class="builtBy" alt="Built by Maven" src="../images/logos/maven-feather.png" /></a> |
| </div> |
| </div> |
| </div> |
| <div id="bodyColumn" class="span10" > |
| <html> |
| |
| |
| <h1>Axis2 Integration with the Spring Framework</h1> |
| |
| |
| <p>This document is a guide on how to use Axis2 with the Spring Framework</p> |
| |
| |
| <div class="section"> |
| <h2><a name="Content"></a>Content</h2> |
| |
| <ul> |
| |
| <li><a href="#a1">Introduction</a></li> |
| |
| <li><a href="#a2">Configuring Axis2 to be Spring Aware</a> |
| |
| <ul> |
| |
| <li><a href="#a21">Programming Model</a></li> |
| |
| <li><a href="#a22">Simple Spring Config Example</a></li> |
| |
| <li><a href="#a23">With a ServletContext</a></li> |
| |
| <li><a href="#a24">Without a ServletContext</a></li> |
| |
| <li><a href="#a25">Putting it All Together</a></li> |
| |
| <li><a href="#a26">Spring Inside an AAR </a> |
| |
| <ul> |
| |
| <li><a href="#a261">The Spring Inside an AAR Layout</a></li> |
| |
| <li><a href="#a262">The Spring Inside an AAR init Class</a></li> |
| |
| <li><a href="#a263">Known Issues Running Spring Inside the |
| AAR</a></li> |
| </ul> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| <a name="a1"></a> |
| |
| |
| <div class="section"> |
| <h2><a name="Introduction"></a>Introduction</h2> |
| |
| |
| <p>Axis2 and Spring integration takes place when Spring supplies one of its |
| pre-loaded beans to the Axis2 Message Receiver defined in the AAR |
| services.xml. Axis2 typically uses reflection to instantiate the ServiceClass |
| defined in the services.xml used by the Message Receiver. Alternatively, you |
| can define a ServiceObjectSupplier that will supply the Object.</p> |
| |
| |
| <p>This guide describes how to use two separate ServiceObjectSupplier classes |
| that are a part of the Axis2 standard distribution - one for use with a |
| ServletContext, and one without. Configuring Axis2 with a ServletContext is |
| simpler than without, and is recommended for most use cases. Without a |
| ServletContext, ie, Spring inside the AAR, requires an extra Spring bean |
| and is considered an advanced use case. Once configured, the Web service itself |
| acts like any other Spring wired bean. These Spring beans can be loaded any way |
| desired as Axis2 has no configuration file dependencies from Spring. Spring |
| versions 1.2.6, 1.2.8 and 2.0 have been tested, but probably any version |
| would work as only the core functionality is required.</p> |
| |
| |
| <p>This guide assumes some basic knowledge of Axis2. See the <a href="userguide.html">User's Guide</a> for more information.</p> |
| <a name="a2"></a> |
| |
| |
| <div class="section"> |
| <h3><a name="Programming_Model"></a>Programming Model</h3> |
| |
| |
| <p>From an Axis2 standpoint, two hooks are needed to be placed into the AAR |
| services.xml - the ServiceObjectSupplier that hooks Axis2 and Spring |
| together, and the name of the Spring bean that Axis2 will use as the service. |
| All Message Receivers are currently supported, as would be any Message |
| Receiver that extends org.apache.axis2.receivers.AbstractMessageReceiver .</p> |
| <a name="a22"></a> |
| |
| </div> |
| <div class="section"> |
| <h3><a name="Simple_Spring_Config_Example"></a>Simple Spring Config Example</h3> |
| |
| |
| <p>For the purpose of this example, we'll configure Spring via a WAR file's |
| web.xml. Let's add a context-param and a listener:</p> |
| |
| <div> |
| <pre><listener> |
| <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> |
| </listener> |
| <context-param> |
| <param-name>contextConfigLocation</param-name> |
| <param-value>/WEB-INF/applicationContext.xml</param-value> |
| </context-param></pre></div> |
| |
| |
| <p>Next we will show two examples of Spring's /WEB-INF/applicationContext.xml |
| referenced in the web.xml listener - one using a ServletContext, and one |
| without.</p> |
| <a name="a23"></a> |
| |
| </div> |
| <div class="section"> |
| <h3><a name="With_a_ServletContext"></a>With a ServletContext</h3> |
| |
| |
| <p>This example (with a ServletContext) applicationContext.xml should be |
| familiar to any Spring user:</p> |
| |
| <div> |
| <pre><?xml version="1.0" encoding="UTF-8"?> |
| <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> |
| |
| <beans> |
| <!-- Axis2 Web Service, but to Spring, its just another bean that has dependencies --> |
| <bean id="springAwareService" class="spring.SpringAwareService"> |
| <property name="myBean" ref="myBean"/> |
| </bean> |
| |
| <!-- just another bean / interface with a wired implementation, that's injected by Spring |
| into the Web Service --> |
| <bean id="myBean" class="spring.MyBeanImpl"> |
| <property name="val" value="Spring, emerge thyself" /> |
| </bean> |
| </beans></pre></div> |
| |
| |
| <p>If the service is running in a Servlet Container, i.e., Axis2 will be able |
| to get a hold of the ServletContext, the services.xml for the example would |
| be using SpringServletContextObjectSupplier such as:</p> |
| |
| <div> |
| <pre> <service name="SpringAwareService"> |
| <description> |
| simple spring example |
| </description> |
| <parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier</parameter> |
| <parameter name="SpringBeanName">springAwareService</parameter> |
| <operation name="getValue"> |
| <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/> |
| </operation> |
| </service> </pre></div> |
| |
| |
| <p>While the above example uses RawXMLINOutMessageReceiver as its |
| messageReceiver class, all Message Receivers are currently supported, as |
| would be any Message Receiver that extends |
| org.apache.axis2.receivers.AbstractMessageReceiver .</p> |
| <a name="a24"></a> |
| |
| </div> |
| <div class="section"> |
| <h3><a name="Without_a_ServletContext"></a>Without a ServletContext</h3> |
| |
| |
| <p>In case Axis2 can't get a ServletContext, (i.e., uses a different |
| transport or is running Axis2 inside the AAR etc,) you have the option of |
| defining a bean that takes advantage of Spring's internal abilities |
| (ApplicationContextAware interface, specifically) to provide an Application |
| Context to Axis2, with a bean ref 'applicationContext' :</p> |
| |
| <div> |
| <pre><?xml version="1.0" encoding="UTF-8"?> |
| <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> |
| |
| <beans> |
| <!-- Configure spring to give a hook to axis2 without a ServletContext --> |
| <bean id="applicationContext" |
| class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" /> |
| |
| <!-- Axis2 Web Service, but to Spring, its just another bean that has dependencies --> |
| <bean id="springAwareService" |
| class="spring.SpringAwareService"> |
| <property name="myBean" ref="myBean" /> |
| </bean> |
| |
| <!-- just another bean with a wired implementation, that's injected by Spring |
| into the Web Service --> |
| <bean id="myBean" |
| class="spring.MyBeanImpl"> |
| <property name="val" value="Spring, emerge thyself" /> |
| </bean> |
| </beans></pre></div> |
| |
| |
| <p>If the service is <b>not</b> running in a Servlet Container, |
| i.e., Axis2 will not be able to get a hold of ServletContext or you prefer |
| not to, the services.xml for the example will be using |
| SpringAppContextAwareObjectSupplier such as:</p> |
| |
| <div> |
| <pre> <service name="SpringAwareService"> |
| <description> |
| simple spring example |
| </description> |
| <parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier</parameter> |
| <parameter name="SpringBeanName">springAwareService</parameter> |
| <operation name="getValue"> |
| <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/> |
| </operation> |
| </service> </pre></div> |
| |
| |
| <p>While the above example uses RawXMLINOutMessageReceiver as its |
| messageReceiver class, all Message Receivers are currently supported, as |
| would be any Message Receiver that extends |
| org.apache.axis2.receivers.AbstractMessageReceiver .</p> |
| |
| |
| <p>In an environment <b>without a ServletContext</b>, one way you |
| could load the applicationContext.xml file is in a place that will be run |
| once. Upon start-up, execute the following:</p> |
| |
| <div> |
| <pre>import org.springframework.context.support.ClassPathXmlApplicationContext; |
| |
| public void createSpringAppCtx(ClassLoader cl) |
| throws Exception { |
| |
| ClassPathXmlApplicationContext ctx = new |
| ClassPathXmlApplicationContext(new String[] {Constants.MY_PATH + |
| "spring/applicationContext.xml"}, false); |
| ctx.setClassLoader(cl); |
| ctx.refresh();</pre></div> |
| } <a name="a25"></a> |
| |
| </div> |
| <div class="section"> |
| <h3><a name="Putting_It_All_Together"></a>Putting It All Together</h3> |
| |
| |
| <p>From here, it's just standard Axis2 coding. Only now the service has |
| Spring wiring capabilities. The implementation is the same whether using |
| either SpringServletContextObjectSupplier or |
| SpringAppContextAwareObjectSupplier. The service is as follows:</p> |
| |
| <div> |
| <pre>package spring; |
| |
| import org.apache.axiom.om.OMAbstractFactory; |
| import org.apache.axiom.om.OMElement; |
| import org.apache.axiom.om.OMFactory; |
| import org.apache.axiom.om.OMNamespace; |
| import org.apache.axiom.om.OMText; |
| |
| public class SpringAwareService { |
| |
| private MyBean myBean = null; |
| |
| //spring 'injects' this implementation |
| public void setMyBean(MyBean myBean) { |
| this.myBean = myBean; |
| } |
| |
| // The web service |
| public OMElement getValue(OMElement ignore) { |
| OMFactory factory= |
| OMAbstractFactory.getOMFactory(); |
| OMNamespace payloadNs= factory.createOMNamespace( |
| "http://springExample.org/example1", "example1"); |
| OMElement payload = |
| factory.createOMElement("string", payloadNs); |
| OMText response = factory.createOMText(this.myBean.emerge()); |
| payload.addChild(response); |
| return payload; |
| } |
| } </pre></div> |
| |
| |
| <p>For those who are new to Spring, one of the ideas is that you program an |
| Interface, as the implementation is pluggable. This idea is referenced in the |
| Spring config file above. Below is the interface:</p> |
| |
| <div> |
| <pre>package spring; |
| |
| /** Interface for Spring aware Bean */ |
| public interface MyBean { |
| String emerge(); |
| }</pre></div> |
| |
| |
| <p>Here's the implementation:</p> |
| |
| <div> |
| <pre>/** Spring wired implementation */ |
| public class MyBeanImpl implements MyBean { |
| |
| String str = null; |
| // spring 'injects' this value |
| public void setVal(String s) { |
| str = s; |
| } |
| // web service gets this value |
| public String emerge() { |
| return str; |
| } |
| }</pre></div> |
| |
| |
| <p>Lastly here's the client - not really necessary for the example, other |
| than for completeness:</p> |
| |
| <div> |
| <pre>package client; |
| |
| import java.io.StringWriter; |
| |
| import javax.xml.stream.XMLOutputFactory; |
| |
| import org.apache.axiom.om.OMAbstractFactory; |
| import org.apache.axiom.om.OMElement; |
| import org.apache.axiom.om.OMFactory; |
| import org.apache.axiom.om.OMNamespace; |
| import org.apache.axis2.addressing.EndpointReference; |
| import org.apache.axis2.client.Options; |
| import org.apache.axis2.client.ServiceClient; |
| |
| public class TestClient { |
| private static EndpointReference targetEPR = |
| new EndpointReference( |
| "http://localhost:8080/axis2/services/SpringAwareService"); |
| |
| /** |
| * Simple axis2 client. |
| * |
| * @param args Main |
| */ |
| public static void main(String[] args) { |
| try { |
| OMFactory factory = OMAbstractFactory.getOMFactory(); |
| OMNamespace omNs = factory.createOMNamespace( |
| "http://springExample.org/example1", "example1"); |
| |
| OMElement method = factory.createOMElement("getValue", omNs); |
| OMElement value = factory.createOMElement("Text", omNs); |
| value.addChild(factory.createOMText(value, "Some String ")); |
| method.addChild(value); |
| |
| ServiceClient serviceClient = new ServiceClient(); |
| |
| Options options = new Options(); |
| serviceClient.setOptions(options); |
| options.setTo(targetEPR); |
| |
| //Blocking invocation |
| OMElement result = serviceClient.sendReceive(method); |
| |
| StringWriter writer = new StringWriter(); |
| result.serialize(XMLOutputFactory.newInstance() |
| .createXMLStreamWriter(writer)); |
| writer.flush(); |
| |
| System.out.println("Response: " + writer.toString()); |
| } catch (Exception ex) { |
| ex.printStackTrace(); |
| } |
| } |
| } </pre></div> |
| |
| |
| <p>The examples above assume that both the spring framework .jar and the |
| axis2-spring-*.jar are under WEB-INF/lib. In such a case, the classes shown |
| in this tutorial need to be placed in a JAR under WEB-INF/lib. In this |
| example the JAR layout is:</p> |
| |
| <div> |
| <pre>./mySpring.jar |
| ./META-INF |
| ./META-INF/MANIFEST.MF |
| ./spring |
| ./spring/MyBean.class |
| ./spring/MyBeanImpl.class |
| ./spring/SpringAwareService.class</pre></div> |
| |
| <p>Since all the user classes are in mySpring.jar in this example, the AAR |
| merely contains the services.xml file:</p> |
| |
| <div> |
| <pre>./springExample.aar |
| ./META-INF |
| ./META-INF/MANIFEST.MF |
| ./META-INF/services.xml</pre></div> |
| |
| <p>To run this example, make sure you have the axis2-spring*.jar that comes |
| from the axis2-std-*-bin distro in the server side WEB-INF/lib, as well as |
| the appropriate Spring jar - most will use the full spring.jar, but the |
| minimum requirements are spring-core, spring-beans, spring-context, and |
| spring-web. When running the client, you should see this output:</p> |
| |
| <div> |
| <pre>Response: <example1:string xmlns:example1="http://springExample.org/example1" |
| xmlns:tns="http://ws.apache.org/axis2">Spring, emerge thyself</example1:string></pre></div> |
| <a name="a26"></a> |
| |
| </div> |
| <div class="section"> |
| <h3><a name="Spring_Inside_an_AAR"></a>Spring Inside an AAR</h3> |
| |
| |
| <p>Axis2 users frequently want to run Spring inside the AAR. Here we show you |
| how it is done. There are four points to be aware of:</p> |
| |
| |
| <p>(A) You need to configure Spring to use the Axis2 Service Classloader. See |
| the <a href="#a263">Known issues running Spring inside the AAR</a> area.</p> |
| |
| |
| <p>(B) It's up to you to load Spring, though we give an example below.</p> |
| |
| |
| <p>(C) For reasons such as classloader isolation, the |
| SpringAppContextAwareObjectSupplier is the best choice.</p> |
| |
| |
| <p>(D) The springframework .jar and axis2-spring-*.jar will be placed inside |
| the AAR under the lib directory. Please <b>move</b> the |
| axis2-spring-*.jar from WEB-INF/lib to inside the AAR, as shown below - it |
| will <b>not</b> work otherwise.</p> |
| |
| <ul> |
| |
| <li><b><a name="a261"></a>The Spring inside an AAR layout</b></li> |
| </ul> |
| |
| <div> |
| <pre>./springExample.aar |
| ./META-INF |
| ./META-INF/MANIFEST.MF |
| ./META-INF/services.xml |
| ./applicationContext.xml |
| ./lib |
| ./lib/axis2-spring-1.4.jar |
| ./lib/spring.jar |
| ./spring |
| ./spring/MyBean.class |
| ./spring/MyBeanImpl.class |
| ./spring/SpringAwareService.class |
| ./spring/SpringInit.class </pre></div> |
| |
| <p>As explained in the <a href="#a24">Without a ServletContext</a> section, |
| the 'Spring inside an AAR' config needs to hook Axis2 and Spring together via |
| a Spring bean. Place the following in your Spring config file:</p> |
| |
| <div> |
| <pre> <!-- Configure spring to give a hook to axis2 without a ServletContext --> |
| <bean id="applicationContext" |
| class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" /></pre></div> |
| |
| <ul> |
| |
| <li><b><a name="a262"></a>The Spring inside an AAR init |
| class</b></li> |
| </ul> |
| |
| |
| <p>One way to initialize Spring inside the AAR is to use the |
| org.apache.axis2.engine.ServiceLifeCycle interface. Here we give an |
| example:</p> |
| |
| <div> |
| <pre>package spring; |
| |
| import org.apache.axis2.engine.ServiceLifeCycle; |
| import org.apache.axis2.context.ConfigurationContext; |
| import org.apache.axis2.description.AxisService; |
| import org.springframework.context.support.ClassPathXmlApplicationContext; |
| |
| public class SpringInit implements ServiceLifeCycle { |
| |
| /** |
| * This will be called during the deployement time of the service. |
| * irrespective |
| * of the service scope this method will be called |
| */ |
| public void startUp(ConfigurationContext ignore, AxisService service) { |
| |
| try { |
| System.out.println("Starting spring init"); |
| ClassLoader classLoader = service.getClassLoader(); |
| ClassPathXmlApplicationContext appCtx = new |
| ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"}, false); |
| appCtx.setClassLoader(classLoader); |
| appCtx.refresh(); |
| System.out.println("spring loaded"); |
| } catch (Exception ex) { |
| ex.printStackTrace(); |
| } |
| } |
| |
| /** |
| * This will be called during the system shut down time. |
| * irrespective |
| * of the service scope this method will be called |
| */ |
| public void shutDown(ConfigurationContext ctxIgnore, AxisService ignore) { |
| } |
| }</pre></div> |
| |
| <p>Here's the services.xml that now includes SpringInit and the |
| SpringAwareService shown above. There is also the composite parameter which |
| is needed when loading Spring in the AAR - see the <a href="#a263">Known |
| issues running Spring inside the AAR</a> area.</p> |
| |
| <div> |
| <pre><serviceGroup> |
| <!-- Invoke SpringInit on server startup and shutdown --> |
| <service name="SpringAwareService" class="spring.SpringInit"> |
| <description> |
| simple spring example - inside the AAR |
| </description> |
| <!-- need the TCCL param when using spring inside the AAR --> |
| <parameter name="ServiceTCCL">composite</parameter> |
| <parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier</parameter> |
| <parameter name="SpringBeanName">springAwareService</parameter> |
| <operation name="getValue"> |
| <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/> |
| </operation> |
| </service> |
| </serviceGroup></pre></div> |
| |
| <ul> |
| |
| <li><b><a name="a263"></a>Known issues running Spring inside the |
| AAR</b></li> |
| </ul> |
| |
| |
| <p>By default, the Axis2 classloader strategy does not permit Spring to run |
| inside the AAR. To allow Spring to run inside the AAR, the 'composite' |
| parameter is used in the services.xml as shown in the example above. There |
| was default behavior of 'composite' in the development cycle in between 1.0 |
| and 1.1, but it resulted in the JIRA issue AXIS2-1214 -problems with getting |
| an initContext.lookup() handle inside the AAR. Spring users typically have |
| little desire to use initContext.lookup(), as they get their Datasources via |
| org.springframework.jdbc.datasource.DriverManagerDataSource in an XML file or |
| with annotations. For EJB home references and the like, Spring provides |
| JndiObjectFactoryBean. </p> |
| |
| |
| <p>A common requirement is to run Hibernate along with Spring with Axis2 web services. |
| It is easier to run Hibernate as well as Spring outside the AAR as shown in the |
| ServletContext example, ie, place the Spring and Hibernate jars in WEB-INF/lib and |
| the hibernate config files under WEB-INF/classes. With that advisement, Spring provides |
| an API that allows Spring to load Hibernate under the contraints of an AAR. </p> |
| |
| |
| <p>Hibernate by default looks for its config files in the classpath. By running Hibernate |
| inside the AAR, Hibernate won't be able to find its config files. The way to get |
| around this limitation is either to expand the AAR or place the hibernate config |
| files in a specific directory under WEB-INF/classes - and then use |
| <a class="externalLink" href="http://www.springframework.org/docs/api/org/springframework/orm/hibernate/LocalSessionFactoryBean.html#setMappingDirectoryLocations(org.springframework.core.io.Resource[])">Spring's setMappingDirectoryLocations</a> for several options. </p> |
| |
| |
| <p>By placing Spring into DEBUG mode you can look at the logs to see where Spring will look |
| for your jar / class locations. Use the wildcards in the following example to list your mapping |
| locations as shown: </p> |
| |
| |
| <div> |
| <pre><bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> |
| <property name="mappingLocations"> |
| <value>classpath*:**/MyEntity.hbm.xml</value> |
| </property> |
| ... |
| </bean> </pre></div> |
| </div> |
| </html> |
| </div> |
| </div> |
| </div> |
| <hr/> |
| <footer> |
| <div class="container-fluid"> |
| <div class="row-fluid"> |
| <p>Copyright ©2004–2018 |
| <a href="https://www.apache.org/">The Apache Software Foundation</a>. |
| All rights reserved.</p> |
| </div> |
| </div> |
| </footer> |
| </body> |
| </html> |