title: Spring Boot register JAX-WS webservice as bean
body: <p>In my spring boot ws based application I have created a jax-ws webservice following a contract first approach. The Web service is up but I cannot autowire my other beans inside my webservice.</p>
  <p>How can I define, my webservice in spring as bean?</p>
  <p>Following is my webservice impl class:</p>
  <pre class="lang-java prettyprint-override"><code>@WebService(endpointInterface = &quot;com.foo.bar.MyServicePortType&quot;)
  @Service
  public class MySoapService implements MyServicePortType {
      @Autowired
      private MyBean obj;
  
      public Res method(final Req request) {
          System.out.println(&quot;\n\n\nCALLING.......\n\n&quot; + obj.toString()); //obj is null here
          return new Res();
      }
  }
  </code></pre>
  <p>MyServicePortType is geneated by maven from wsdl file</p>
  <p>When I call this service (via SoapUi) it gives <code>NullPointerException</code> as the MyBean object is not autowired.</p>
  <p>Since my application is built on Spring boot, there is no xml file. Currently I have <code>sun-jaxws.xml</code> file with endpoint configuration. How can I do following configuration in spring boot application</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;wss:binding url=&quot;/hello&quot;&gt;
      &lt;wss:service&gt;
          &lt;ws:service bean=&quot;#helloWs&quot;/&gt;
      &lt;/wss:service&gt;
  &lt;/wss:binding&gt;
  </code></pre>
  <p>Following is my <code>SpringBootServletInitializer</code> class:</p>
  <pre class="lang-java prettyprint-override"><code>@Configuration
  public class WebXml extends SpringBootServletInitializer {
      @Override
      protected SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
          return application.sources(WSApplication.class);
      }
  
      @Bean
      public ServletRegistrationBean jaxws() {
          final ServletRegistrationBean jaxws = new ServletRegistrationBean(new WSServlet(), &quot;/jaxws&quot;);
          return jaxws;
      }
  
      @Override
      public void onStartup(final ServletContext servletContext) throws ServletException {
          super.onStartup(servletContext);
          servletContext.addListener(new WSServletContextListener());
      }
  }
  </code></pre>