Spring Boot Register JAX-WS webservice as a bean

In my spring boot ws application, I created the jax-ws web service after the first approach to the contract. The web service is working, but I cannot auto-retry my other beans inside my web service.

How can I determine my web service in spring as a bean?

Below is my webservice impl class

@WebService(endpointInterface = "com.foo.bar.MyServicePortType")
@Service
public class MySoapService implements MyServicePortType {

@Autowired
private MyBean obj;


public Res method(final Req request) {
    System.out.println("\n\n\nCALLING.......\n\n" + obj.toString()); //obj is null here
    return new Res();
}
}

MyServicePortType generated by maven from wsdl file

When I call this service (via SoapUi), it gives a NullPointerException, because the MyBean is not auto-sensing.

Since my application is built on spring boot, there is no xml file. I currently have an sun-jaxws.xmlendpoint configuration file . How to perform the following configuration in a spring boot application

    <wss:binding url="/hello">
    <wss:service>
        <ws:service bean="#helloWs"/>
    </wss:service>
    </wss:binding>

Below is my SpringBootServletInitializer class

@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(), "/jaxws");
    return jaxws;
}

@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
    super.onStartup(servletContext);
    servletContext.addListener(new WSServletContextListener());
}
}

thank

+4
4

SpringBeanAutowiringSupport - beans JAX-WS, - spring. spring boot, .

SpringBootServletInitializer.startup() ContextLoaderListener ContextLoader. , JAX-WS , SpringBeanAutowiringSupport ContextLoader null.

public abstract class SpringBootServletInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext rootAppContext = createRootApplicationContext(
                servletContext);
        if (rootAppContext != null) {
            servletContext.addListener(new ContextLoaderListener(rootAppContext) {
                @Override
                public void contextInitialized(ServletContextEvent event) {
                    // no-op because the application context is already initialized
                }
            });
        }
        ......
    }
}

bean, org.springframework.boot.context.embedded.ServletContextInitializer, startup().

@Configuration
public class WebApplicationContextLocator implements ServletContextInitializer {

    private static WebApplicationContext webApplicationContext;

    public static WebApplicationContext getCurrentWebApplicationContext() {
        return webApplicationContext;
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    }
}

JAX-WS.

@WebService
public class ServiceImpl implements ServicePortType {

    @Autowired
    private FooBean bean;

    public ServiceImpl() {
        AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
        WebApplicationContext currentContext = WebApplicationContextLocator.getCurrentWebApplicationContext();
        bpp.setBeanFactory(currentContext.getAutowireCapableBeanFactory());
        bpp.processInjection(this);
    }

    // alternative constructor to facilitate unit testing.
    protected ServiceImpl(ApplicationContext context) {
        AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
        bpp.setBeanFactory(new DefaultListableBeanFactory(context));
        bpp.processInjection(this);
    }
}

spring .

@Autowired 
private ApplicationContext context;

private ServicePortType service;

@Before
public void setup() {
    this.service = new ServiceImpl(this.context);
}
+3

Spring JAX-WS - JAX-WS, Spring. , SpringBeanAutowiringSupport. , :

public class MySoapService extends SpringBeanAutowiringSupport implements MyServicePortType {
}

, @PostConstruct:

public class MySoapService implements MyServicePortType {

    @PostConstruct
    public void init() {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }
}
+1

SpringBootServletInitializer configure() onStartup(). -, WebApplicationInitializer. ( @Configuration-, @SpringBootApplication , - , @ComponentScan).

  • CXFServlet ServletRegistrationBean
  • SpringBus
  • JAX-WS SEI (Service Interface)
  • EndpointImpl SpringBus SEI, Beans
  • () EndpointImpl

.

@SpringBootApplication
public class SimpleBootCxfApplication {

    public static void main(String[] args) {
        SpringApplication.run(SimpleBootCxfApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/soap-api/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }    

    @Bean
    public WeatherService weatherService() {
        return new WeatherServiceEndpoint();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), weatherService());
        endpoint.publish("/WeatherSoapService");
        return endpoint;
    }
}
+1

Taking the key from @Andy Wilkinson using SpringBeanAutoWiringSupport , you can use

public class MySoapService extends SpringBeanAutowiringSupport implements MyServicePortType {
}

You also have the option to call it directly from a method annotated with @PostConstruct:

@Service
public class MySoapService implements MyServicePortType {

    @Autowired
    ServletContext servletContext;

    @PostConstruct
    public void init() {
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
            servletContext);
    }
}
0
source

All Articles