java – Spring Boot将JAX-WS webservice注册为bean
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Spring Boot将JAX-WS webservice注册为bean,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3935字,纯文字阅读大概需要6分钟。
内容图文
在我的spring boot基于ws的应用程序中,我根据合同第一种方法创建了一个jax-ws webservice. Web服务已启动,但我无法在我的Web服务中自动装配其他bean.
我如何定义,我的网络服务在春天作为bean?
以下是我的webservice impl类
@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由来自wsdl文件的maven生成
当我调用此服务(通过SoapUi)时,它会给出NullPointerException,因为MyBean对象没有自动装配.
由于我的应用程序是基于Spring启动的,因此没有xml文件.目前我有带端点配置的sun-jaxws.xml文件.如何在spring启动应用程序中执行以下配置
<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());
}
}
谢谢
解决方法:
扩展SpringBeanAutowiringSupport是从当前Spring根Web应用程序上下文为JAX-WS端点类注入bean的推荐方法.但是这不适用于弹簧启动,因为它在servlet context initialization上有点不同.
问题
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
}
});
}
......
}
}
解决方法
您可以注册一个实现org.springframework.boot.context.embedded.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-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);
}
内容总结
以上是互联网集市为您收集整理的java – Spring Boot将JAX-WS webservice注册为bean全部内容,希望文章能够帮你解决java – Spring Boot将JAX-WS webservice注册为bean所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。