springboot java ws_Spring Boot将JAX-WS Web服务注册为Bean

本文介绍了在Spring Boot应用中如何处理JAX-WS Web服务的Spring Bean自动装配问题,由于SpringBootServletInitializer的特殊初始化方式,导致SpringBeanAutowiringSupport无法正常工作。解决方案是通过实现ServletContextInitializer接口来定位并注册WebApplicationContext,然后在JAX-WS端点类中使用这个上下文进行bean注入。同时提供了单元测试的配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

小编典典

SpringBeanAutowiringSupport推荐使用扩展方法,从当前的Spring根Web应用程序上下文中为JAX-

WS端点类注入bean。但是,这不适用于 spring boot,

因为它在servlet上下文初始化方面有些不同。

问题

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);

}

2020-11-16

Spring Boot 可以通过集成 JAX-RS 规范来使用 RESTful 服务。下面是一些步骤,帮助您在 Spring Boot 应用程序中使用 JAX-RS: 1. 添加 JAX-RS 依赖 将以下依赖项添加到您的 pom.xml 文件中: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency> ``` 2. 创建 REST 资源类 创建一个类,并使用 @Path 注解将其标记为 REST 资源类。在这个类中,您可以使用 JAX-RS 注解定义 RESTful 资源的行为。 ```java import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("hello") public class HelloResource { @GET @Produces(MediaType.TEXT_PLAIN) public String sayHello() { return "Hello, World!"; } } ``` 3. 创建一个 Application 类 创建一个继承自 javax.ws.rs.core.Application 的类,并使用 @ApplicationPath 注解将其标记为应用程序的根路径。 ```java import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("api") public class JaxrsApplication extends Application { } ``` 4. 启动应用程序 在您的 Spring Boot 应用程序中添加以下代码来启动 JAX-RS: ```java import org.glassfish.jersey.server.ResourceConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public ResourceConfig resourceConfig() { return new ResourceConfig(); } } ``` 5. 启动应用程序并测试 启动应用程序,访问 URL http://localhost:8080/api/hello,您应该可以看到 "Hello, World!" 这个字符串。 这就是在 Spring Boot 应用程序中集成 JAX-RS 的基本步骤。您可以根据您的需求,使用更多的 JAX-RS 注解来定义 RESTful 资源类的行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值