从 Spring Boot 2.7 更新后。到版本 3 时,出现了奇怪的错误,即没有可用的“jakarta.xml.ws.WebServiceContext”类型的合格 bean。我按照迁移步骤(javax 到 jakarta)进行操作,编译项目工作正常,但无法启动。 SOAP 请求需要 WebServiceContext,因为我需要从标头/正文中提取一些信息。 有没有办法注入WebServiceContext?在 Spring 2.7 版本中运行良好
我已经消除了包含旧 Spring 版本的第三方库,以便旧版本不会干扰 Spring 的功能。 我还看了一些像this这样的讨论,但没有一个有帮助。 按照建议,我尝试使用具有相同结果的 setter 注入 WebServiceContext。
@jakarta.jws.WebService
public class MyEndpoint {
...
private WebServiceContext context;
@Resource
public void setContext(WebServiceContext context) {
this.context = context;
}
...
如果您使用的是 Maven,请尝试在 pom.xml 文件中添加这些依赖项
<dependency>
<groupId>jakarta.xml.ws</groupId>
<artifactId>jakarta.xml.ws-api</artifactId>
<version>3.0.0</version>
</dependency>
如果不起作用你也可以尝试将@Resource更改为@Autowired 这可能有助于 Spring 将 WebServiceContext 识别为需要注入的 bean。
@jakarta.jws.WebService
public class MyEndpoint {
...
@Autowired
private WebServiceContext context;
public void setContext(WebServiceContext context) {
this.context = context;
}
...
@Configuration
public class WebServiceConfig {
@Bean
public WebServiceContext webServiceContext() {
return new WebServiceContext() {
// implementation of WebServiceContext methods
};
}
}
我在 Spring Boot 3 中的其他几个 bean 中也遇到过这个问题。每次问题都与尝试自动装配一个接口(而不是类)的 Bean 有关。一种解决方案是注册一个 bean,该 bean 返回实现相关接口的类的实例。
本例中,来自Jakarta的
WebServiceContext
是接口,我选择的实现类来自org.apache.cxf.jaxws.context
。我在 @Configuration 类中注册了以下 bean,它解决了问题:
import org.apache.cxf.jaxws.context.WebServiceContextImpl;
import jakarta.xml.ws.WebServiceContext;
import org.springframework.context.annotation.Configuration;
@Bean
public WebServiceContext webServiceContext() {
return new WebServiceContextImpl();
}
这种方法使配置变得明确,这可能很好。附带说明一下,您还可以包含 spring-boot-starter 依赖项,它为基于 application.properties 值的接口提供基本实现,因此您无需配置它们。我没能找到适合这个案例的,但我没有找很长时间