当前位置: 首页 > 知识库问答 >
问题:

Spring Boot将JAX-WS webservice注册为bean

滕翔飞
2023-03-14

在我的基于spring boot ws的应用程序中,我遵循契约优先的方法创建了一个jax-ws webservice。Web服务已启动,但我无法在WebService中调用其他bean。

@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();
    }
}

由于我的应用程序是基于Spring boot构建的,所以没有xml文件。目前,我有sun-jaxws.xml文件和endpoint配置。如何在spring boot应用程序中进行以下配置

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

下面是我的SpringBootServletInitializer类:

@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());
    }
}

共有1个答案

谯和煦
2023-03-14

扩展SpringBeanAutowiringSupport是从当前spring根web应用程序上下文中为JAX-WSendpoint类注入bean的推荐方法。但是,这并不适用于spring boot,因为它在servlet上下文初始化方面有点不同。

SpringBootServletInitializer.startup()使用自定义的ContextLoaderListener并且不将创建的应用程序上下文传递给ContextLoader。稍后,当初始化JAX-WSendpoint类的对象时,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
                }
            });
        }
        ......
    }
}

您可以注册一个实现org.springframework.boot.context.embeddedd.ServletContextInitializer的bean,以便在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-WSendpoint类中实现自自动化。

@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);
}
 类似资料:
  • 问题内容: 在基于春季启动ws的应用程序中,我遵循合同优先方法创建了一个jax-ws Web服务。Web服务已启动,但是我无法自动将其他bean绑定到Webservice中。 我如何在春季将Web服务定义为bean? 以下是我的webservice impl类: MyServicePortType由Maven从wsdl文件生成 当我通过SoapUi调用此服务时,它会给出,因为MyBean对象未自动

  • 我一直在尝试将mysql配置为WildFly中的数据源。我不确定我错过了什么,我得到一个错误启动。 我在文件夹“/wildfly-8.1.0.final/module/system/layers/base/com/mysql/main”中有mysql-connector-java-5.0.8-bin.jar和module.xml 下面是文件 module.xml standalone.xml 这是

  • 我正试图在Tomcat 7.0.34.0上使用Servlet3.0应用程序类开发一个RESTful webapp,但似乎没有任何工作。我对以前的应用程序使用Jersey ServletContainer方法并在web.xml中声明所有REST服务没有任何问题。 我的当前应用程序子类: 和web服务: 当我尝试部署应用程序时,Tomcat告诉我上下文已重新加载: web应用程序本身已经部署(我可以访

  • 我有WebLogic服务器版本:12.2.1.2.0和JBoss BPM套件版本6.4.0 我尝试部署business central。war应用程序到WebLogic服务器。迪福商业中心。战争有网络。使用此servlet的xml地址: 从doc WebLogic我看到,business-central.war正确构建,但部署到服务器后,url更改为 /resources/,WebLogic写[

  • 本文向大家介绍springboot 注册服务注册中心(zk)的两种方式详解,包括了springboot 注册服务注册中心(zk)的两种方式详解的使用技巧和注意事项,需要的朋友参考一下 在使用springboot进行开发的过程中,我们经常需要处理这样的场景:在服务启动的时候,需要向服务注册中心(例如zk)注册服务状态,以便当服务状态改变的时候,可以故障摘除和负载均衡。 我遇到过两种注册的途径: 1、

  • 本文向大家介绍SpringBoot的服务注册与发现示例,包括了SpringBoot的服务注册与发现示例的使用技巧和注意事项,需要的朋友参考一下 微服务 实践“微服务”自然要学习如何做服务注册与发现 基于SpringBoot来进行微服务的学习,自然选择了与之息息相关的SpringCloud;当然可以选择其他的技术进行,比如dubbo 也可以用zookeeper来实现服务注册与发现,至于zookeep