title: Axis2(aar) + spring, without a servletContext
body: <p>Greetings dear Stackoverflow users, I have been lately in lots of pain with one specific problem with axis2 web services with Spring framework. I have read lots of different guides and read different forums but found people with the same problems but with no solutions. Basically ended up holding the monitor with both of my hands and yelling "What did you find out BudapestHacker938?". Anyway my axis2 web service class needs Spring beans and therefore they are autowired inside the web service class. Everything works so well inside the jetty server where I have servletContext. Just define needed listeners in web.xml and it works. Such a bliss. But unfortunately all good things come to the end in some point, for me, the devil is CICS environment inside of mainframe. There is no servletcontext like in Jetty/Tomcat, luckily it still has axis2 support. So according to the different user-guides I decided to archive my web-service into .aar and added it under the services folder. Axis2 folder structure is the following:</p>
  
  <ul>
  <li>repository/
  <ul>
  <li>modules</li>
  <li>services</li>
  </ul></li>
  </ul>
  
  <p>When I am building this .aar archive then I am also generating my own wsdl, not using axis2 inbuilt wsdl generator which according to services.xml generates the services out of the given class (when I am running the axis2server, not using because doesn't like JAX-WS annotations as far as I know). To initialize Spring framework, I needed to write little SpringInit class which initializes Spring beans. Unfortunately it also for some reason initializes my web-service class according to its annotations and then occupies the main port(suspect that SpringInit intializes by its own the web service class since it is also defined as a Spring bean and SpringInit extends Axis2 class ServiceLifeCycle) and I get JVM BIND exception where it is stating that address is already in use. I would like to have the service built up according to the wsdl which is stored inside of the WSDL rather than generate new one, because I have various environments: 1) local machine - Jetty 2) mainframe. Anyway I give an insight to my services.xml:</p>
  
  <pre><code>&lt;service name="Absence" class="org.services.SpringInit"&gt;
  &lt;description&gt;
      random description
  &lt;/description&gt;
  &lt;parameter name="ServiceTCCL"&gt;composite&lt;/parameter&gt;
  &lt;parameter name="useOriginalwsdl" locked="false"&gt;true&lt;/parameter&gt;
  &lt;parameter name="ServiceObjectSupplier"&gt;org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier&lt;/parameter&gt;
  &lt;parameter name="ServiceClass"&gt;org.services.Absence&lt;/parameter&gt;
  &lt;parameter name="SpringBeanName"&gt;absence&lt;/parameter&gt;
  &lt;parameter name="SpringContextLocation"&gt;META-INF/applicationContextAar.xml&lt;/parameter&gt;
  &lt;/service&gt;
  </code></pre>
  
  <p>Spring applicationContextAar.xml, little bit refactored it for dear Stack community:</p>
  
  <pre><code>&lt;beans&gt;
  &lt;bean id="applicationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" /&gt;
  &lt;bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/&gt;
  &lt;bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
  &lt;property name="driverClassName" value="org.h2.Driver" /&gt;
  &lt;property name="url" value="jdbc:h2:tcp://localhost/~/devDb" /&gt;
  &lt;property name="username" value="sa" /&gt;
  &lt;property name="password" value="" /&gt;
  &lt;/bean&gt;
  &lt;bean id="absence" class="org.services.Absence"&gt;&lt;/bean&gt;
  &lt;bean id="jtemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"&gt;
  &lt;constructor-arg ref="ds"&gt;&lt;/constructor-arg&gt;
  &lt;/bean&gt;
  &lt;bean id="datasetFactory" class="org.vsam.DataSetFactory"&gt;&lt;/bean&gt;
  &lt;bean id="dataManagerFactory" class="org.datamanager.DataManagerFactory"&gt;&lt;/bean&gt;
  &lt;bean id="absenceFactory" class="org.services.AbsenceFactory"&gt;&lt;/bean&gt;
  &lt;bean id="h2Database" class="org.dataset.H2Database"&gt;&lt;constructor-arg ref="jtemplate"&gt;&lt;/constructor-arg&gt;
  &lt;/bean&gt;
  &lt;bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter"&gt;&lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>My SpringInit class looks something like that:</p>
  
  <pre><code>public class SpringInit implements ServiceLifeCycle {
  
  public void startUp(ConfigurationContext ignore, AxisService service) {
  
      try {
          ClassLoader classLoader = service.getClassLoader();
          ClassPathXmlApplicationContext appCtx = new
          ClassPathXmlApplicationContext(new String[] {"applicationContextAar.xml"}, false);
              appCtx.setClassLoader(classLoader);
              appCtx.refresh();
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  }
  
  public void shutDown(ConfigurationContext ctxIgnore, AxisService ignore) {}
  }
  </code></pre>
  
  <p>Now we are moving to org.services.Absence.class, it is an ordinary JAX-WS web-service class with following header (contains JAX-WS annotations):</p>
  
  <pre><code>@WebService(name = "AbsenceService", serviceName = "Absence", portName = "Absence",
  targetNamespace = "http://www.something.org/Absence")
  public class Absence extends ServiceHandlerBase { 
  
      @Autowired
  private AbsenceFactory absenceFactory;
  
  @Autowired
  private DataManagerFactory dataManagerFactory;
  
  @Autowired
  private DataSetFactory dataSetFactory;
  
  
  ... 
  
  }
  </code></pre>
  
  <p>Containing methods like that:</p>
  
  <pre><code>@WebMethod
  @WebResult(name = "AbsenceResponse")
  public SearchAbsenceRecordsResponse invokeSearchAbsenceRecords(
          @WebParam ServiceRequest request,
          @WebParam SearchAbsenceRecordsRequest absenceRequest) {...}
  </code></pre>
  
  <p>One alternative is to add "servicejars" folder into "repository" folder and populate it with absence.jar which has all its dependencies in the sub-folder "lib". Axis2 then automatically runs absense.jar since it has JAX-WS annotation. But in there when I call out the web-service for example with SOAP-UI, it doesn't have Spring initialized since I don't know how to initialize Spring in that solution. Maybe someone has any expertise about that.</p>
  
  <p><strong>TL;DR</strong></p>
  
  <p>How do I get my Spring beans initialized in manner that it doesn't start the services in the web service class according to the annotation and would rather build up services according to the wsdl?</p>
  
  <p>You are welcome to ask questions.</p>