Java 使用spring引导和JAXRSServerFactoryBean时无法注入spring服务类
我有一个运行Spring Boot的独立应用程序。它在应用程序的根(/)处为Soap端点注册一个CXF servlet。我还通过编程方式添加了CXF JAXRJava 使用spring引导和JAXRSServerFactoryBean时无法注入spring服务类,java,spring,cxf,jax-rs,spring-boot,Java,Spring,Cxf,Jax Rs,Spring Boot,我有一个运行Spring Boot的独立应用程序。它在应用程序的根(/)处为Soap端点注册一个CXF servlet。我还通过编程方式添加了CXF JAXR @Configuration @EnableAutoConfiguration @ComponentScan public class Application { /** * register cxf SOAP web services with spring boot * @return */
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
/**
* register cxf SOAP web services with spring boot
* @return
*/
@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new CXFServlet(), "/*");
}
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
// register cxf SOAP REST services with spring boot
JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
factory.setProviders(Arrays.asList(new JacksonJsonProvider()));
factory.setResourceClasses(ConfigureSoapResponseServiceImpl.class);
factory.setAddress("/rest");
factory.create();
}
}
到目前为止,这适用于注册我的所有jaxws和jaxrs服务。但是,我无法将服务类注入rest端点。我假设这是因为我使用JAXRSServerFactoryBean以编程方式设置它
@Consumes("application/json")
@Produces("application/json")
@Path("/config")
public interface ConfigureSoapResponseService {
@GET
@Path("/")
public List<String> getAllOperations(@QueryParam("wsdlLocation") String wsdlLocation);
}
@Service
public class ConfigureSoapResponseServiceImpl implements ConfigureSoapResponseService {
@Autowired
public WsdlInspectionService wsdlInspectionService;
@Override
public List<String> getAllOperations(String wsdlLocation) {
return wsdlInspectionService.getWsdlOperations(wsdlLocation);
}
@PostConstruct
public void init() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
}
@Component("wsdlInspectionService")
public class WsdlInspectionServiceImpl implements WsdlInspectionService {
public List<String> getWsdlOperations(String wsdlLocation) {
// ...
}
}
这不是我的服务。如何设置此选项以正确注入?为什么要注入?JAX-RS类对Spring和应用程序上下文一无所知,只创建一个新的bean实例,而不是从上下文获取实例或使用上下文进行自动连接。另外,您的web.xml是无用的,因为Spring Boot对它没有任何作用,所以这也意味着您的
cxfservlet.xml
没有任何作用。有关两者集成的更多信息,请参阅。我希望能够将服务类注入到资源类中。cxf-servlet.xml正在注册jaxws端点。不,不是因为它没有被加载,因为Spring Boot/Embedded tomcat忽略了web.xml。。。这两个在这个设置中都是无用的,因为如果我删除web.xml,我会在maven war插件中得到一个异常。我没有在pom.xml中显式调用maven war插件。然而,我正在构建独立战争,而不是一个罐子,所以这可能就是那里正在发生的事情。。。我想我已经决定把它分成两个不同的应用程序。我需要将我的CXF jaxws路由到/*以便我无法将任何内容定向到/rest或任何与此相关的内容。通过主应用程序启动CXF JAXR是注册CXF JAXR的一种变通方法。然而,我认为我希望使用SpringMVC而不是CXFRestServices,因为我认为它更适合web前端。谢谢你的帮助和洞察力。
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<jaxws:endpoint
xmlns:tns="https://someService/"
id="someService"
address="/someService"
serviceName="tns:someService"
endpointName="tns:someService"
implementor="net.livetv.ws.SomeServicePortImpl">
</jaxws:endpoint>
// etc...
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>cxf</servlet-name>
<display-name>cxf</display-name>
<description>Apache CXF Endpoint</description>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
return wsdlInspectionService.getWsdlOperations(wsdlLocation);